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

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.ErrorLeakCheckResult;
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.LogUtils;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 错漏项检查
 * @author guoyr 
 */
public class ErrorLeakItemListComposite extends Composite implements IBoqChecker{
	
	private Grid checkItemGrid;
	{
		LogUtils.logClass(ErrorLeakItemListComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ErrorLeakItemListComposite(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);
		
		GridColumn gridColumn_num = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_num.setTree(true);
		gridColumn_num.setText("序号");
		gridColumn_num.setWidth(40);
		
		GridColumn gridColumn_3 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_3.setText("编码");
		gridColumn_3.setWidth(80);
		
		GridColumn gridColumn_2 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_2.setText("项目名称");
		gridColumn_2.setWidth(250);
		gridColumn_2.setWordWrap(true);
		
		GridColumn gridColumn_5 = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_5.setText("检查结果");
		gridColumn_5.setWidth(50);
		
		GridColumn gridColumn_6 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_6.setText("错误项");
		gridColumn_6.setWidth(100);
		
		GridColumn gridColumn_7 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_7.setText("正确项");
		gridColumn_7.setWidth(100);
		
		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();
		List<ErrorLeakCheckResult> checkResultList = check.getCheckResultErrorList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(ErrorLeakCheckResult 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();
//		check.check(tenderProject, supplier, evaluationStep, dictionaryValue);
		List<ErrorLeakCheckResult> checkResultList = check.getCheckResultDetailTreeList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(ErrorLeakCheckResult 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);
//				setCheckResult(constructItem, checkResult);
				initSubCheckResult(checkResult, constructItem, false);
				i ++;
			}
		}
	}
	
	private void initSubCheckResult(ErrorLeakCheckResult checkResult, GridItem parentItem, boolean getParentNo){
		List<ErrorLeakCheckResult> checkResultList = checkResult.getChildList();
		if(null != checkResultList && checkResultList.size() > 0){
			parentItem.setExpanded(true);
			int index = 1;
			for(ErrorLeakCheckResult 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, ErrorLeakCheckResult result){
		gridItem.setText(1, ObjectUtils.getObjString(result.getCode()));
		gridItem.setText(2, ObjectUtils.getObjString(result.getName()));
		gridItem.setText(4, ObjectUtils.getObjString(result.getErrorItem()));
		gridItem.setText(5, ObjectUtils.getObjString(result.getCorrectItem()));
		if(result.isReasonable()){
			gridItem.setText(3, "无错漏");
			gridItem.setForeground(3,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		}else {
			gridItem.setText(3, ObjectUtils.getObjString(result.getOpinion()));
			gridItem.setForeground(3,SWTResourceManager.getColor(SWT.COLOR_RED));
		}
	}
	
	protected void checkSubclass() {
	}
}
