package com.gx.obe.management.composite;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
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.util.utils.ObjectUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 随机平均价为评分基准价的方法
 * @author chenxw
 */
public class RandomAveragePriceComposite extends BasePriceManager {
	
	private UnderlineText extractPercentageCText;
	private ELink extractRatioLink;
	
	public RandomAveragePriceComposite(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×C。");
		
		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(composite_1, SWT.WRAP);
		label_1.setText("A：部分有效投标总价的算术平均值（保留两位小数）。");
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		composite_2.setLayout(new GridLayout(1, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(composite_2, SWT.WRAP);
		label_2.setText("  有效投标家数＜20家时，全部参与算数平均值A的计算；");
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_3 = new Composite(this, SWT.NONE);
		composite_3.setLayout(new GridLayout(1, false));
		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.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(2, false);
		gl_composite_4.marginHeight = 0;
		gl_composite_4.marginWidth = 0;
		composite_4.setLayout(gl_composite_4);
		
		Composite composite_5 = new Composite(composite_4, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		RowLayout rl_composite_5 = new RowLayout(SWT.HORIZONTAL);
		composite_5.setLayout(rl_composite_5);
		
		Label label_5 = new Label(composite_5, SWT.NONE);
		label_5.setText("C：");
		
		extractPercentageCText = new UnderlineText(composite_5, SWT.CENTER);
		extractPercentageCText.setLayoutData(new RowData(40, SWT.DEFAULT));
		extractPercentageCText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		extractPercentageCText.setCheckIntValue(false);
		extractPercentageCText.setEnabled(false);
		
		Label label_4 = new Label(composite_5, SWT.NONE);
		label_4.setText("%");
		
		extractRatioLink = new ELink(composite_4, SWT.NONE);
		extractRatioLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				extractRatioAction();
			}
		});
		extractRatioLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/switch.png"));
		extractRatioLink.setText("随机抽取系数");
	}
	
	/**
	 * @Description: 抽取系数事件
	 * @author chenxw
	 */
	private void extractRatioAction() {
		if (!extractRatioLink.isEnabled()) {
			return;
		}
		RandomRateShell randomRateShell = new RandomRateShell(getShell(), 95, 99, new CallBack() {
			public void callBackObject(Object obj) {
				extractPercentageCText.setIntValue(ObjectUtils.getIntValue(obj, Integer.MAX_VALUE));
				extractRatioLink.setEnabled(false);
			}
		});
		SwtUtils.setCenter(randomRateShell);
		randomRateShell.open();
	}
	
	protected void refresh() {
		extractPercentageCText.setIntValue(basePriceCalculation.getExtractPercentageC());
		extractRatioLink.setEnabled(extractPercentageCText.getText().isEmpty());
	}
	
	public boolean save() {
		if (!extractPercentageCText.checkIntIsNull("抽取系数C不能为空！")) {
			return false;
		}
		if (!extractPercentageCText.checkIntIsInRange(95, 99, "抽取系数C不得小于95%或大于99%！")) {
			return false;
		}
		basePriceCalculation.setExtractPercentageC(extractPercentageCText.getIntValue());
		return true;
	}
	
	/**
	 * @Description:
	 * @author chenxw
	 * @return
	 */
	public String getFormulaContent() {
		StringBuilder formulaContentBuilder = new StringBuilder();
		formulaContentBuilder.append("评分基准价=A×C。").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("C：").append(extractPercentageCText.getIntValue()).append("%");
		return formulaContentBuilder.toString();
	}
	
}
