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

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.NumberModifyListener;
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.components.core.vo.Finance;
import com.gx.obe.evaluation.step.inter.IStruct;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Struct;

/** 
 * @Description: 财务状况
 * @author chenxw 
 */
public class FinanceComposite extends Composite implements IStruct {
	private Text totalText;
	private Text liabilitiesText;
	private Text incomeText;
	private Text profitText;
	private Label liabilitiesRateLabel;
//	private Label currentAssetsLabel;
//	private Label currentLiabilitiesLabel;
//	private Label currentRateLabel;
	private Text paidUpCapitalText;
	private Text mainIncomeText;
	private Text mainIncomeProfitsText;
	private Label operatingMarginLabel;
	private Finance finance;
	private Struct struct;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 * @param object
	 */
	public FinanceComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(2, false));
		
		Label label = new Label(this, SWT.NONE);
		label.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("资产总计（万元）：");
		
		totalText = new Text(this, SWT.BORDER);
		totalText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("负债总价（万元）：");
		
		liabilitiesText = new Text(this, SWT.BORDER);
		liabilitiesText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_10 = new Label(this, SWT.NONE);
		label_10.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_10.setText("实收资本（万元）：");
		
		paidUpCapitalText = new Text(this, SWT.BORDER);
		paidUpCapitalText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		paidUpCapitalText.addModifyListener(numberModifyListener);
		
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("营业收入（万元）：");
		
		incomeText = new Text(this, SWT.BORDER);
		incomeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		incomeText.addModifyListener(numberModifyListener);
		
		Label label_3 = new Label(this, SWT.NONE);
		label_3.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("利润总额（万元）：");
		
		profitText = new Text(this, SWT.BORDER);
		profitText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		profitText.addModifyListener(numberModifyListener);
		
		Label label_8 = new Label(this, SWT.NONE);
		label_8.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setText("主营业务收入（万元）：");
		
		mainIncomeText = new Text(this, SWT.BORDER);
		mainIncomeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_5 = new Label(this, SWT.NONE);
		label_5.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setText("主营业务利润（万元）：");
		
		mainIncomeProfitsText = new Text(this, SWT.BORDER);
		mainIncomeProfitsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_6 = new Label(this, SWT.NONE);
		label_6.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_6.setText("主营业务利润率（%）：");
		
		operatingMarginLabel = new Label(this, SWT.WRAP);
		operatingMarginLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_4 = new Label(this, SWT.NONE);
		label_4.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText("资产负债率（%）：");
		
		liabilitiesRateLabel = new Label(this, SWT.WRAP);
		liabilitiesRateLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
