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.ECombo;
import org.eclipse.swt.core.widgets.ECombo.ComboSelectoinListener;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.management.Lisener.EvaluationPriceModifyListener;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceResultService;
import com.swtdesigner.SWTResourceManager;

public class AddOrSubItemEvaluationPriceComposite extends Composite implements EvaluationPriceModifyListener{
	private Text value_text;
	private Label EvaluationPrice_label;
	private ECombo combo;
	private String addSymbol = "加";
	private String subSymbol = "减";
	private TenderProjectRuleEntity TenderProjectRuleEntity;
	private BidPriceResult bidPriceResult;
	private BidPriceResultService bidPriceResultService;
	private Label priceUintCN_label;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AddOrSubItemEvaluationPriceComposite(Composite parent, int style, TenderProjectRuleEntity TenderProjectRuleEntity, BidPriceResult bidPriceResult) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setLayout(new GridLayout(1, false));
		this.TenderProjectRuleEntity = TenderProjectRuleEntity;
		this.bidPriceResult = bidPriceResult;

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		GridData gd_composite = new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1);
		gd_composite.heightHint = 180;
		gd_composite.widthHint = 420;
		composite.setLayoutData(gd_composite);
		
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		label_3.setText("方式：");
		
		combo = new ECombo(composite, SWT.READ_ONLY | SWT.SHADOW_NONE);
		combo.setAlignment(SWT.CENTER);
		combo.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(composite, SWT.NONE);
		combo.addSelectionListener(new ComboSelectoinListener() {
			public void itemSelectoin(String arg0, Object arg1, int arg2) {
				calculationAction();
			}
		});
		
		Label label = new Label(composite, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("金额：");
		
		value_text = new Text(composite, SWT.BORDER);
		value_text.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text.heightHint = 30;
		value_text.setLayoutData(gd_text);
		
		new Label(composite, SWT.NONE);
		
		value_text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				modifyFactorScoreAction(this);
			}
		});
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		label_1.setText("评标价：");
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.horizontalSpacing = 0;
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_composite_1.heightHint = 40;
		composite_1.setLayoutData(gd_composite_1);
		
		EvaluationPrice_label = new Label(composite_1, SWT.NONE);
		EvaluationPrice_label.setAlignment(SWT.CENTER);
		EvaluationPrice_label.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		EvaluationPrice_label.setText("23123123");
		EvaluationPrice_label.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, true, 1, 1));
		
		ESepator sep = new ESepator(composite_1, SWT.NONE);
		
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		priceUintCN_label = new Label(composite, SWT.NONE);
		priceUintCN_label.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		priceUintCN_label.setText("万元");
		init();

	}
	/**
	 * @Description: 界面初始化
	 * @author guoyr 
	 */
	private void init(){
		bidPriceResultService = new BidPriceResultService();
		combo.addItem(addSymbol, 0);
		combo.addItem(subSymbol, 1);
		combo.select(0);
		EvaluationPrice_label.setText(MoneyUtils.getMoneyFormatByUnit(ObjectUtils.getObjBigDecimal(bidPriceResult.getEvaluationPrice()), bidPriceResult.getPriceUnit(TenderProjectRuleEntity.getEvalPriceUnit())));
		priceUintCN_label.setText(TenderProjectRuleEntity.getPriceUnitUseDefault());
	}
	

	/**
	 * @Description: 修改分值
	 * @author guoyr
	 */
	private void modifyFactorScoreAction(ModifyListener listener){
		BigDecimal evaluationPrice = ObjectUtils.getObjBigDecimal(value_text.getText().trim());
		if(null != evaluationPrice){
			calculationAction();
		}else {
			value_text.removeModifyListener(listener);
			value_text.setText("");
			value_text.addModifyListener(listener);
			EvaluationPrice_label.setText(MoneyUtils.getMoneyFormatByUnit(ObjectUtils.getObjBigDecimal(bidPriceResult.getEvaluationPrice()), bidPriceResult.getPriceUnit(TenderProjectRuleEntity.getEvalPriceUnit())));
		}
		
	}
	
	
	/**
	 * @Description: 计算事件
	 * @author guoyr 
	 */
	private void calculationAction(){
		String value = value_text.getText().trim();
		if(!"".equals(value)){
			try {
				BigDecimal textValue = new BigDecimal(value);
				textValue = calculationPrice(textValue);
				EvaluationPrice_label.setText(MoneyUtils.getMoneyFormatByUnit(ObjectUtils.getObjBigDecimal(textValue), bidPriceResult.getPriceUnit(TenderProjectRuleEntity.getEvalPriceUnit())));
				
			} catch (Exception e) {
				MessageDialog.openError(getShell(), "输入数据非法，请重新输入！");
			}
			
		}
	}
	
	
	/**
	 * @Description: 通过折扣率计算评标价
	 * @author guoyr
	 * @param textValue
	 * @return 
	 */
	private BigDecimal calculationPrice(BigDecimal textValue){
		if(addSymbol.equals(combo.getText())){
			textValue = bidPriceResult.getBidPrice().add(textValue);
		}else{
			textValue = bidPriceResult.getBidPrice().subtract(textValue);
		}
		return textValue;
	}
	@Override
	protected void checkSubclass() {
	}
	/** 
	 * @Description: 保存修改事件
	 * @author guoyr
	 * @return 
	 */
	public boolean saveEvaluactionPrice() {
		String value = value_text.getText().trim();
		if(!"".equals(value)){
			try {
				BigDecimal textValue = new BigDecimal(value);
				bidPriceResult.setEvaluationPrice(calculationPrice(textValue));
				bidPriceResultService.updateAssignProperty(bidPriceResult, new String[]{"id","evaluationPrice"});
				return true;
			} catch (Exception e) {
				MessageDialog.openError(getShell(), "输入数据非法，请重新输入！");
			}
			
		}
		return false;
	}

}
