package com.gx.obe.evaluation.step.resutl.composite;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.RectangleRadioButton;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.business.entity.SupplierEvaluationStep;
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.CommonEnum;
import com.gx.obe.components.core.enumeration.ExpertEnum;
import com.gx.obe.evaluation.step.composite.BusinessSummariseResultComposite;
import com.gx.obe.evaluation.step.composite.business.BusinessVetoEvalResultComposite;
import com.gx.obe.evaluation.step.dialog.ApplyReEvaluationDialog;
import com.gx.obe.evaluation.step.entity.BusinessSummariseResult;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierEvaluationStepService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description:
 * @author chenxw
 */
public class BusinessVetoEvaluationResultComposite extends Composite {
	
	private ExpertService expertService = new ExpertService();
	private SupplierService supplierService = new SupplierService();
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private ExpertEvaluationResultService expertEvaluationResultService = new ExpertEvaluationResultService();
	private SupplierEvaluationStepService supplierEvaluationStepService = new SupplierEvaluationStepService();
	
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	
	private Composite expert_list_composite;
	private Label message_label;
	private ELink applyReEvaluation_link;
	private Composite load_composite;
	private BusinessVetoEvalResultComposite businessVetoEvalResultComposite;
	private BusinessSummariseResultComposite businessSummariseResultComposite;
	private RectangleRadioButton curExpertButton;
	
	private StackLayout stackLayout = new StackLayout();
	
	private List<Expert> expertList;
	private String supplierName;
	private ExpertEvaluationStep expertEvaluationStep;
	private List<Supplier> supplierList;
	
	{
		LogUtils.logClass(BusinessVetoEvaluationResultComposite.class);
	}
	