//		Label label_5 = new Label(this, SWT.NONE);
//		label_5.setForeground(ColorConstants.FONT_BLACK_COLOR);
//		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
//		label_5.setText("流动资产（万元）：");
//		
//		currentAssetsLabel = new Label(this, SWT.WRAP);
//		currentAssetsLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//		
//		Label label_6 = new Label(this, SWT.NONE);
//		label_6.setForeground(ColorConstants.FONT_BLACK_COLOR);
//		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
//		label_6.setText("流动负债（万元）：");
//		
//		currentLiabilitiesLabel = new Label(this, SWT.WRAP);
//		currentLiabilitiesLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//		
//		Label label_7 = new Label(this, SWT.NONE);
//		label_7.setForeground(ColorConstants.FONT_BLACK_COLOR);
//		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
//		label_7.setText("流动比率（%）：");
//		
//		currentRateLabel = new Label(this, SWT.WRAP);
//		currentRateLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
	}
	
	/** 
	 * @Description: 刷新
	 * @author chenxw
	 * @param struct 
	 */
	public void refresh(Struct struct) {
		this.struct = struct;
		this.finance = struct.getFinance();
		
		totalText.setText(ObjectUtils.getObjString(finance.getTotal()));
		liabilitiesText.setText(ObjectUtils.getObjString(finance.getLiabilities()));
		incomeText.setText(ObjectUtils.getObjString(finance.getIncome()));
		mainIncomeText.setText(ObjectUtils.getObjString(finance.getMainIncome()));
		profitText.setText(ObjectUtils.getObjString(finance.getProfit()));
		mainIncomeProfitsText.setText(ObjectUtils.getObjString(finance.getMainIncomeProfits()));
		paidUpCapitalText.setText(ObjectUtils.getObjString(finance.getPaidUpCapital()));
		
		BigDecimal operatingMargin = BigDecimalUtils.div(finance.getMainIncomeProfits(), finance.getMainIncome());
		operatingMarginLabel.setText(ObjectUtils.getObjString(BigDecimalUtils.round(BigDecimalUtils.mulIsNull(operatingMargin, BigDecimal.valueOf(100)), 2)));
		BigDecimal liabilitiesRate = BigDecimalUtils.div(finance.getLiabilities(), finance.getTotal());
		liabilitiesRateLabel.setText(ObjectUtils.getObjString(BigDecimalUtils.round(BigDecimalUtils.mulIsNull(liabilitiesRate, BigDecimal.valueOf(100)), 2)));
//		currentAssetsLabel.setText(ObjectUtils.getObjString(finance.getCurrentAssets()));
//		currentLiabilitiesLabel.setText(ObjectUtils.getObjString(finance.getCurrentLiabilities()));
//		currentRateLabel.setText(ObjectUtils.getObjString(finance.getCurrentRate()));

		setListener();
	}
	
	/**
	 * @Description: 设置监听
	 * @author chenxw 
	 */
	private void setListener() {
		mainIncomeText.addModifyListener(operatingMarginListener);
		mainIncomeProfitsText.addModifyListener(operatingMarginListener);
		totalText.addModifyListener(liabilitiesRateListener);
		liabilitiesText.addModifyListener(liabilitiesRateListener);
	}

	/** 
	 * @Description: 设置是否编辑
	 * @author chenxw
	 * @param isEdit 
	 */
	public void setEdit(boolean isEdit) {
		totalText.setEnabled(isEdit);
		liabilitiesText.setEnabled(isEdit);
		incomeText.setEnabled(isEdit);
		mainIncomeText.setEnabled(isEdit);
		profitText.setEnabled(isEdit);
		mainIncomeProfitsText.setEnabled(isEdit);
		paidUpCapitalText.setEnabled(isEdit);
	}
	
	/** 
	 * @Description: 刷新编辑数据
	 * @author chenxw 
	 */
	public void refreshEditData() {
		finance.setTotal(ObjectUtils.getObjBigDecimal(totalText.getText().trim()));
		finance.setLiabilities(ObjectUtils.getObjBigDecimal(liabilitiesText.getText().trim()));
		finance.setIncome(ObjectUtils.getObjBigDecimal(incomeText.getText().trim()));
		finance.setMainIncome(ObjectUtils.getObjBigDecimal(mainIncomeText.getText().trim()));
		finance.setProfit(ObjectUtils.getObjBigDecimal(profitText.getText().trim()));
		finance.setMainIncomeProfits(ObjectUtils.getObjBigDecimal(mainIncomeProfitsText.getText().trim()));
		finance.setPaidUpCapital(ObjectUtils.getObjBigDecimal(paidUpCapitalText.getText().trim()));
		struct.setFinance(finance);
	}
	
	/**
	 * 数字验证
	 */
	NumberModifyListener numberModifyListener = new NumberModifyListener(BigDecimal.class, "0");
	
	/**
	 * 主营业务利润率计算监听
	 */
	ModifyListener operatingMarginListener = new ModifyListener() {
		public void modifyText(ModifyEvent arg0) {
			numberModifyListener.modifyText(arg0);
			finance.setMainIncomeProfits(ObjectUtils.getObjBigDecimal(mainIncomeProfitsText.getText().trim()));
			finance.setMainIncome(ObjectUtils.getObjBigDecimal(mainIncomeText.getText().trim()));
			
			BigDecimal operatingMargin = BigDecimalUtils.div(finance.getMainIncomeProfits(), finance.getMainIncome());
			operatingMarginLabel.setText(ObjectUtils.getObjString(BigDecimalUtils.round(BigDecimalUtils.mulIsNull(operatingMargin, BigDecimal.valueOf(100)), 2)));
		}
	};
	
	/**
	 * 资产负债率计算监听
	 */
	ModifyListener liabilitiesRateListener = new ModifyListener() {
		public void modifyText(ModifyEvent arg0) {
			numberModifyListener.modifyText(arg0);
			finance.setLiabilities(ObjectUtils.getObjBigDecimal(liabilitiesText.getText().trim()));
			finance.setTotal(ObjectUtils.getObjBigDecimal(totalText.getText().trim()));
			
			BigDecimal liabilitiesRate = BigDecimalUtils.div(finance.getLiabilities(), finance.getTotal());
			liabilitiesRateLabel.setText(ObjectUtils.getObjString(BigDecimalUtils.round(BigDecimalUtils.mulIsNull(liabilitiesRate, BigDecimal.valueOf(100)), 2)));
		}
	};
	
	/** 
	 * @Description: 获取标题名称
	 * @author chenxw
	 * @return 
	 */
	public String getTopName() {
		return ObjectUtils.getObjString(finance.getAnnual());
	}
	
	protected void checkSubclass() {}

 
	
}
