package com.gx.obe.management.composite;

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ELink;
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.common.icons.IconsActivator;
import com.gx.obe.common.shell.RandomRateShell;
import com.gx.obe.common.widget.composite.UnderlineText;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 合理平均价为评分基准价的方法
 * @author chenxw
 */
public class RationalAveragePriceComposite extends BasePriceManager {
	
	private UnderlineText limitPriceText;
	private Label priceUintLabel;
	private UnderlineText extractPercentageC1Text;
	private UnderlineText extractPercentageC2Text;
	private ELink extractRatioC1Link;
	private ELink extractRatioC2Link;
	private UnderlineText weightPercentageQ1Text;
	private UnderlineText weightPercentageQ2Text;
	
	public RationalAveragePriceComposite(Composite parent, int style) {
		super(parent, style);
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("评分基准价=A×C1×Q1+B×C2×Q2。");
		
		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setText("A：部分有效投标总价的算术平均值（保留两位小数）。");
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		composite_2.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(composite_2, SWT.NONE);
		label_2.setText("    有效投标家数＜20家时，全部参与算数平均值A的计算；");
		
		Composite composite_3 = new Composite(this, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 3;
		composite_3.setLayout(gl_composite_3);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_3 = new Label(composite_3, SWT.WRAP);
		label_3.setText("    有效投标家数≥20家时，去掉20%最高报价和20%最低报价（取整，不四舍五入），计算算术平均值A。");
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_4 = new Composite(this, SWT.NONE);
		composite_4.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_4 = new Label(composite_4, SWT.NONE);
		label_4.setText("B：");
		
		limitPriceText = new UnderlineText(composite_4, SWT.CENTER);
		limitPriceText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		limitPriceText.setLayoutData(new RowData(120, SWT.DEFAULT));
		limitPriceText.setCheckBigDecimal(false);
		
		priceUintLabel = new Label(composite_4, SWT.NONE);
		
		Label label_15 = new Label(composite_4, SWT.NONE);
		label_15.setText("（最高限价）。");
		
		Composite composite_5 = new Composite(this, SWT.NONE);
		composite_5.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_5 = new Label(composite_5, SWT.NONE);
		label_5.setText("C：抽取的系数。");
		
		Composite composite_11 = new Composite(this, SWT.NONE);
		composite_11.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_11 = new GridLayout(2, false);
		gl_composite_11.marginHeight = 0;
		gl_composite_11.marginWidth = 0;
		composite_11.setLayout(gl_composite_11);
		
		Composite composite_7 = new Composite(composite_11, SWT.NONE);
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		composite_7.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Label label_7 = new Label(composite_7, SWT.NONE);
		label_7.setText("\tC1：");
		
		extractPercentageC1Text = new UnderlineText(composite_7, SWT.CENTER);
		extractPercentageC1Text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		extractPercentageC1Text.setLayoutData(new RowData(40, SWT.DEFAULT));
		extractPercentageC1Text.setCheckIntValue(false);
		extractPercentageC1Text.setEnabled(false);
		
		Label label_11 = new Label(composite_7, SWT.NONE);
		label_11.setText("%");
		
		extractRatioC1Link = new ELink(composite_11, SWT.NONE);
		extractRatioC1Link.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				extractRatioC1Action();
			}
		});
		extractRatioC1Link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/switch.png"));
		extractRatioC1Link.setText("随机抽取系数");
		
		Composite composite_12 = new Composite(this, SWT.NONE);
		composite_12.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_12 = new GridLayout(2, false);
		gl_composite_12.marginHeight = 0;
		gl_composite_12.marginWidth = 0;
		composite_12.setLayout(gl_composite_12);
		
		Composite composite_8 = new Composite(composite_12, SWT.NONE);
		composite_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		composite_8.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Label label_8 = new Label(composite_8, SWT.NONE);
		label_8.setText("\tC2：");
		
		extractPercentageC2Text = new UnderlineText(composite_8, SWT.CENTER);
		extractPercentageC2Text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		extractPercentageC2Text.setLayoutData(new RowData(40, SWT.DEFAULT));
		extractPercentageC2Text.setCheckIntValue(false);
		extractPercentageC2Text.setEnabled(false);
		
		Label label_12 = new Label(composite_8, SWT.NONE);
		label_12.setText("%");
		
		extractRatioC2Link = new ELink(composite_12, SWT.NONE);
		extractRatioC2Link.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				extractRatioC2Action();
			}
		});
		extractRatioC2Link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/switch.png"));
		extractRatioC2Link.setText("随机抽取系数");
		
		Composite composite_6 = new Composite(this, SWT.NONE);
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_6.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Label label_6 = new Label(composite_6, SWT.NONE);
		label_6.setText("Q：权重比例Q1+Q2=100%； Q1、Q2取值均应≥30%。");
		
		Composite composite_9 = new Composite(this, SWT.NONE);
		composite_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_9.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Label label_9 = new Label(composite_9, SWT.NONE);
		label_9.setText("\tQ1：");
		
		weightPercentageQ1Text = new UnderlineText(composite_9, SWT.CENTER);
		weightPercentageQ1Text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		weightPercentageQ1Text.setLayoutData(new RowData(40, SWT.DEFAULT));
