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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.business.enumeration.SupplierEnum;
import com.gx.obe.common.widget.button.EvalLeftButton;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.composite.business.BusinessEvaluationTableComposite;
import com.gx.obe.evaluation.step.dialog.ViewBoqDialog;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierOpeningResult;
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.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.SupplierOpeningResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.utils.ActionLogUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 商务评审
 * @author chenxw
 */
public class BusinessEvaluationComposite extends Composite implements BusinessEvaluationTableComposite.BusinessEvaluationListener {
	
	private final ExpertService expertService = new ExpertService();
	private final SupplierService supplierService = new SupplierService();
	private final ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private final SupplierOpeningResultService supplierOpeningResultService = new SupplierOpeningResultService();
	
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private TenderProjectRuleEntity projectRule;
	private Object hideLeft;
	
	private Composite left_tool_composite;
	private BusinessEvaluationTableComposite tableComposite;
	private EButton save_result_btn;
	private EButton submit_result_btn;
	
	private Expert CURRENT_EXPERT;
	private List<Supplier> supplierList;
	
	private ExpertEvaluationListener expertEvaluationListener;
	
	{
		LogUtils.logClass(BusinessEvaluationComposite.class);
	}
	
	/**
	 * @author chenxw
	 * @param parent
	 * @param style
	 */
	public BusinessEvaluationComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		this.tenderProject = (TenderProjectEntity) parent.getData("tenderProject");
		this.evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		this.projectRule = (TenderProjectRuleEntity) parent.getData("projectRule");
		this.hideLeft = parent.getData("hideLeft");
		
		left_tool_composite = new Composite(this, SWT.NONE);
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		left_tool_composite.setLayout(gl_left_tool_composite);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		left_tool_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		left_tool_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.SELECTED);
		goto_back.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		goto_back.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		goto_back.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/back.png"));
		goto_back.setToolTipText("返回");
		
		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.SELECTED);
		refresh_evaluation.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshTenderInfo();
			}
		});
		goto_back.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		refresh_evaluation.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refresh_evaluation.setToolTipText("刷新");
		
		EvalLeftButton viewTenderDetail_btn = new EvalLeftButton(left_tool_composite, SWT.NONE);
		viewTenderDetail_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				viewBoqAction();
			}
		});
		viewTenderDetail_btn.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/boq_1.png"));
		viewTenderDetail_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		viewTenderDetail_btn.setToolTipText("工程量清单");
		
		Composite evaluationComposite = new Composite(this, SWT.NONE);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		gl_container.horizontalSpacing = 0;
		evaluationComposite.setLayout(gl_container);
		evaluationComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite price_evaluation_table = new Composite(evaluationComposite, SWT.NONE);
		price_evaluation_table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		price_evaluation_table.setLayout(new FillLayout(SWT.HORIZONTAL));
		price_evaluation_table.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		tableComposite = new BusinessEvaluationTableComposite(price_evaluation_table, SWT.NONE);
		tableComposite.addBusinessEvaluationListener(this);
		
		Composite composite_5 = new Composite(evaluationComposite, SWT.NONE);
		composite_5.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_5.setLayout(new GridLayout(2, false));
		
		save_result_btn = new EButton(composite_5, SWT.NONE | EButton.CSS_GREEN);
		save_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationResultAction();
			}
		});
		GridData gd_save_result_btn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_save_result_btn.widthHint = 200;
		gd_save_result_btn.heightHint = 35;
		save_result_btn.setLayoutData(gd_save_result_btn);
		save_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		save_result_btn.setText("暂存结果");
		
		submit_result_btn = new EButton(composite_5, SWT.NONE | EButton.CSS_GREEN);
		submit_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});
		GridData gd_signBtn = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_signBtn.heightHint = 35;
		gd_signBtn.widthHint = 200;
		submit_result_btn.setLayoutData(gd_signBtn);
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		submit_result_btn.setText("完成评审");
		
		init();
	}
	
	/**
	 * @Description: 初始化界面数据
	 * @author chenxw
	 */
	private void init() {
		if (null != hideLeft) {
			left_tool_composite.dispose();
		}
		ActionLogUtils.addServerLog(tenderProject.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
		CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProject.getId(), evaluationStep);
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProject.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
		refreshTenderInfo();
	}
	
	/**
	 * @Description: 返回
	 * @author chenxw
	 */
	private void goBackAction() {
		if (null == expertEvaluationListener) {
			return;
		}
		expertEvaluationListener.complete();
	}
	
	/**
	 * @Description: 刷新项目信息
	 * @author chenxw
	 */
	private void refreshTenderInfo() {
		refreshSupplierList();
		tableComposite.refresh(tenderProject, CURRENT_EXPERT, evaluationStep, projectRule, supplierList);
	}
	
	/**
	 * @Description:
	 * @author chenxw
	 */
	public void error() {
		save_result_btn.setEnabled(false);
		submit_result_btn.setEnabled(false);
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.NOT_START, tenderProject.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
	}
	
	/**
	 * @Description: 刷新供应商列表
	 * @author chenxw
	 */
	private void refreshSupplierList() {
		List<SupplierOpeningResult> resultList = supplierOpeningResultService.getSupplierOpeningResultListByTenderIdAndBidOpeningStatus(tenderProject.getId(), SupplierEnum.OPENBID_SUCCESS);
		Map<String, SupplierOpeningResult> resultMap = CollectionUtils.toHashMap(resultList, new CollectionUtils.NewMap<String, SupplierOpeningResult>() {
			public String getKey(SupplierOpeningResult t) {
				return t.getSupplierId();
			}
		});
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProject.getId(), evaluationStep);
		if (null == supplierList) {
			supplierList = new ArrayList<Supplier>();
		}
		for (Supplier supplier : supplierList) {
			SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
			if (null == supplierOpeningResult) {
				supplier.setSupplierOpeningResult(resultMap.get(supplier.getId()));
			}
		}
	}
	
	/**
	 * @Description: 查看工程量清单
	 * @author chenxw
	 */
	private void viewBoqAction() {
		ViewBoqDialog dialog = new ViewBoqDialog(getShell(), tenderProject, null, evaluationStep);
		dialog.open();
	}
	
	/**
	 * @Description: 保存评审结果
	 * @author chenxw
	 */
	private void saveEvaluationResultAction() {
		if (!save_result_btn.getEnabled()) {
			return;
		}
		if (tableComposite.saveEvaluationResult()) {
			MessageDialog.openShortMessage("暂存成功！");
		} else {
			MessageDialog.openInformation(getShell(), "评标结果暂存失败！");
		}
	}
	
	/**
	 * @Description: 提交评审结果
	 * @author chenxw
	 */
	private void submitEvaluationResultAction() {
		if (!submit_result_btn.getEnabled()) {
			return;
		}
		String message = "        完成评审后将不能再对当前评标结果进行修改，确认完成[" + evaluationStep.getStepName() + "]的评审操作吗？";
		if (!MessageDialog.openConfirm(getShell(), "确认是否完成评审", message, new String[] { "确认完成评审", "继续评审" })) {
			return;
		}
		if (!tableComposite.saveEvaluationResult()) {
			MessageDialog.openInformation(getShell(), "暂存结果失败！");
			return;
		}
		if (!tableComposite.submitEvaluationResult()) {
			MessageDialog.openInformation(getShell(), "完成评审失败！");
			return;
		}
		goBackAction();
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	protected void checkSubclass() {}
	
}
