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.InventoryPriceCheckResult;
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.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 清单报价检查
 * @author guoyr 
 */
public class InventoryPriceItemListComposite extends Composite implements IBoqChecker{
	
	private Grid checkItemGrid;
	private EvaluationStep evaluationStep;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public InventoryPriceItemListComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 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);
		checkItemGrid.setWordWrapHeader(true);
		
		
		GridColumn gridColumn_num = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_num.setTree(true);
		gridColumn_num.setText("序号");
		gridColumn_num.setWidth(50);
		gridColumn_num.setTreeIndent(10);
		
		GridColumn gridColumn_2 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_2.setText("清单编码");
		gridColumn_2.setWidth(80);
		
		GridColumn gridColumn_3 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_3.setText("清单名称");
		gridColumn_3.setWidth(150);
		gridColumn_3.setWordWrap(true);
		
		GridColumn gridColumn_op = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_op.setText("检查\r\n结果");
		gridColumn_op.setWidth(40);
		gridColumn_op.setHeaderWordWrap(true);
		
		GridColumn gridColumn_4 = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_4.setText("单位");
		gridColumn_4.setWidth(40);
		
		GridColumn gridColumn_5 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_5.setText("数量");
		gridColumn_5.setWidth(60);
		
		GridColumn gridColumn_6 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_6.setText("单价(元)");
		gridColumn_6.setWidth(80);
		
		GridColumn gridColumn_7 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_7.setText("定额单价（元）");
		gridColumn_7.setWidth(80);
		gridColumn_7.setHeaderWordWrap(true);
		
		GridColumn gridColumn_8 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_8.setText("比定额单价差额");
		gridColumn_8.setWidth(50);
		gridColumn_8.setHeaderWordWrap(true);
		
		GridColumn gridColumn_9 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_9.setText("比定额单价差额率(%)");
		gridColumn_9.setWidth(40);
		gridColumn_9.setHeaderWordWrap(true);
		
		GridItem gridItem_1 = new GridItem(checkItemGrid, SWT.NONE);
		gridItem_1.setText("New Row");
		init();
	}
	
	/**
	 * @Description:  初始化界面
	 * @author guoyr
	 */
	private void init(){
		checkItemGrid.removeAll();
		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<InventoryPriceCheckResult> checkResultList = check.getCheckResultErrorList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(InventoryPriceCheckResult 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<InventoryPriceCheckResult> checkResultList = check.getCheckResultDetailTreeList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(InventoryPriceCheckResult checkResult : checkResultList){
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				constructItem.setText(0, NumericChineseUtils.getChinese(i)+"");
				constructItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
				constructItem.setText(1, checkResult.getName());
				constructItem.setColumnSpan(1, checkItemGrid.getColumnCount() -2);
				initSubCheckResult(checkResult, constructItem, false);
				i ++;
			}
		}
	}
	
	private void initSubCheckResult(InventoryPriceCheckResult checkResult, GridItem parentItem, boolean getParentNo){
		List<InventoryPriceCheckResult> checkResultList = checkResult.getChildList();
		if(null != checkResultList && checkResultList.size() > 0){
			parentItem.setExpanded(true);
			int index = 1;
			for(InventoryPriceCheckResult 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, InventoryPriceCheckResult result){
		gridItem.setText(1, ObjectUtils.getObjString(result.getCode()));
		gridItem.setText(2, ObjectUtils.getObjString(result.getName()));
		if(result.getFlag() != 0){
			if(result.getFlag() > 0){
				gridItem.setText(3, "过高");
			}else {
				gridItem.setText(3, "过低");
			}
			gridItem.setForeground(3,SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(8,SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(9,SWTResourceManager.getColor(SWT.COLOR_RED));
		}else {
			
			gridItem.setText(3, "正常");
			gridItem.setForeground(3,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			gridItem.setForeground(8,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			gridItem.setForeground(9,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			if(null != evaluationStep){
				//将合理的用不同梯度颜色区分
				if(null != result.getDifferenceRate() && result.getDifferenceRate().compareTo(BigDecimal.ZERO) != 0){
					if(result.getDifferenceRate().compareTo(BigDecimal.ZERO) > 0){
						BigDecimal priceRateMaxRate = evaluationStep.getEvaluationParamByKey(EvaluationStep.MAX_DIFFERENCE_RATE, BigDecimal.class);
						if(null == priceRateMaxRate || priceRateMaxRate.compareTo(BigDecimal.ZERO) <= 0){
							priceRateMaxRate = BigDecimal.valueOf(25);
						}
						// 将差额率分为5个级别
						int gradient = BigDecimalUtils.getGradient(result.getDifferenceRate(), priceRateMaxRate, 10);
						// 获得每个级别的由绿到红的梯度颜色
						int[] rgb = ColorUtils.greenToRedColorOf(gradient);
						gridItem.setForeground(8,SWTResourceManager.getColor(rgb[0], rgb[1], rgb[2]));
					}else {
						BigDecimal priceRateMinRate = evaluationStep.getEvaluationParamByKey(EvaluationStep.MIN_DIFFERENCE_RATE, BigDecimal.class);
						if(null == priceRateMinRate || priceRateMinRate.compareTo(BigDecimal.ZERO) >= 0){
							priceRateMinRate = BigDecimal.valueOf(-25);
						}
						// 将差额率分为5个级别
						int gradient = BigDecimalUtils.getGradient(result.getDifferenceRate(), priceRateMinRate, 10);
						// 获得每个级别的由绿到红的梯度颜色
						int[] rgb = ColorUtils.greenToRedColorOf(gradient);
						gridItem.setForeground(8,SWTResourceManager.getColor(rgb[0], rgb[1], rgb[2]));
					}
					
				}
				if(null != result.getDifference() && result.getDifference().compareTo(BigDecimal.ZERO) != 0){
					BigDecimal rate = evaluationStep.getEvaluationParamByKey(EvaluationStep.CALCULATION_ERROR_VALUE, BigDecimal.class);
					// 将差额率分为5个级别
					int gradient = BigDecimalUtils.getGradient(result.getDifference(), rate, 10);
					// 获得每个级别的由绿到红的梯度颜色
					int[] rgb = ColorUtils.greenToRedColorOf(gradient);
					gridItem.setForeground(9,SWTResourceManager.getColor(rgb[0], rgb[1], rgb[2]));
					
				}
			}
		}
		gridItem.setText(4, ObjectUtils.getObjString(result.getUnit()));
		gridItem.setText(5, ObjectUtils.getObjString(result.getQuantity()));
		gridItem.setText(6, ObjectUtils.getObjString(result.getPrice()));
		gridItem.setText(7, ObjectUtils.getObjString(result.getComparePrice()));
		gridItem.setText(8, ObjectUtils.getObjString(result.getDifference()));
		gridItem.setText(9, ObjectUtils.getObjString(result.getDifferenceRate()));
	}
	
	protected void checkSubclass() {
	}
}
