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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.DecimalPlacesModifyListener;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
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.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.price.BidPriceHisResult;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class BidPriceHisResultComposite extends Composite {

	private CLabel bidPriceResultTitle_label;
	private Label bidPriceResultUnit_label;
	private Text bidPriceResult_text;
	private BidPriceHisResult bidPriceResult;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public BidPriceHisResultComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(3, false));
		
		bidPriceResultTitle_label = new CLabel(this, SWT.WRAP);
		bidPriceResultTitle_label.setAlignment(SWT.RIGHT);
		bidPriceResultTitle_label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		bidPriceResultTitle_label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		GridData gd_title = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_title.widthHint = 100;
		bidPriceResultTitle_label.setLayoutData(gd_title);
		
		bidPriceResult_text = new Text(this, SWT.BORDER);
		GridData gd_value = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_value.widthHint = 100;
		
		bidPriceResult_text.setLayoutData(gd_value);
		bidPriceResult_text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		
		bidPriceResult_text.setForeground(ColorConstants.FONT_BLACK_COLOR);
		bidPriceResult_text.addFocusListener(new FocusAdapter(){
			public void focusGained(FocusEvent e) {
				bidPriceResult_text.setForeground(ColorConstants.SKIN_BG);
			}
			public void focusLost(FocusEvent e) {
				bidPriceResult_text.setForeground(ColorConstants.FONT_BLACK_COLOR);
			}
		});
		
		bidPriceResultUnit_label = new Label(this, SWT.WRAP);
		bidPriceResultUnit_label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_unit = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_unit.widthHint = 30;
		bidPriceResultUnit_label.setLayoutData(gd_unit);
		bidPriceResultUnit_label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
	}

	public void setEditable(boolean editable){
		bidPriceResult_text.setEditable(editable);
	}
	
	public void refreshBidPriceHisResult(BidPriceHisResult result, String defaultUnit){
		this.bidPriceResult = result;
		bidPriceResultTitle_label.setText(ObjectUtils.getObjString(bidPriceResult.getBidPriceName())+ "：");
		if(BidPriceEnum.IS_PRICE_ITEM.equals(bidPriceResult.getBidPriceType())){
			if(!BidPriceEnum.IS_RATE.equals(bidPriceResult.getPriceType())){
				bidPriceResultTitle_label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/money-yen.png"));
			}
			bidPriceResultUnit_label.setText(ObjectUtils.getObjString(bidPriceResult.getPriceUnit(defaultUnit)));
			bidPriceResult_text.setText(bidPriceResult.getFormatBidPriceByUnit(bidPriceResult.getPriceUnit(defaultUnit)));
		}else {
			bidPriceResult_text.setText(ObjectUtils.getObjString(bidPriceResult.getPriceContent()));
		}
	}
	
	/**
	 * @Description: 
	 * @author chenxw 
	 */
	public void setPriceItem() {
		bidPriceResult_text.setTextLimit(15);
//		bidPriceResult_text.addModifyListener(new BigDecimalModifyListener(bidPriceResult_text));
		if (CommonEnum.YUAN.equals(CommonEnum.getPriceUnitType(bidPriceResult.getPriceUnit()))) {
			bidPriceResult_text.addModifyListener(new DecimalPlacesModifyListener(bidPriceResult_text, 2));
		} else if (CommonEnum.WAN_YUAN.equals(CommonEnum.getPriceUnitType(bidPriceResult.getPriceUnit()))) {
			bidPriceResult_text.addModifyListener(new DecimalPlacesModifyListener(bidPriceResult_text, 6));
		}
	}
	
	public void refreshBidPriceUnit(String currencyCode, String currencyName, String priceUnit){
		if(BidPriceEnum.IS_PRICE_ITEM.equals(bidPriceResult.getBidPriceType())){
			bidPriceResult.setCurrencyCode(currencyCode);
			bidPriceResult.setCurrencyName(currencyName);
			bidPriceResult.setPriceUnit(priceUnit);
			bidPriceResultUnit_label.setText(ObjectUtils.getObjString(bidPriceResult.getPriceUnit()));
			bidPriceResult_text.setText(bidPriceResult.getFormatBidPriceByUnit(bidPriceResult.getPriceUnit()));
		}
	}
	
	public BidPriceResult getBidPriceResult(){
		if(BidPriceEnum.IS_PRICE_ITEM.equals(bidPriceResult.getBidPriceType())){
			bidPriceResult.setBidPrice(MoneyUtils.getMoneyFromText(bidPriceResult_text.getText().trim(), bidPriceResult.getPriceUnit()));
			if(null != bidPriceResult.getBidPrice()) {
				bidPriceResult.setPriceContent(MoneyUtils.amountToChinese(bidPriceResult.getBidPrice().doubleValue()));
			}
		}else {
			bidPriceResult.setPriceContent(bidPriceResult_text.getText().trim());
		}
		return bidPriceResult;
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
