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

import java.math.BigDecimal;
import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.business.vo.ArithmeticErrorCheckResult;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.utils.ColorUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.evaluation.step.inter.IBoqChecker;
import com.gx.obe.evaluation.step.inter.IChecker;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 算术性错误检查
 * @author guoyr 
 */
public class ArithmeticErrorItemListComposite extends Composite implements IBoqChecker{
	{
		LogUtils.logClass(ArithmeticErrorItemListComposite.class);
	}
	private Grid checkItemGrid;
	private EvaluationStep evaluationStep;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ArithmeticErrorItemListComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);
		checkItemGrid = new Grid(this, SWT.H_SCROLL | SWT.V_SCROLL);
		checkItemGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		checkItemGrid.setHeaderVisible(true);
		
		GridColumn gridColumn_2 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_2.setTree(true);
		gridColumn_2.setText("序号");
		gridColumn_2.setWidth(65);
		gridColumn_2.setTreeIndent(10);
		
		GridColumn gridColumn_3 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_3.setText("项目名称");
		gridColumn_3.setWidth(150);
		gridColumn_3.setWordWrap(true);
		
		GridColumn gridColumn_6 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_6.setText("投标报价");
		gridColumn_6.setWidth(90);
		
		GridColumn gridColumn_7 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_7.setText("计算价格");
		gridColumn_7.setWidth(90);
		
		GridColumn gridColumn_8 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_8.setText("差额");
		gridColumn_8.setWidth(60);
		
		GridColumn gridColumn_rate = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_rate.setText("差额率%");
		gridColumn_rate.setWidth(60);
		
		GridColumn resultColumn = new GridColumn(checkItemGrid, SWT.CENTER);
		resultColumn.setText("是否合理");
		resultColumn.setWidth(50);
		
		GridColumn gridColumn_9 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_9.setText("检查结果");
		gridColumn_9.setWidth(150);
		gridColumn_9.setWordWrap(true);
		
		GridItem gridItem_1 = new GridItem(checkItemGrid, SWT.NONE);
		gridItem_1.setText("New Row");

		
		init();
	}
	
	/**
	 * @Description:  初始化界面
	 * @author guoyr
	 */
	private void init(){
		checkItemGrid.setItemHeight(Constants.ROW_HIGHT);
		checkItemGrid.setAutoHeight(true);
	}
	
	
	/**
	 * @Description:  只显示示错误
	 * @author guoyr
	 * @param check
	 * @param showErrorOnly
	 */
	public void listErrorOnlyCheckDetail(IChecker check){
		checkItemGrid.removeAll();
		this.evaluationStep = check.getEvaluationStep();
		List<ArithmeticErrorCheckResult> checkResultList = check.getCheckResultErrorList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(ArithmeticErrorCheckResult checkResult : checkResultList){
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				setCheckResult(constructItem, checkResult);
				constructItem.setText(0, (i++)+"");
			}
		}
	}
	
	/**
	 * @Description:  进行检查
	 * @author guoyr
	 */
	public void listCheckDetail(IChecker check){
		checkItemGrid.removeAll();
		this.evaluationStep = check.getEvaluationStep();
		List<ArithmeticErrorCheckResult> checkResultList = check.getCheckResultDetailTreeList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 0;
			for(ArithmeticErrorCheckResult checkResult : checkResultList){
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				if(i == 0){
					constructItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/sum.png"));
				}else {
					constructItem.setText(0, NumericChineseUtils.getChinese(i)+"");
					constructItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
				}
				setCheckResult(constructItem, checkResult);
				initSubCheckResult(checkResult, constructItem, false);
				i ++;
			}
		}
	}
	
	private void initSubCheckResult(ArithmeticErrorCheckResult checkResult, GridItem parentItem, boolean getParentNo){
		List<ArithmeticErrorCheckResult> checkResultList = checkResult.getChildList();
		if(null != checkResultList && checkResultList.size() > 0){
			parentItem.setExpanded(true);
			int index = 1;
			for(ArithmeticErrorCheckResult childCheckResult : checkResultList){
				GridItem constructItem = new GridItem(parentItem, SWT.NONE);
				if(getParentNo){
					constructItem.setText(0, parentItem.getText(0)+"."+ (index++));
				}else {
					constructItem.setText(0, (index++)+"");
				}
				setCheckResult(constructItem, childCheckResult);
				initSubCheckResult(childCheckResult, constructItem, true);
			}
		}
		
	}
	
	/**
	 * @Description:  设置检查结果
	 * @author guoyr
	 * @param gridItem
	 * @param totalPrice
	 * @param sumPrice
	 * @param error
	 */
	private void setCheckResult(GridItem gridItem, ArithmeticErrorCheckResult result){
		gridItem.setText(1, ObjectUtils.getObjString(result.getName()));
		gridItem.setText(2, ObjectUtils.getObjString(result.getPrice()));
		gridItem.setText(3, ObjectUtils.getObjString(result.getCountPrice()));
		gridItem.setText(4, ObjectUtils.getObjString(result.getDifference()));
		gridItem.setText(5, ObjectUtils.getObjString(result.getDifferenceRate()));
//		gridItem.setText(2, NumberFormatUtils.format(result.getPrice(), 10));
//		gridItem.setText(3, NumberFormatUtils.format(result.getCountPrice(),10));
//		gridItem.setText(4, NumberFormatUtils.format(result.getDifference(), 10));
//		gridItem.setText(5, NumberFormatUtils.format(result.getDifferenceRate(), 10));
//		gridItem.setForeground(3,SWTResourceManager.getColor(SWT.COLOR_BLUE));
		if(result.isReasonable()){
			gridItem.setText(6, "合理");
			gridItem.setText(7, ObjectUtils.getObjString(result.getOpinion()));
			gridItem.setForeground(4,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			if(null != evaluationStep){
				//将合理的用不同梯度颜色区分
				if(null != result.getDifference() && result.getDifference().compareTo(BigDecimal.ZERO) != 0){
					BigDecimal value = evaluationStep.getEvaluationParamByKey(EvaluationStep.CALCULATION_ERROR_VALUE, BigDecimal.class);
					// 将差额分为5个级别
					int gradient = BigDecimalUtils.getGradient(result.getDifference(), value, 10);
					// 获得每个级别的由绿到红的梯度颜色
					int[] rgb = ColorUtils.greenToRedColorOf(gradient);
					gridItem.setForeground(4,SWTResourceManager.getColor(rgb[0], rgb[1], rgb[2]));
				}
				gridItem.setForeground(5,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
				if(null != result.getDifferenceRate() && result.getDifferenceRate().compareTo(BigDecimal.ZERO) != 0){
					
					BigDecimal rate = evaluationStep.getEvaluationParamByKey(EvaluationStep.CALCULATION_ERROR_RATE, BigDecimal.class);
					// 将差额率分为5个级别
					int gradient = BigDecimalUtils.getGradient(result.getDifferenceRate(), rate, 10);
					// 获得每个级别的由绿到红的梯度颜色
					int[] rgb = ColorUtils.greenToRedColorOf(gradient);
					gridItem.setForeground(5,SWTResourceManager.getColor(rgb[0], rgb[1], rgb[2]));
					
				}
			}
			gridItem.setForeground(6,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		}else {
			gridItem.setText(6, "不合理");
			gridItem.setText(7, ObjectUtils.getObjString(result.getOpinion()).replace("=", "≠"));
			gridItem.setForeground(4,SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(5,SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(6,SWTResourceManager.getColor(SWT.COLOR_RED));
		}
	}
	
	
	public static void main(String[] arges){
		BigDecimal je = BigDecimal.ZERO;
		je = BigDecimalUtils.add(je, mul("1.22", "59.267742965911"));
		je = BigDecimalUtils.add(je, mul("0.3", "3.15094302837765"));
		je = BigDecimalUtils.add(je, mul("306", "0.428654549891124"));
		System.out.println(je);
		
	}
	public static BigDecimal add(BigDecimal b1, String b2){
		BigDecimal sum = BigDecimalUtils.add(b1, new BigDecimal(b2));
		return sum;
	}
	public static BigDecimal mul(String b1, String b2){
		BigDecimal sum = BigDecimalUtils.mul(new BigDecimal(b1), new BigDecimal(b2));
		return sum;
	}
	protected void checkSubclass() {
	}
}
