package com.gx.obe.management.composite;

import java.math.BigDecimal;

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.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.widget.composite.UnderlineText;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.evaluation.step.entity.BusinessParam;
import com.gx.obe.message.Messages;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 商务指标
 * @author chenxw
 */
public class BusinessFactorManagerComposite extends Composite {
	
	private final TenderProjectService tenderProjectService = new TenderProjectService();
	private final EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	
	private ScrolledComposite scrolledComposite;
	private Composite factorComposite;
	private UnderlineText weightText;
	private UnderlineText highBasePricePercentageText;
	private UnderlineText highBasePriceDeductPointsText;
	private UnderlineText lowBasePricePercentageText;
	private UnderlineText lowBasePriceDeductPointsText;
	private Composite basePriceComposite;
	
	private TenderProjectEntity tenderProject;
	private String evalMethod;
	
	private EvaluationFactor businessFactor;
	private BusinessParam businessParam;
	private BasePriceManager basePriceManager;
	
	private TenderProjectRuleEntity projectRule;
	
	/**
	 * @author chenxw
	 * @param parent
	 * @param style
	 */
	public BusinessFactorManagerComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		
		scrolledComposite = new ScrolledComposite(this, SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		factorComposite = new Composite(scrolledComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginHeight = 1;
		gl_composite.marginWidth = 1;
		gl_composite.horizontalSpacing = 1;
		gl_composite.verticalSpacing = 1;
		factorComposite.setLayout(gl_composite);
		factorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		factorComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Composite composite_1 = new Composite(factorComposite, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_1.setLayout(new GridLayout(1, false));
		
		Label label_10 = new Label(composite_1, SWT.NONE);
		label_10.setText("得分计算设置");
		
		Composite composite_6 = new Composite(factorComposite, SWT.NONE);
		composite_6.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_6.setLayout(new GridLayout(1, false));
		
		Label label_9 = new Label(composite_6, SWT.NONE);
		label_9.setText("基准价设置");
		
		Composite composite_3 = new Composite(factorComposite, SWT.NONE);
		composite_3.setLayout(new GridLayout(1, false));
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.widthHint = 200;
		composite_3.setLayoutData(gd_composite_3);
		
		Composite composite_4 = new Composite(composite_3, SWT.NONE);
		composite_4.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		Label label = new Label(composite_4, SWT.NONE);
		label.setText("商务部分的分值权重占总得分的权重为");
		
		weightText = new UnderlineText(composite_4, SWT.CENTER);
		weightText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		weightText.setLayoutData(new RowData(50, SWT.DEFAULT));
//		weightText.setTextLimit(2);
		weightText.setCheckBigDecimal(false);
		
		Label label_1 = new Label(composite_4, SWT.NONE);
		label_1.setText("%。");
		
		Composite composite_8 = new Composite(composite_3, SWT.NONE);
		composite_8.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(composite_8, SWT.NONE);
		label_2.setText("当投标总报价等于评分基准价时得标准分。");
		
		Composite composite_5 = new Composite(composite_3, SWT.NONE);
		composite_5.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_8 = new Label(composite_5, SWT.NONE);
//		label_8.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_8.setText("当投标总报价每高出评分基准价");
		
		highBasePricePercentageText = new UnderlineText(composite_5, SWT.CENTER);
		highBasePricePercentageText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		highBasePricePercentageText.setLayoutData(new RowData(40, SWT.DEFAULT));
//		highBasePricePercentageText.setTextLimit(2);
		highBasePricePercentageText.setDefaultBigDecimalIsZERO(BigDecimal.ONE, false);
		
		Label label_3 = new Label(composite_5, SWT.NONE);
		label_3.setText("%，减该项标准分");
		
		highBasePriceDeductPointsText = new UnderlineText(composite_5, SWT.CENTER);
		highBasePriceDeductPointsText.setLayoutData(new RowData(40, SWT.DEFAULT));
//		highBasePriceDeductPointsText.setTextLimit(3);
		highBasePriceDeductPointsText.setCheckBigDecimal(false);
		highBasePriceDeductPointsText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		Label label_4 = new Label(composite_5, SWT.NONE);
		label_4.setText("分，减完为止；");
		
		Composite composite_7 = new Composite(composite_3, SWT.NONE);
		composite_7.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_7 = new Label(composite_7, SWT.NONE);
		label_7.setText("当投标总报价每低于评分基准价");
		
		lowBasePricePercentageText = new UnderlineText(composite_7, SWT.CENTER);
		lowBasePricePercentageText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		lowBasePricePercentageText.setLayoutData(new RowData(40, SWT.DEFAULT));
//		lowBasePricePercentageText.setTextLimit(2);
		lowBasePricePercentageText.setDefaultBigDecimalIsZERO(BigDecimal.ONE, false);
		
		Label label_5 = new Label(composite_7, SWT.NONE);
		label_5.setText("%，减该项标准分");
		
		lowBasePriceDeductPointsText = new UnderlineText(composite_7, SWT.CENTER);
		lowBasePriceDeductPointsText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		lowBasePriceDeductPointsText.setLayoutData(new RowData(40, SWT.DEFAULT));
//		lowBasePriceDeductPointsText.setTextLimit(3);
		lowBasePriceDeductPointsText.setCheckBigDecimal(false);
		
		Label label_6 = new Label(composite_7, SWT.NONE);
		label_6.setText("分，减完为止。");
		
		basePriceComposite = new Composite(factorComposite, SWT.NONE);
		basePriceComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_basePriceComposite = new GridLayout(1, false);
		gl_basePriceComposite.marginHeight = 2;
		gl_basePriceComposite.marginWidth = 2;
		basePriceComposite.setLayout(gl_basePriceComposite);
		GridData gd_basePriceComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_basePriceComposite.widthHint = 200;
		basePriceComposite.setLayoutData(gd_basePriceComposite);
		
		scrolledComposite.setContent(factorComposite);
		scrolledComposite.setMinSize(factorComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_composite_2.widthHint = 400;
		composite_2.setLayoutData(gd_composite_2);
		composite_2.setLayout(new GridLayout(1, false));
		
		EButton save_btn = new EButton(composite_2, SWT.NONE);
		save_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationFactorAction();
			}
		});
		GridData gd_save_btn = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_save_btn.heightHint = 35;
		gd_save_btn.widthHint = 100;
		save_btn.setLayoutData(gd_save_btn);
		save_btn.setBackground(ColorConstants.SKIN_BG);
		save_btn.setForeground(ColorConstants.SKIN_FOREGROUND);
		save_btn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		save_btn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		save_btn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		save_btn.setArcWidth(6);
		save_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_savebutton_1 = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_savebutton_1.heightHint = 35;
		gd_savebutton_1.widthHint = 200;
		save_btn.setLayoutData(gd_savebutton_1);
		save_btn.setText("保 存");
		
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProject
	 * @param evalMethod
	 */
	public void refresh(TenderProjectEntity tenderProject, TenderProjectRuleEntity projectRule, String evalMethod) {
		this.tenderProject = tenderProject;
		this.projectRule = projectRule;
		this.evalMethod = evalMethod;
		
		this.businessFactor = getBusinessFactor();
		
		businessParam = BusinessParam.setSerialize(businessFactor.getComputerParams());
		if (null == businessParam) {
			businessParam = new BusinessParam();
		}
		
		refreshBusinessFactor();
		refreshBasePriceComposite();
	}
	
	/**
	 * @Description: 获取商务指标
	 * @author chenxw
	 * @return
	 */
	private EvaluationFactor getBusinessFactor() {
		EvaluationFactor businessFactor = evaluationFactorService.getBillFactoryByType(tenderProject.getId(), EvaluationFactorEnum.FACTOR_BILL);
		if (null == businessFactor) {
			businessFactor = new EvaluationFactor();
			businessFactor.setTenderId(tenderProject.getId());
			businessFactor.setFactorName("商务指标");
			businessFactor.setFactorCode("businessFactor");
			businessFactor.setFactorScore(BigDecimal.valueOf(100));
			businessFactor.setFactorMinScore(BigDecimal.ZERO);
			businessFactor.setFactorMaxScore(BigDecimal.valueOf(100));
			businessFactor.setAuditType(EvaluationFactorEnum.AUDIT_BILL);
			businessFactor.setFactorType(EvaluationFactorEnum.FACTOR_BILL);
		}
		return businessFactor;
	}
	
	/**
	 * @Description: 刷新商务指标
	 * @author chenxw
	 */
	private void refreshBusinessFactor() {
		weightText.setBigDecimalValue(businessFactor.getFactorWeight());
		BusinessParam.ScoreCalculation scoreCalculation = businessParam.getScoreCalculation();
		highBasePricePercentageText.setBigDecimalValue(scoreCalculation.getHighBasePricePercentage());
		highBasePriceDeductPointsText.setBigDecimalValue(scoreCalculation.getHighBasePriceDeductPoints());
		lowBasePricePercentageText.setBigDecimalValue(scoreCalculation.getLowBasePricePercentage());
		lowBasePriceDeductPointsText.setBigDecimalValue(scoreCalculation.getLowBasePriceDeductPoints());
	}
	
	/**
	 * @Description: 刷新基准价界面
	 * @author chenxw
	 */
	private void refreshBasePriceComposite() {
		basePriceManager = null;
		if (TenderProjectEnum.SUB_LOW_PRICE.equals(evalMethod)) {
			basePriceManager = new RationalSubLowPriceComposite(basePriceComposite, SWT.FILL);
		} else if (TenderProjectEnum.RANDOM_AVERAGE_PRICE.equals(evalMethod)) {
			basePriceManager = new RandomAveragePriceComposite(basePriceComposite, SWT.FILL);
		} else if (TenderProjectEnum.RATIONAL_AVERAGE_PRICE.equals(evalMethod)) {
			basePriceManager = new RationalAveragePriceComposite(basePriceComposite, SWT.FILL);
		}
		basePriceComposite.layout();
		if (null == basePriceManager) {
			return;
		}
		basePriceManager.refresh(tenderProject, projectRule, businessParam.getBasePriceCalculation());
		factorComposite.layout();
		scrolledComposite.setMinSize(factorComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 保存评标指标事件
	 * @author chenxw
	 */
	private void saveEvaluationFactorAction() {
		if (tenderProjectService.isInProgress(tenderProject.getId(), EvaluationStepEnum.BILL)) {
			MessageDialog.openInformation(getShell(), "已进入商务得分评审环节，不能进行此操作！");
			return;
		}
		if (!weightText.checkBigDecimalIsNull("商务部分的分值权重占总得分的权重不能为空！")) {
			return;
		}
		businessFactor.setFactorWeight(weightText.getBigDecimalValue());
		if (null == basePriceManager) {
			MessageDialog.openInformation(getShell(), "未获取到基准价设置，请检查评标办法是否正确！");
			return;
		}
		if (!basePriceManager.save()) {
			return;
		}
		if (!saveScoreCalculation()) {
			return;
		}
		StringBuilder formulaContentBuilder = new StringBuilder();
		formulaContentBuilder.append(basePriceManager.getFormulaContent());
		formulaContentBuilder.append("\r\n");
		formulaContentBuilder.append("当投标总报价等于评分基准价时得标准分。");
		formulaContentBuilder.append("\r\n");
		formulaContentBuilder.append("当投标总报价每高出评分基准价").append(highBasePricePercentageText.getBigDecimalValue()).append("%，");
		formulaContentBuilder.append("减该项标准分").append(highBasePriceDeductPointsText.getBigDecimalValue()).append("分，减完为止；");
		formulaContentBuilder.append("\r\n");
		formulaContentBuilder.append("当投标总报价每低于评分基准价").append(lowBasePricePercentageText.getBigDecimalValue()).append("%，");
		formulaContentBuilder.append("减该项标准分").append(lowBasePriceDeductPointsText.getBigDecimalValue()).append("分，减完为止；");
		businessParam.setFormulaContent(formulaContentBuilder.toString());
		
		businessFactor.setComputerParams(businessParam.getSerialize());
		if (evaluationFactorService.saveBusinessFactor(tenderProject, businessFactor)) {
			MessageDialog.openShortMessage(Messages.Tips_saveSuccess);
		} else {
			MessageDialog.openError(getShell(), Messages.Tips_saveFail);
		}
	}
	
	/**
	 * @Description: 保存得分计算参数
	 * @author chenxw
	 * @return
	 */
	private boolean saveScoreCalculation() {
		BusinessParam.ScoreCalculation scoreCalculation = businessParam.getScoreCalculation();
		if (!highBasePriceDeductPointsText.checkBigDecimalIsNull("高出评分基准价百分比时所扣分值不能为空！")) {
			return false;
		}
		if (!lowBasePriceDeductPointsText.checkBigDecimalIsNull("低于评分基准价百分比时所扣分值不能为空！")) {
			return false;
		}
		if (!highBasePriceDeductPointsText.checkBigDecimalIsInRange(BigDecimal.valueOf(0.5), BigDecimal.valueOf(2), "高出评分基准价百分比时所扣分值不得低于0.5分或高于2分！")) {
			return false;
		}
		if (!lowBasePriceDeductPointsText.checkBigDecimalIsInRange(BigDecimal.valueOf(0.5), BigDecimal.valueOf(2), "低于评分基准价百分比时所扣分值不得低于0.5分或高于2分！")) {
			return false;
		}
		scoreCalculation.setHighBasePricePercentage(highBasePricePercentageText.getBigDecimalValue());
		scoreCalculation.setLowBasePricePercentage(lowBasePricePercentageText.getBigDecimalValue());
		scoreCalculation.setHighBasePriceDeductPoints(highBasePriceDeductPointsText.getBigDecimalValue());
		scoreCalculation.setLowBasePriceDeductPoints(lowBasePriceDeductPointsText.getBigDecimalValue());
		return true;
	}
	
	protected void checkSubclass() {}
}
