package com.gx.obe.evaluation.step.dialog;
import java.awt.Cursor;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridEditor;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.gx.obe.business.http.HttpResponse;
import com.gx.obe.business.http.HttpUrl;
import com.gx.obe.business.http.HttpUtils;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.entity.SupplierCreditEntity;
import com.gx.obe.http.HttpUtil;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.ScreenUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.SWTResourceManager;

import org.eclipse.swt.core.MessageDialog;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CreditChinaDialog extends EDialog{
	
	private TenderProjectEntity tenderProject;
	private Grid supplier_gird;
	private Table table;
	private List<Supplier> supplierList;
	private Composite composite_1;
	private GridEditor editor;
	private GridColumn gridColumn;
	private GridColumn gridColumn1;
	private GridColumn gridColumn2;
	private GridColumn gridColumn3;
	private Composite composite;
	private Map<String,String> supplierCreditMap =new HashMap<String,String>();
	private ArrayList<SupplierCreditEntity> supplierCreditEntityList=null;
	//private static final String URL="https://test.powerbeijing-ec.com/jndzzb/creditChinaController.do?getZhixingList&ids=";
	private final static String URL=PropertiesUtils.getProperty("creditChinaURL","https://www.powerbeijing-ec.com/jndzzb/creditChinaController.do?getZhixingList&ids=");

	/** 
	 * @author guoyr
	 * @param parentShell
	 * @param tenderProject
	 * @param templateType 
	 */
	public CreditChinaDialog(Shell parentShell, TenderProjectEntity tenderProject,List<Supplier> supplierList) {
		super(parentShell);
		setTitle("失信被执行人查询（数据来源于信用中国）");
		this.tenderProject = tenderProject;
		this.supplierList=supplierList;
	}

	/**
	 * @Description: 
	 * @author guoyr
	 * @param parent
	 * @return 
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		container.setData("tenderProject", tenderProject);
		
		
		composite = new Composite(container, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(211, 211, 211));
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		
		
		composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(255, 255, 255));
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		
		supplier_gird = new Grid(composite_1, SWT.V_SCROLL|SWT.SCROLL_PAGE);
		supplier_gird.setHeaderVisible(true);
		supplier_gird.setItemHeaderWidth(1400);
		supplier_gird.setItemHeight(35);
		supplier_gird.setAutoWidth(false);
		
		
		
		
		
		gridColumn = new GridColumn(supplier_gird, SWT.CENTER);
		gridColumn.setText("序号");
		gridColumn.setWidth(400);
		gridColumn.setMaxmumWidth(400);
		
		gridColumn1 = new GridColumn(supplier_gird, SWT.NONE);
		gridColumn1.setText("企业名称");
		gridColumn1.setWidth(400);
		gridColumn.setMaxmumWidth(400);
		
		gridColumn2 = new GridColumn(supplier_gird, SWT.CENTER);
		gridColumn2.setText("是否为失信被执行人");
		gridColumn2.setWidth(400);
		gridColumn.setMaxmumWidth(400);
		
		gridColumn3 = new GridColumn(supplier_gird, SWT.CENTER);
		gridColumn3.setText("查看详情");
		gridColumn3.setWidth(400);
		gridColumn.setMaxmumWidth(400);
		
		
		init();
		gridColumn.pack();
		gridColumn1.pack();
		gridColumn2.pack();
		gridColumn3.pack();
		supplier_gird.setFocus();
		supplier_gird.pack();
		supplier_gird.layout();
		container.pack();
		container.layout();
		
		
		
		return container;
	}
	
	private void init() {
		getSupplierCredit();
		refreshSupplierCredit();
		
	}

	private void getSupplierCredit() {
		// TODO Auto-generated method stub
		String ids = supplierList.stream().map(supplier -> {
			String orgId = supplier.getOrgId();
			return orgId;
			}).collect(Collectors.joining(","));
		String url =URL+ids;
		HttpUrl httpUrl = new HttpUrl(url);
		HttpResponse httpResponse = HttpUtils.doPost(httpUrl);
		System.err.println("接口信息："+httpUrl.getUrl() +"\r\n接口参数："+httpUrl.getAttributs()+"\r\n接口返回内容："+httpResponse);
		supplierCreditEntityList = new ArrayList<SupplierCreditEntity>();
		if(null != httpResponse && httpResponse.getStatus()){
			JSONArray supplierCreditListArray = httpResponse.getDataJSONArray("zhiXingSupplierList");
			Object[] supplierCreditJsonList =  supplierCreditListArray.toArray();
			if(null != supplierCreditJsonList && supplierCreditJsonList.length > 0){
				for(Object object : supplierCreditJsonList){
						SupplierCreditEntity supplierCreditEntity =new SupplierCreditEntity();
						JSONObject supplierCreditJson = (JSONObject)object;
						String creditCode = (String)supplierCreditJson.get("code");//社会统一信用代码
						String ifBreakLaw = ((String)supplierCreditJson.get("ifBreakLaw"));//是否失信被执行人(0否 1是)
						String supplierName = ((String)supplierCreditJson.get("name"));
						supplierCreditMap.put(supplierName, ifBreakLaw);
						supplierCreditEntity.setCode(creditCode);
						supplierCreditEntity.setIfBreakLaw(ifBreakLaw);
						supplierCreditEntity.setName(supplierName);
						supplierCreditEntityList.add(supplierCreditEntity);
					}
				}
			}else {
				MessageDialog.openError(getShell(), "调用失信执行人查询错误！");
				return;
			}
	}

	private void refreshSupplierCredit() {
		// TODO Auto-generated method stub
		supplier_gird.removeAll();
		if(null != supplierList && supplierList.size() > 0){
			int index = 1;
			for(Supplier supplier : supplierList){
				GridItem gridItem = new GridItem(supplier_gird,SWT.CENTER);
				setSupplierInfo(gridItem, index, supplier);
				index ++;
			}

		}
		
	}
	

	public void setSupplierInfo(GridItem gridItem, int index, Supplier supplier){
		gridItem.setText(0, index + "");
		gridItem.setText(1, ObjectUtils.getObjString(supplier.getSupplierName()));
		//gridItem.setText(1, "查看详情查看详情查看详情查看详情查看详情");
		//gridItem.setText(2, "否");
		gridItem.setText(2, supplierCreditMap.get(ObjectUtils.getObjString(supplier.getSupplierName())).equals("1")?"是":"否");
		gridItem.setData("supplier",supplier);
		Button button = new Button(supplier_gird, SWT.NONE);
		button.setBackground(SWTResourceManager.getColor(255, 255, 255));
        button.setText(" 查看详情 ");
        button.setData("supplier",supplier);
        editor =new GridEditor(supplier_gird);
        editor.grabHorizontal = true;
        editor.setEditor(button, gridItem,3);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                System.out.println("Button " +  " clicked");     
               Button button = (Button)e.getSource();
               Supplier supplier=(Supplier)button.getData("supplier");
               LookupDetailAction(supplier);
            }
        });
	}

	protected void LookupDetailAction(Supplier supplier) {
		// TODO Auto-generated method stub
		CreditChinaDetailDialog creditChinaDetailDialog = new CreditChinaDetailDialog(getShell(),supplier,supplierCreditEntityList);
		creditChinaDetailDialog.open();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		ScreenUtils  util = new ScreenUtils();
		int screenHight  = util.getScreenHight();
		return new Point(650,500);
	//return new Point(1000, 616);
	}

}
