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

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.custom.ScrolledComposite;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.gx.obe.action.bean.Action;
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.CreditEvaluationTableComposite;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.formula.PriceParam;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
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.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.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
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 CreditEvaluationComposite extends Composite {

	private Composite left_tool_composite;
	private TenderProjectEntity tenderProjectEntity;
	private Expert CURRENT_EXPERT;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private EButton submit_result_btn;
	private SupplierService supplierService = new SupplierService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ExpertEvaluationStepService expertEvaluationStepService ;;
	private ExpertService expertService;
	private ExpertEvaluationListener expertEvaluationListener;
	private Composite evaluation_table_composite;
	// private List<Supplier> supplierList;
	private Composite right_composite;
	private Composite evaluationComposite;
	// 获得项目的价格公式参数
	private PriceParam priceParam = null;
	private EButton save_result_btn;
	private Object hideLeft;
	private ScrolledComposite scrolledComposite;
	private CreditEvaluationTableComposite evaluationTable;
	private EvaluationStep noComplateFilterStep = null;

	{
		LogUtils.logClass(CreditEvaluationComposite.class);
	}

	/**
	 * @param parent
	 * @param style
	 */
	public CreditEvaluationComposite(Composite parent,Action action, 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);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
			}
		});
		this.tenderProjectEntity = action.getTenderProject();
		this.tenderProjectRuleEntity = action.getTenderProjectRule();
		this.evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		hideLeft = parent.getData("hideLeft");

		left_tool_composite = new Composite(this, SWT.NONE);
		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));
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		left_tool_composite.setLayout(gl_left_tool_composite);

		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.SELECTED);
		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(Messages.Button_return);
		goto_back.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});

		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.SELECTED);
		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(Messages.Button_refresh);

		refresh_evaluation.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshTenderInfo();
			}
		});

		right_composite = new Composite(this, SWT.NONE);
		GridLayout gl_right_composite = new GridLayout(1, false);
		gl_right_composite.marginHeight = 0;
		gl_right_composite.marginWidth = 0;
		right_composite.setLayout(gl_right_composite);
		right_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		evaluationComposite = new Composite(right_composite, SWT.NONE);
		evaluationComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		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);

		scrolledComposite = new ScrolledComposite(evaluationComposite, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		evaluation_table_composite = new Composite(scrolledComposite, SWT.NONE);
		evaluation_table_composite.setBackground(SWTResourceManager .getColor(SWT.COLOR_WHITE));
		GridLayout gl_price_evaluation_table = new GridLayout(1, false);
		gl_price_evaluation_table.marginHeight = 0;
		gl_price_evaluation_table.marginWidth = 0;
		evaluation_table_composite.setLayout(gl_price_evaluation_table);
		
		evaluationTable = new CreditEvaluationTableComposite(evaluation_table_composite, SWT.NONE);
		evaluationTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		scrolledComposite.setContent(evaluation_table_composite);
		scrolledComposite.setMinSize(evaluation_table_composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		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.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		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.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationResultAction();
			}
		});
		save_result_btn.setText(Messages.Button_saveEvaluationResult);

		submit_result_btn = new EButton(composite_5, SWT.NONE | EButton.CSS_GREEN);
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		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.setText(Messages.Button_submitEvaluationResult);
		submit_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});

		init();
	}

	/**
	 * @Description: 初始化界面数据
	 * @author chenxw
	 */
	private void init() {
		expertEvaluationStepService = new ExpertEvaluationStepService();
		expertService = new ExpertService();
		if (null != hideLeft) {
			left_tool_composite.dispose();
		}
		CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		
		// 修改状态为正在进行中
		ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
		refreshTenderInfo();
		scrolledComposite.getVerticalBar().setIncrement(20);
		scrolledComposite.forceFocus();
		scrolledComposite.addListener(SWT.Activate, new Listener() {
			public void handleEvent(Event e) {
				scrolledComposite.setFocus();
			}
		});
	}

	/**
	 * @Description: 返回
	 * @author chenxw
	 */
	private void goBackAction() {
		if (null != expertEvaluationListener) {
			expertEvaluationListener.complete();
		}
	}

	/**
	 * @Description: 刷新项目信息
	 * @author chenxw
	 */
	private void refreshTenderInfo() {
		getNoComplateFilterStep();
		showEvalPriceComposite();
	}

	/**
	 * @Description: 显示评审界面
	 * @author chenxw
	 */
	private void showEvalPriceComposite() {
		drawEvaluationTable();
		right_composite.layout();
	}

	/**
	 * @Description: 实现评审表格
	 * @author chenxw
	 */
	private void drawEvaluationTable() {
		// 获取投标人评标结果
		evaluationTable.refresh(tenderProjectEntity, tenderProjectRuleEntity, evaluationStep, CURRENT_EXPERT, getSupplierList());
		evaluation_table_composite.layout();
		scrolledComposite.setMinSize(evaluation_table_composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}

	/**
	 * @Description: 刷新投标人的价格分
	 * @author chenxw
	 * @return
	 */
	private List<Supplier> getSupplierList() {
		return supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
	}

	/**
	 * @Description: 获得价格分的四舍五入
	 * @author chenxw
	 * @param priceScore
	 * @return
	 */
	public BigDecimal getPriceScoreRound(BigDecimal priceScore) {
		return BigDecimalUtils.round(priceScore, priceParam.getPriceScorePoint());
	}

	/**
	 * @Description: 保存评标结果
	 * @author chenxw
	 */
	private void saveEvaluationResultAction() {
		if (save_result_btn.getEnabled()) {
			boolean saveSuccess = evaluationTable.saveVetoBidEvaluationResult();
			if (saveSuccess) {
				MessageDialog.openShortMessage(Messages.Tips_saveEvalResultSuccess);
			} else {
				MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
			}
		}
	}

	/**
	 * @Description: 评审完成
	 * @author chenxw
	 */
	private void submitEvaluationResultAction() {
		if (submit_result_btn.getEnabled()) {
			if (evaluationTable.saveVetoBidEvaluationResult()) {
				// 检查是否存在当前评标步骤之前未完成的步骤
				boolean isFreeProgress = ObjectUtils.equals(tenderProjectRuleEntity.getOrderReview(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.ORDER_SELF_FREE);
				if (isFreeProgress) {
					List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateStepList(tenderProjectEntity.getId(), evaluationStep.getSortNo());
					if (null != evaluationStepList && evaluationStepList.size() > 0) {
						MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_saveAllComplate, evaluationStepList.get(0).getStepName()));
						return ;
					}
				}
				if(null != noComplateFilterStep){
					if (MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_preStepNotCompletedBeforeEntry, noComplateFilterStep.getStepName()), new String[]{Messages.Button_refresh, Messages.Button_cancel})) {
						refreshTenderInfo();
					}
					return;
				}
				String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, evaluationStep.getStepName());
				if (!MessageDialog.openConfirm(getShell(), Messages.Button_submitEvaluationResult, message, new String[] { Messages.Button_confirmComplate, Messages.Button_continueEvaluation})) {
					return;
				}
				if (evaluationTable.submitCreditEvaluationResult()) {
					goBackAction();
				} else {
					MessageDialog.openInformation(getShell(), "评标结果完成失败！");
				}
			} else {
				MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
			}
		}
	}
	
	/**
	 * @Description: 获得未完成的可能会存在废标的评审节点
	 * @author guoyr 
	 */
	private void getNoComplateFilterStep(){
		List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateFilterStepList(tenderProjectEntity.getId(), evaluationStep.getSortNo());
		if (null != evaluationStepList && evaluationStepList.size() > 0) {
			noComplateFilterStep =  evaluationStepList.get(0);
		}else {
			noComplateFilterStep = null;
		}
	}

	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}

	protected void checkSubclass() {

	}
}
