package com.gx.obe.evaluation.step.dialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.utils.GridUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.evaluation.step.listener.ImportEvaluationResultListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.evaluation.EvaluationFactorResultService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 导入其他专家的打分
 * @author guoyr
 */
public class ImportOtherExpertEvaluationResultDialog extends EDialog {

	private String supplierName;
	private Grid supplierGrid;
	private Grid factorGrid;
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private EButton selectAllBtn;
	private EButton selectBallAll;
	private EButton importScortBtn;
	private ImageResolver imageResolver = new ImageResolver();
	private List<Supplier> supplierList;
	private List<EvaluationFactor> evaluationFactorList;
	private ImportEvaluationResultListener listener;
	private EvaluationFactorResultService evaluationFactorResultService;
	private ExpertService expertService ;
	private Label tipSecondLabel;
	private Combo expertCombo;
	
	{
		LogUtils.logClass(ImportOtherExpertEvaluationResultDialog.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProjectEntity
	 * @param evaluationStep
	 * @param supplierList
	 * @param evaluationFactorList
	 * @param listener
	 */
	public ImportOtherExpertEvaluationResultDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep, List<Supplier> supplierList, List<EvaluationFactor> evaluationFactorList, ImportEvaluationResultListener listener) {
		super(parentShell);
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		this.supplierList = supplierList;
		this.listener = listener;
		this.evaluationFactorList = evaluationFactorList;
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
	}

	/** 
	 * @Description: 
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(3, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		container.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		
		Composite topComposite = new Composite(container, SWT.NONE);
		topComposite.setLayout(new GridLayout(1, false));
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		tipSecondLabel = new Label(topComposite, SWT.WRAP);
		tipSecondLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		tipSecondLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		tipSecondLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tipSecondLabel.setText("");
		
		ESepator label_4 = new ESepator(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		label_4.setBackground(ColorConstants.SEP_LIGHT_COLOR);

		Composite composite_1 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(3, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.horizontalSpacing = 0;
		gl_composite_1.marginWidth = 0;
		gl_composite_1.marginHeight = 0;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.horizontalSpacing = 0;
		gl_composite_2.marginHeight = 0;
		gl_composite_2.marginWidth = 0;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 3));
		
		Composite composite_9 = new Composite(composite_2, SWT.NONE);
		composite_9.setLayout(new GridLayout(2, false));
		composite_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//		composite_9.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite_9.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_3 = new CLabel(composite_9, SWT.NONE);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("1、选择要导入结果的评标专家");
		
		expertCombo = new Combo(composite_9, SWT.READ_ONLY);
		expertCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		ESepator label = new ESepator(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		Composite composite = new Composite(composite_2, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
//		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_2 = new CLabel(composite, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_2.setText("2、选择要导入结果的评审指标");
		
		selectAllBtn = new EButton(composite, SWT.NONE);
		GridData gd_selectAllBtn = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_selectAllBtn.widthHint = 60;
		selectAllBtn.setLayoutData(gd_selectAllBtn);
		selectAllBtn.setText(Messages.Button_selectAll);
		
		selectBallAll = new EButton(composite, SWT.NONE);
		GridData gd_selectBallAll = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_selectBallAll.widthHint = 60;
		selectBallAll.setLayoutData(gd_selectBallAll);
		selectBallAll.setText(Messages.Button_selectInvert);
		selectBallAll.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectAllHistoryResultAction(false);
			}
		});
		selectAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectAllHistoryResultAction(true);
			}
		});
		
		ESepator sep3 = new ESepator(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep3.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		factorGrid = new Grid(composite_2, SWT.H_SCROLL | SWT.V_SCROLL);
		factorGrid.setHeaderVisible(true);
		factorGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn_4 = new GridColumn(factorGrid, SWT.CHECK);
		gridColumn_4.setTree(true);
		gridColumn_4.setText(Messages.EvalFactor);
		gridColumn_4.setWidth(200);
		gridColumn_4.setWordWrap(true);
		
		ESepator label_5 = new ESepator(container, SWT.SEPARATOR | SWT.VERTICAL);
		label_5.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
		label_5.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_3 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.verticalSpacing = 0;
		gl_composite_3.horizontalSpacing = 0;
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 0;
		composite_3.setLayout(gl_composite_3);
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.widthHint = 254;
		gd_composite_3.heightHint = 230;
		composite_3.setLayoutData(gd_composite_3);
		
		Composite composite_7 = new Composite(composite_3, SWT.NONE);
		composite_7.setLayout(new GridLayout(1, false));
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_7.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_1 = new CLabel(composite_7, SWT.NONE);
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_1.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label_1.setText("3、选择要导入结果的"+ supplierName);
		
		ESepator sep2 = new ESepator(composite_3, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep2.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		supplierGrid = new Grid(composite_3, SWT.V_SCROLL);
		supplierGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn_2 = new GridColumn(supplierGrid, SWT.CHECK);
		gridColumn_2.setText(supplierName);
		gridColumn_2.setWidth(300);
		gridColumn_2.setWordWrap(true);
		
		ESepator composite_sep4 = new ESepator(container, SWT.NONE);
		composite_sep4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		composite_sep4.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_4 = new Composite(container, SWT.NONE);
		composite_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 3, 1));
		GridLayout gl_composite_4 = new GridLayout(2, false);
		composite_4.setLayout(gl_composite_4);
		
		importScortBtn = new EButton(composite_4, SWT.NONE);
		importScortBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				importHistoryResultAction();
			}
		});
		importScortBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/download.png"));
		importScortBtn.setText("导入结果");
		
		EButton cancle_btn = new EButton(composite_4, SWT.NONE);
		cancle_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancle_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		cancle_btn.setLayoutData(gd_button_1);
		cancle_btn.setText(Messages.Button_close);

		init();
		return container;
	}

	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		expertService = new ExpertService();
		evaluationFactorResultService = new EvaluationFactorResultService();
		supplierGrid.setItemHeight(Constants.ROW_HIGHT);
		factorGrid.setItemHeight(Constants.ROW_HIGHT);
		factorGrid.setAutoHeight(true);
		this.setTitle("导入其他专家的打分");
		tipSecondLabel.setText("1、选择您要导入结果的评标专家  〉〉 2、选择要导入结果的评审指标 〉〉 3、 选择要导入结果的"+supplierName+"〉〉 4、点击“导入结果”");
		importScortBtn.setEnabled(true);
		refreshFactorGrid();
		refreshExpertGrid();
		refreshSupplierGrid();
	}
	
	/**
	 * @Description: 刷新投标项目列表
	 * @author guoyr
	 */
	private void refreshExpertGrid() {
		List<Expert> expertList = expertService.getEvaluationStepExpertList(tenderProjectEntity.getId(), evaluationStep);
		expertCombo.add("--请选择评标专家--", 0);
		if(null != expertList && expertList.size() > 0){
			int index = 1;
			for(Expert expert : expertList){

				expert.getUserId();
				if(expert.getUserId().equals(Constants.USER.getId())){
					continue;
				}
				AuthUser user = expert.getUser();
				if(null != user && null != user.getUserName()){
					expertCombo.add(user.getUserName(), index);
					expertCombo.setData(index +"userId", expert.getUserId());
					index ++;
				}
			
			}
			
			if(expertCombo.getItemCount() > 1){
			}else {
				importScortBtn.setEnabled(false);
			}
		}
		expertCombo.select(0);
	}
	
	/**
	 * @Description: 刷新投标人列表
	 * @author guoyr
	 */
	private void refreshSupplierGrid(){
		supplierGrid.removeAll();
		if(null != supplierList && supplierList.size() > 0){
			
			for(Supplier supplier : supplierList){
				GridItem gridItem = new GridItem(supplierGrid, SWT.NONE);
				gridItem.setData("supplierId", supplier.getId());
				gridItem.setChecked(0, true);
				gridItem.setText(0, ObjectUtils.getObjString(supplier.getSupplierName()));
				gridItem.setToolTipText(0, gridItem.getText(0));
			}
		}else {
			importScortBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 刷新投标人评标结果
	 * @author guoyr
	 */
	private void refreshFactorGrid(){
		factorGrid.removeAll();
		if(null != evaluationFactorList && evaluationFactorList.size() > 0){
			for(EvaluationFactor evaluationFactor : evaluationFactorList){
				if(ObjectUtils.equals(evaluationFactor.getFactorType(), EvaluationFactorEnum.FACTOR_FLOAT)){
					continue;
				}
				GridItem gridItem = new GridItem(factorGrid, SWT.NONE);
				gridItem.setText(0, evaluationFactor.getFactorName());
				gridItem.setData("factorId", evaluationFactor.getId());
				gridItem.setToolTipText(0, evaluationFactor.getFactorName());
				if(null != evaluationFactor.getObjective() && evaluationFactor.getObjective().equals(EvaluationFactorEnum.OBJECTIVE_YES)){
					gridItem.setChecked(0, true);
					gridItem.setForeground(0,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
				}
			}
		}
		if(factorGrid.getItemCount() > 0){
			selectBallAll.setEnabled(true);
			selectAllBtn.setEnabled(true);
		}else {
			selectBallAll.setEnabled(false);
			selectAllBtn.setEnabled(false);
			importScortBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 全选、反选
	 * @author guoyr
	 * @param selectAll
	 */
	private void selectAllHistoryResultAction(boolean selectAll){
		if(selectAll){
			GridUtils.checkAll(factorGrid, 0, true);
		}else {
			GridUtils.checkAllBack(factorGrid, 0);
			
		}
	}
	
	/**
	 * @Description: 导入其他专家的结果
	 * @author guoyr
	 */
	private void importHistoryResultAction(){
		if(importScortBtn.getEnabled()){
			
			String userId = (String)expertCombo.getData(expertCombo.getSelectionIndex()+"userId");
			if(StringUtils.isEmpty(userId)){
				MessageDialog.openWarning(getShell(), "请选择要导入结果的评标专家！");
				return;	
			}
			
			List<Object> checkedFactorList = GridUtils.getCheckedValues(factorGrid, 0, "factorId", true);
			List<GridItem> supplierItemList = GridUtils.getCheckedItems(supplierGrid, 0, true);
			if(null == supplierItemList || supplierItemList.size() < 1){
				MessageDialog.openWarning(getShell(), "请选择要导入结果的"+supplierName+"！");
				return;
			}
			if(null == checkedFactorList || checkedFactorList.size() < 1){
				MessageDialog.openWarning(getShell(), "请选择要导入结果的评审指标！");
				return;
			}
			// 是否打分节点
			boolean isScoreNode = false;
			if(null != evaluationStep.getStepType() && evaluationStep.getStepType().equals(com.gx.obe.components.core.enumeration.EvaluationStepEnum.DETAIL)){
				isScoreNode = true;
			}
			Map<String, Map<String, EvaluationFactorResult>> importResultMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
			for(GridItem gridItem : supplierItemList){
				String supplierId = (String)gridItem.getData("supplierId");
//				QueryWrapper queryWrapper = new QueryWrapper(EvaluationFactorResult.class);
//				queryWrapper.setWhereParam("tenderId", tenderProjectEntity.getId());
//				queryWrapper.setWhereParam("supplierId", supplierId);
//				queryWrapper.setWhereParam("userId", userId);
//				queryWrapper.setWhereParam("stepId", evaluationStep.getId());
				
				List<EvaluationFactorResult> evaluationFactorResultList = evaluationFactorResultService.getExpertEvaluationFactorResult(tenderProjectEntity.getId(), evaluationStep.getId(), userId, supplierId);
				if(null != evaluationFactorResultList && evaluationFactorResultList.size() > 0){
					Map<String, EvaluationFactorResult> supplierEvalResultMap = new HashMap<String, EvaluationFactorResult>();
					for(EvaluationFactorResult evaluationFactorResult : evaluationFactorResultList){
						if(checkedFactorList.contains(evaluationFactorResult.getFactorId())){
							if(isScoreNode && null != evaluationFactorResult.getEvaluationScore()){
								supplierEvalResultMap.put(evaluationFactorResult.getFactorId(), evaluationFactorResult);
							}else if(null != evaluationFactorResult.getEvaluationResult()){
								supplierEvalResultMap.put(evaluationFactorResult.getFactorId(), evaluationFactorResult);
							}
						}
					}
					importResultMap.put(supplierId, supplierEvalResultMap);
				}
			}
			getShell().close();
			if(null != listener){
				listener.importEvalResult(importResultMap);
			}
		}
	}
	@Override
	protected Point getInitialSize() {
		return new Point(789, 495);
	}
}