	/**
	 * @author chenxw
	 * @param parent
	 * @param style
	 */
	public BusinessVetoEvaluationResultComposite(Composite parent, int style) {
		super(parent, style);
		
		this.tenderProject = (TenderProjectEntity) parent.getData("tenderProject");
		this.evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		
		Composite tabItem_composite = new Composite(this, SWT.NONE);
		tabItem_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_tabItem_composite = new GridLayout(1, false);
		gl_tabItem_composite.marginTop = 5;
		gl_tabItem_composite.verticalSpacing = 0;
		gl_tabItem_composite.marginWidth = 0;
		gl_tabItem_composite.marginHeight = 0;
		tabItem_composite.setLayout(gl_tabItem_composite);
		
		expert_list_composite = new Composite(tabItem_composite, SWT.NONE);
		expert_list_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		RowLayout rl_expert_list_composite = new RowLayout(SWT.HORIZONTAL);
		rl_expert_list_composite.marginLeft = 0;
		rl_expert_list_composite.marginBottom = 0;
		rl_expert_list_composite.spacing = 5;
		expert_list_composite.setLayout(rl_expert_list_composite);
		
		ESepator expert_tab = new ESepator(tabItem_composite, SWT.NONE);
		expert_tab.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		expert_tab.setBackground(ColorConstants.SKIN_BG);
		expert_tab.setLineWidth(2);
		
		Composite message_composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite_6 = new GridLayout(2, false);
		message_composite.setLayout(gl_composite_6);
		message_composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		message_label = new Label(message_composite, SWT.NONE);
		message_label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		message_label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		message_label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		applyReEvaluation_link = new ELink(message_composite, SWT.NONE);
		applyReEvaluation_link.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});
		applyReEvaluation_link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/hand.png"));
		applyReEvaluation_link.setText("申请退回[" + evaluationStep.getStepName() + "]");
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_2.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginHeight = 1;
		gl_composite_2.marginWidth = 1;
		composite_2.setLayout(gl_composite_2);
		
		load_composite = new Composite(composite_2, SWT.NONE);
		load_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		load_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		load_composite.setLayout(stackLayout);
		load_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		businessVetoEvalResultComposite = new BusinessVetoEvalResultComposite(load_composite, SWT.NONE, false);
		
		businessSummariseResultComposite = new BusinessSummariseResultComposite(load_composite, SWT.NONE);
		
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw
	 */
	private void init() {
		refreshTenderInfo();
		refreshExpertListComposite();
	}
	
	/**
	 * @Description: 刷新数据
	 * @author chenxw
	 */
	private void refreshTenderInfo() {
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		expertList = expertService.getEvaluationStepExpertList(tenderProject.getId(), evaluationStep);
		if (CollectionUtils.isNull(expertList)) {
			expertList = new ArrayList<Expert>();
		}
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProject.getId(), evaluationStep);
		if (CollectionUtils.isNull(supplierList)) {
			supplierList = new ArrayList<Supplier>();
		}
	}
	
	/**
	 * @Description: 刷新专家列表界面
	 * @author chenxw
	 */
	private void refreshExpertListComposite() {
		for (Control control : expert_list_composite.getChildren()) {
			control.dispose();
		}
		for (final Expert expert : expertList) {
			AuthUser user = expert.getUser();
			final RectangleRadioButton button = new RectangleRadioButton(expert_list_composite, SWT.NONE);
			button.addMouseListener(new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					curExpertButton.setSelection(false);
					curExpertButton = button;
					curExpertButton.setSelection(true);
					selectExpertAction(expert);
				}
			});
			button.setText(user.getUserName().concat(ExpertEnum.LEADER_YES.equals(expert.getIsLeader()) ? "（组长）" : ""));
			if (null == curExpertButton) {
				curExpertButton = button;
			}
		}
		final RectangleRadioButton button = new RectangleRadioButton(expert_list_composite, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				curExpertButton.setSelection(false);
				curExpertButton = button;
				curExpertButton.setSelection(true);
				showSummariseResult();
			}
		});
		button.setText(evaluationStep.getStepName().concat("汇总"));
		if (null == curExpertButton) {
			curExpertButton = button;
		}
		curExpertButton.notifyListeners(SWT.MouseUp, null);
		expert_list_composite.layout();
	}
	
	/**
	 * @Description: 选中专家事件
	 * @author guoyr
	 */
	private void selectExpertAction(Expert expert) {
		refreshMessageLabel(expert);
		showExpertResultComposite(expert);
	}
	
	/**
	 * @Description: 显示专家结果界面
	 * @author chenxw
	 * @param expert
	 */
	private void showExpertResultComposite(Expert expert) {
		stackLayout.topControl = businessVetoEvalResultComposite;
		load_composite.layout();
		List<ExpertEvaluationResult> evaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), expert.getUserId());
		Map<String, ExpertEvaluationResult> evalResultMap = CollectionUtils.toHashMap(evaluationResultList, new CollectionUtils.NewMap<String, ExpertEvaluationResult>() {
			public String getKey(ExpertEvaluationResult t) {
				return t.getSupplierId();
			}
		});
		businessVetoEvalResultComposite.refresh(tenderProject, supplierList, evalResultMap);
	}
	
	/**
	 * @Description: 刷新消息标签
	 * @author chenxw
	 * @param expert
	 */
	private void refreshMessageLabel(Expert expert) {
		applyReEvaluation_link.setVisible(false);
		StringBuilder msg = new StringBuilder();
		ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProject.getId(), evaluationStep.getId(), expert.getUserId());
		if (null == expertEvaluationStep) {
			msg.append("未开始[");
			msg.append(evaluationStep.getStepName());
			msg.append("]！");
		} else {
			msg.append("[");
			msg.append(evaluationStep.getStepName());
			msg.append("]评标开始时间：");
			msg.append(DateUtils.getTimestampFormat(expertEvaluationStep.getStartTime()));
			msg.append("---");
			if (CommonEnum.COMPLETE.equals(expertEvaluationStep.getStepStatus())) {
				msg.append("评标结束时间：");
				msg.append(DateUtils.getTimestampFormat(expertEvaluationStep.getEndTime()));
				if (expert.getUserId().equals(Constants.USER.getId())) {
					applyReEvaluation_link.setVisible(true);
				}
				this.expertEvaluationStep = expertEvaluationStep;
			} else {
				msg.append("[");
				msg.append(evaluationStep.getStepName());
				msg.append("]进行中...   ");
			}
		}
		message_label.setText(msg.toString());
		message_label.getParent().layout();
	}
	
	/**
	 * @Description: 显示汇总结果
	 * @author chenxw
	 */
	private void showSummariseResult() {
		refreshSummariseMessageLable();
		showSummariseResultComposite();
	}
	
	/**
	 * @Description: 显示汇总结果界面
	 * @author chenxw
	 */
	private void showSummariseResultComposite() {
		stackLayout.topControl = businessSummariseResultComposite;
		load_composite.layout();
		List<ExpertEvaluationResult> evaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), null);
		Map<String, List<ExpertEvaluationResult>> evalResultListMap = CollectionUtils.toArrayListHashMap(evaluationResultList, new CollectionUtils.NewMap<String, ExpertEvaluationResult>() {
			public String getKey(ExpertEvaluationResult t) {
				return t.getSupplierId();
			}
		});
		List<SupplierEvaluationStep> supplierEvaluationStepList = supplierEvaluationStepService.getSupplierEvaluationStepByTenderIdAndStepId(tenderProject.getId(), evaluationStep.getId());
		Map<String, SupplierEvaluationStep> evaluationStepMap = CollectionUtils.toHashMap(supplierEvaluationStepList, new CollectionUtils.NewMap<String, SupplierEvaluationStep>() {
			public String getKey(SupplierEvaluationStep t) {
				return t.getSupplierId();
			}
		});
		List<BusinessSummariseResult> summariseResultList = new ArrayList<BusinessSummariseResult>();
		for (Supplier supplier : supplierList) {
			summariseResultList.add(getBusinessSummariseResult(supplier, evalResultListMap.get(supplier.getId()), evaluationStepMap.get(supplier.getId())));
		}
		businessSummariseResultComposite.refresh(tenderProject, expertList, summariseResultList);
	}
	
	/**
	 * @Description: 获取商务否决汇总结果
	 * @author chenxw
	 * @param supplier
	 * @param evaluationResultList
	 * @param supplierEvaluationStep
	 * @return
	 */
	private BusinessSummariseResult getBusinessSummariseResult(Supplier supplier, List<ExpertEvaluationResult> evaluationResultList, SupplierEvaluationStep supplierEvaluationStep) {
		BusinessSummariseResult businessSummariseResult = new BusinessSummariseResult();
		businessSummariseResult.setSupplierName(supplier.getSupplierName());
		Map<String, ExpertEvaluationResult> evalResultMap = CollectionUtils.toHashMap(evaluationResultList, new CollectionUtils.NewMap<String, ExpertEvaluationResult>() {
			public String getKey(ExpertEvaluationResult t) {
				return t.getUserId();
			}
		});
		Map<String, String> expertResultMap = businessSummariseResult.getExpertResultMap();
		int accessYesCount = 0;
		int accessNoCount = 0;
		if (null != evalResultMap) {
			for (Expert expert : expertList) {
				ExpertEvaluationResult evalResult = evalResultMap.get(expert.getUserId());
				if (null == evalResult) {
					continue;
				}
				String accessment = evalResult.getAccessment();
				expertResultMap.put(expert.getId(), accessment);
				if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(accessment)) {
					accessNoCount++;
				} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(accessment)) {
					accessYesCount++;
				}
			}
		}
		businessSummariseResult.setAccessYesCount(accessYesCount);
		businessSummariseResult.setAccessNoCount(accessNoCount);
		if (null != supplierEvaluationStep) {
			businessSummariseResult.setSummariseResult(supplierEvaluationStep.getAccessment());
		}
		return businessSummariseResult;
	}
	
	/**
	 * @Description: 刷新汇总消息标签
	 * @author chenxw
	 */
	private void refreshSummariseMessageLable() {
		StringBuilder msg = new StringBuilder();
		if (CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus())) {
			msg.append("已完成[");
			msg.append(evaluationStep.getStepName());
			msg.append("]！   ");
		} else if (CommonEnum.IN_PROGRESS.equals(evaluationStep.getStepStatus())) {
			msg.append("[");
			msg.append(evaluationStep.getStepName());
			msg.append("]进行中...   ");
		} else {
			msg.append("未开始[");
			msg.append(evaluationStep.getStepName());
			msg.append("]！");
		}
		if (ObjectUtils.equals(evaluationStep.getFilterType(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_YES)) {
			msg.append("   评标规则：");
			if (ObjectUtils.equals(evaluationStep.getPassType(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.PASS_MORE)) {
				msg.append("“少数服从多数”");
			} else {
				msg.append("“一票否决”；");
			}
			msg.append("  结论为“");
			msg.append("不合格");
			msg.append("”的");
			msg.append(supplierName);
			msg.append("不能入围下一评标环节！");
		} else {
			msg.append("评标规则：所有");
			msg.append(supplierName);
			msg.append("全部入围下一评标环节！");
		}
		message_label.setText(msg.toString());
		applyReEvaluation_link.setVisible(false);
		message_label.getParent().layout();
	}
	
	/**
	 * @Description: 申请回退
	 * @author chenxw
	 */
	private void applyReEvaluationAction() {
		if (!applyReEvaluation_link.getEnabled()) {
			return;
		}
		if (null == expertEvaluationStep) {
			return;
		}
		ApplyReEvaluationDialog applyReEvaluationDialog = new ApplyReEvaluationDialog(getShell(), evaluationStep, expertEvaluationStep);
		applyReEvaluationDialog.open();
	}
	
	protected void checkSubclass() {}
}
