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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.ColorConstants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.web.entity.Supplier;
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.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 商务评审表格界面
 * @author chenxw
 */
public class BusinessVetoEvaluationTableComposite extends Composite {
	
	private final ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private final ExpertEvaluationResultService expertEvaluationResultService = new ExpertEvaluationResultService();
	
	private final String EVAL_ACCESSMENT_YES = "合格";
	private final String EVAL_ACCESSMENT_NO = "不合格";
	
	private CLabel evalFactorMemoText;
	private BusinessVetoEvalResultComposite businessEvalResultComposite;
	
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private Expert expert;
	private List<Supplier> supplierList;
	
	private Map<String, ExpertEvaluationResult> evalResultMap;
	
	/**
	 * @author chenxw
	 * @param parent
	 * @param style
	 */
	public BusinessVetoEvaluationTableComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		Composite evaluationComposite = new Composite(this, SWT.NONE);
		GridLayout gl_evaluationComposite = new GridLayout(1, false);
		gl_evaluationComposite.verticalSpacing = 0;
		gl_evaluationComposite.horizontalSpacing = 0;
		gl_evaluationComposite.marginHeight = 0;
		gl_evaluationComposite.marginWidth = 0;
		evaluationComposite.setLayout(gl_evaluationComposite);
		evaluationComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluationComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		Composite composite_5 = new Composite(evaluationComposite, SWT.NONE);
		GridData gd_composite_5 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_composite_5.widthHint = 500;
		composite_5.setLayoutData(gd_composite_5);
		GridLayout gl_composite_5 = new GridLayout(1, false);
		gl_composite_5.marginHeight = 1;
		gl_composite_5.marginWidth = 1;
		gl_composite_5.verticalSpacing = 0;
		gl_composite_5.horizontalSpacing = 0;
		composite_5.setLayout(gl_composite_5);
		composite_5.setBackgroundMode(SWT.INHERIT_FORCE);
		
		evalFactorMemoText = new CLabel(composite_5, SWT.NONE);
		evalFactorMemoText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
		evalFactorMemoText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		evalFactorMemoText.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tips_blue.png"));
		
		Composite composite_1 = new Composite(evaluationComposite, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.marginHeight = 1;
		gl_composite_1.marginWidth = 1;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_1.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		businessEvalResultComposite = new BusinessVetoEvalResultComposite(composite_1, SWT.NONE, true);
		
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProject
	 * @param projectRule
	 * @param evaluationStep
	 * @param supplierList
	 */
	public void refresh(TenderProjectEntity tenderProject, EvaluationStep evaluationStep, Expert expert, List<Supplier> supplierList) {
		this.tenderProject = tenderProject;
		this.evaluationStep = evaluationStep;
		this.expert = expert;
		this.supplierList = supplierList;
		refreshEvaluationResult();
	}
	
	/**
	 * @Description: 刷新评审结果
	 * @author chenxw
	 */
	private void refreshEvaluationResult() {
		List<ExpertEvaluationResult> evaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProject.getId(), evaluationStep.getId(), expert.getUserId());
		if (null == evaluationResultList) {
			evalResultMap = new HashMap<String, ExpertEvaluationResult>();
		} else {
			evalResultMap = CollectionUtils.toHashMap(evaluationResultList, new CollectionUtils.NewMap<String, ExpertEvaluationResult>() {
				public String getKey(ExpertEvaluationResult t) {
					return t.getSupplierId();
				}
			});
		}
		refreshForm();
		refreshTableComposite();
	}
	
	/**
	 * @Description: 刷新表单
	 * @author chenxw
	 */
	private void refreshForm() {
		String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		evalFactorMemoText.setText("请对" + supplierName + "设置“" + EVAL_ACCESSMENT_YES + "”或“" + EVAL_ACCESSMENT_NO + "”，" + EVAL_ACCESSMENT_NO + "的" + supplierName + "将不能入围下一评标环节！");
	}
	
	/**
	 * @Description: 刷新表格界面
	 * @author chenxw
	 */
	private void refreshTableComposite() {
		for (Supplier supplier : supplierList) {
			ExpertEvaluationResult evalResult = evalResultMap.get(supplier.getId());
			if (null == evalResult) {
				initExpertEvaluationResult(supplier.getId());
			}
		}
		businessEvalResultComposite.refresh(tenderProject, supplierList, evalResultMap);
	}
	
	/**
	 * @Description: 初始化专家评审结果
	 * @author chenxw
	 * @return
	 */
	private void initExpertEvaluationResult(String supplierId) {
		ExpertEvaluationResult expertEvaluationResult = new ExpertEvaluationResult();
		expertEvaluationResult.setTenderId(tenderProject.getId());
		expertEvaluationResult.setStepId(evaluationStep.getId());
		expertEvaluationResult.setUserId(expert.getUserId());
		expertEvaluationResult.setSupplierId(supplierId);
		evalResultMap.put(supplierId, expertEvaluationResult);
	}
	
	public String check() {
		for (Supplier supplier : supplierList) {
			if (null == evalResultMap.get(supplier.getId()).getAccessment()) {
				String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
				return "存在未评审的" + supplierName + "，请检查并对所有" + supplierName + "进行评审！";
			}
		}
		return null;
	}
	
	/**
	 * @Description: 保存评审结果
	 * @author chenxw
	 * @return
	 */
	public boolean saveEvaluationResult() {
		return expertEvaluationStepService.saveBusinessVetoEvaluationResult(tenderProject.getId(), evaluationStep, expert.getUserId(), new ArrayList<ExpertEvaluationResult>(evalResultMap.values()));
	}
	
	/**
	 * @Description: 提交评审结果
	 * @author chenxw
	 * @return
	 */
	public boolean submitEvaluationResult() {
		return expertEvaluationStepService.submitBusinessVetoEvaluationResult(tenderProject.getId(), evaluationStep, expert.getUserId(), supplierList);
	}
	
	protected void checkSubclass() {}
	
}