//		weightPercentageQ1Text.setTextLimit(2);
		weightPercentageQ1Text.setCheckBigDecimal(false);
		
		Label label_13 = new Label(composite_9, SWT.NONE);
		label_13.setText("%");
		
		Composite composite_10 = new Composite(this, SWT.NONE);
		composite_10.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_10.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Label label_10 = new Label(composite_10, SWT.NONE);
		label_10.setText("\tQ2：");
		
		weightPercentageQ2Text = new UnderlineText(composite_10, SWT.CENTER);
		weightPercentageQ2Text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		weightPercentageQ2Text.setLayoutData(new RowData(40, SWT.DEFAULT));
//		weightPercentageQ2Text.setTextLimit(2);
		weightPercentageQ2Text.setCheckBigDecimal(false);
		
		Label label_14 = new Label(composite_10, SWT.NONE);
		label_14.setText("%");
		
	}
	
	private void extractRatioC1Action() {
		if (!extractRatioC1Link.isEnabled()) {
			return;
		}
		RandomRateShell randomRateShell = new RandomRateShell(getShell(), 95, 99, new CallBack() {
			public void callBackObject(Object obj) {
				extractPercentageC1Text.setIntValue(ObjectUtils.getIntValue(obj, Integer.MAX_VALUE));
				extractRatioC1Link.setEnabled(false);
			}
		});
		SwtUtils.setCenter(randomRateShell);
		randomRateShell.open();
	}
	
	private void extractRatioC2Action() {
		if (!extractRatioC2Link.isEnabled()) {
			return;
		}
		RandomRateShell randomRateShell = new RandomRateShell(getShell(), Integer.MAX_VALUE, Integer.MAX_VALUE, new CallBack() {
			public void callBackObject(Object obj) {
				extractPercentageC2Text.setIntValue(ObjectUtils.getIntValue(obj, Integer.MAX_VALUE));
				extractRatioC2Link.setEnabled(false);
			}
		});
		SwtUtils.setCenter(randomRateShell);
		randomRateShell.open();
	}
	
	protected void refresh() {
		limitPriceText.setText(MoneyUtils.getMoneyFormatByUnit(tenderProject.getBidPriceLimit(), projectRule.getEvalPriceUnit()));
		priceUintLabel.setText(projectRule.getEvalPriceUnit());
		priceUintLabel.getParent().layout();
		extractPercentageC1Text.setIntValue(basePriceCalculation.getExtractPercentageC1());
		extractPercentageC2Text.setIntValue(basePriceCalculation.getExtractPercentageC2());
		extractRatioC1Link.setEnabled(extractPercentageC1Text.getText().isEmpty());
		extractRatioC2Link.setEnabled(extractPercentageC2Text.getText().isEmpty());
		weightPercentageQ1Text.setBigDecimalValue(basePriceCalculation.getWeightPercentageQ1());
		weightPercentageQ2Text.setBigDecimalValue(basePriceCalculation.getWeightPercentageQ2());
	}
	
	public boolean save() {
		if (!limitPriceText.checkBigDecimalIsNull("最高限价不能为空！")) {
			return false;
		}
		if (!extractPercentageC1Text.checkIntIsNull("抽取系数C1不能为空！")) {
			return false;
		}
		if (!extractPercentageC1Text.checkIntIsInRange(0, 100, "抽取系数C1不得小于0%或大于100%！")) {
			return false;
		}
		if (!extractPercentageC2Text.checkIntIsNull("抽取系数C2不能为空！")) {
			return false;
		}
		if (!extractPercentageC2Text.checkIntIsInRange(0, 100, "抽取系数C1不得小于0%或大于100%！")) {
			return false;
		}
		if (!weightPercentageQ1Text.checkBigDecimalIsNull("权重比例Q1不能为空！")) {
			return false;
		}
		if (!weightPercentageQ1Text.checkBigDecimalIsInRange(BigDecimal.valueOf(30), BigDecimal.valueOf(100), "权重比例Q1不得小于30%或大于100%！")) {
			return false;
		}
		if (!weightPercentageQ2Text.checkBigDecimalIsNull("权重比例Q2不能为空！")) {
			return false;
		}
		if (!weightPercentageQ2Text.checkBigDecimalIsInRange(BigDecimal.valueOf(30), BigDecimal.valueOf(100), "权重比例Q2不得小于30%或大于100%！")) {
			return false;
		}
		BigDecimal weightPercentage = BigDecimalUtils.add(weightPercentageQ1Text.getBigDecimalValue(), weightPercentageQ2Text.getBigDecimalValue());
		if (weightPercentage.compareTo(BigDecimal.valueOf(100)) != 0) {
			MessageDialog.openWarning(getShell(), "权重比例Q1+Q2不等于100%！");
			weightPercentageQ1Text.setFocus();
			return false;
		}
		tenderProject.setBidPriceLimit(MoneyUtils.getMoneyFromText(limitPriceText.getText().trim(), projectRule.getEvalPriceUnit()));
		basePriceCalculation.setExtractPercentageC1(extractPercentageC1Text.getIntValue());
		basePriceCalculation.setExtractPercentageC2(extractPercentageC2Text.getIntValue());
		basePriceCalculation.setWeightPercentageQ1(weightPercentageQ1Text.getBigDecimalValue());
		basePriceCalculation.setWeightPercentageQ2(weightPercentageQ2Text.getBigDecimalValue());
		return true;
	}
	
	public String getFormulaContent() {
		StringBuilder formulaContentBuilder = new StringBuilder();
		formulaContentBuilder.append("评分基准价=A×C1×Q1+B×C2×Q2。").append("\r\n");
		formulaContentBuilder.append("A：部分有效投标总价的算术平均值（保留两位小数）。").append("\r\n");
		formulaContentBuilder.append("\t有效投标家数＜20家时，全部参与算数平均值A的计算；").append("\r\n");
		formulaContentBuilder.append("\t有效投标家数≥20家时，去掉20%最高报价和20%最低报价（取整，不四舍五入），计算算术平均值A。").append("\r\n");
		formulaContentBuilder.append("B：").append(MoneyUtils.getMoneyWidthUnit(tenderProject.getBidPriceLimit(), projectRule.getEvalPriceUnit())).append("（最高限价）。").append("\r\n");
		formulaContentBuilder.append("C：抽取的系数。").append("\r\n");
		formulaContentBuilder.append("\tC1：").append(extractPercentageC1Text.getIntValue()).append("%");
		formulaContentBuilder.append("\tC2：").append(extractPercentageC2Text.getIntValue()).append("%").append("\r\n");;
		formulaContentBuilder.append("Q：权重比例Q1+Q2=100%； Q1、Q2取值均应≥30%。").append("\r\n");
		formulaContentBuilder.append("\tQ1：").append(weightPercentageQ1Text.getBigDecimalValue()).append("%");
		formulaContentBuilder.append("\tQ2：").append(weightPercentageQ2Text.getBigDecimalValue()).append("%");
		return formulaContentBuilder.toString();
	}
	
}
