package com.gx.obe.management.composite;

import java.util.ArrayList;
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.core.ImageResolver;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;


import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评审指标树面板
 * @author guoyr
 */
public class EvaluationFactorTreeComposite extends Composite {
	
	private Grid factorGrid ;
	private GridItem complianceParentItem;
	private GridItem detailParentItem;
	private GridItem priceParentItem;
	private GridItem billParentItem;
	private GridColumn tenderNoColumn;
	private ImageResolver imageResolver = new ImageResolver();
	private Image menuOpenImage;
	private Image menuImage;
	private Image topImage;
	
	{
		LogUtils.logClass(EvaluationFactorTreeComposite.class);
	}
	
	/**
	 * @param parent
	 * @param style
	 */
	public EvaluationFactorTreeComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		setLayout(new FillLayout(SWT.HORIZONTAL));
		Composite composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		
		Composite composite_3 = new Composite(composite, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_3 = new GridLayout(3, false);
		composite_3.setLayout(gl_composite_3);
		composite_3.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label = new CLabel(composite_3, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/television.png"));
		label.setText("指标列表");
		
		ELink button = new ELink(composite_3, SWT.NONE);
		button.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/expend.png"));
		button.setToolTipText("展开");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				expendTreeAction();
			}
		});
		ELink button_1 = new ELink(composite_3, SWT.NONE);
		button_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/merge.png"));
		button_1.setToolTipText("合并");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				closeTreeAction();
			}
		});
		factorGrid = new Grid(composite, SWT.V_SCROLL);
		factorGrid.setHeaderVisible(false);
		factorGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		tenderNoColumn = new GridColumn(factorGrid, SWT.CHECK);
		tenderNoColumn.setText("指标名称");
		tenderNoColumn.setWidth(150);
		tenderNoColumn.setWordWrap(true);
		init();
	}

	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		factorGrid.setAutoHeight(true);
		factorGrid.setAutoWidth(true);
		factorGrid.setItemHeight(30);
		tenderNoColumn.setTreeTogglePic(ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_expend.png"), ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_close.png"));
		tenderNoColumn.setShowTreeTogglePic(true);
		tenderNoColumn.setTree(true);
		menuOpenImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_parent.png");
		menuImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_item.png");
		topImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/television.png");
		
		factorGrid.removeAll();
	}
	
	/**
	 * @Description:指标树重新加载 
	 * @author guoyr
	 * @param evalFactorList
	 * @return
	 */
	public int refreshFactorTree(List<EvaluationFactor> evalFactorList){
		factorGrid.removeAll();
		complianceParentItem = null;
		detailParentItem = null;
		priceParentItem = null;
		billParentItem = null;
		
		if(null != evalFactorList && evalFactorList.size() > 0){
			for(EvaluationFactor factor : evalFactorList){
				GridItem gridItem = null;
				if(StringUtils.equals(EvaluationFactorEnum.AUDIT_COMPLIANCE, factor.getAuditType())){// 符合性评审
					if(null == complianceParentItem){
						complianceParentItem = createTopItem(Messages.Factor_compliance);
					}
					
					gridItem = new GridItem(complianceParentItem, SWT.NONE);
				}else if(StringUtils.equals(EvaluationFactorEnum.AUDIT_SCORE, factor.getAuditType()) || StringUtils.equals(EvaluationFactorEnum.AUDIT_CREDIT, factor.getAuditType())){// 打分评审
					if(null == detailParentItem){
						detailParentItem = createTopItem(Messages.Factor_score);
					}
					gridItem = new GridItem(detailParentItem, SWT.NONE);
					
				}else if(StringUtils.equals(EvaluationFactorEnum.AUDIT_PRICE, factor.getAuditType())){
					if(null == priceParentItem){
						priceParentItem = createTopItem(Messages.Factor_price);
					}
					gridItem = new GridItem(priceParentItem, SWT.NONE);
				}else if(StringUtils.equals(EvaluationFactorEnum.AUDIT_BILL, factor.getAuditType())){
					if(null == billParentItem){
						billParentItem = createTopItem(com.gx.obe.components.core.I18N.getString(I18NEnum.BILL_FACTOR)+"指标");
					}
					gridItem = new GridItem(billParentItem, SWT.NONE);
				}
				
				if(null != gridItem){
					gridItem.setImage(0, topImage);
					gridItem.setText(0, factor.getFactorName());
					gridItem.setData("evaluationFactors", factor);
					initChildGridItem(factor, gridItem);
				}
				
			}
		}
		return factorGrid.getItemCount();
	}

	/**
	 * @Description: 创建顶部（表头）
	 * @author guoyr
	 * @param name
	 * @return
	 */
	private GridItem createTopItem(String name){
		GridItem topGridItem = new GridItem(factorGrid, SWT.NONE);
		topGridItem.setImage(0, topImage);
		topGridItem.setText(name);
		topGridItem.setExpanded(true);
		topGridItem.setChecked(0, true);
		return topGridItem;
	}
	
	/**
	 * @Description: 树展开操作
	 * @author guoyr
	 */
	private void expendTreeAction(){
		if(null != complianceParentItem){
			complianceParentItem.setExpanded(true);
		}
		if(null != detailParentItem){
			detailParentItem.setExpanded(true);
		}
		if(null != priceParentItem){
			priceParentItem.setExpanded(true);
		}
		if(null != billParentItem){
			billParentItem.setExpanded(true);
		}
	}
		
	private void closeTreeAction(){
		if(null != complianceParentItem){
			complianceParentItem.setExpanded(false);
		}
		if(null != detailParentItem){
			detailParentItem.setExpanded(false);
		}
		if(null != priceParentItem){
			priceParentItem.setExpanded(false);
		}
		if(null != billParentItem){
			billParentItem.setExpanded(false);
		}
	}
//	public void getImportEvaluationFactorList(){
//		List<GridItem> topGridItemList = GridUtil.getCheckedValues(factorGrid, 0, "evaluationFactors", true);
//		if(null != topGridItemList){
//			for(GridItem topGridItem : topGridItemList){
//				
//			}
//		}
//		GridUtil.getCheckedItems(factorGrid, 0, true);
//	}
	
	/**
	 * @Description: 采用递归方法加载指标树
	 * @author guoyr
	 * @param factor
	 * @param parentGridItem
	 */
	private void initChildGridItem(EvaluationFactor factor, GridItem parentGridItem){
		if(null != factor.getChildFactorList() && factor.getChildFactorList().size() > 0){
			for(EvaluationFactor childFactor : factor.getChildFactorList()){
				if(null != childFactor.getFactorType() && childFactor.getFactorType().equals(EvaluationFactorEnum.FACTOR_FLOAT)){
					continue;
				}
				GridItem gridItem = new GridItem(parentGridItem, SWT.NONE);
				gridItem.setText(0, childFactor.getFactorName());
				gridItem.setData("evaluationFactors", childFactor);
				initChildGridItem(childFactor, gridItem);
			}
			parentGridItem.setExpanded(true);
			parentGridItem.setImage(0, menuOpenImage);
		}else {
			parentGridItem.setImage(0, menuImage);
		}
		parentGridItem.setCheckVisible(0, false);
		parentGridItem.setToolTipText(0, parentGridItem.getText());
	}

	/**
	 * @Description: 选择要导入的指定分类的指标
	 * @author guoyr
	 * @param auditType
	 * @return
	 */
	public List<EvaluationFactor> getEvaluationFactorListByType(String auditType){
		List<EvaluationFactor> importEvalFactorList = null;
		if(StringUtils.equals(EvaluationFactorEnum.AUDIT_COMPLIANCE, auditType)){// 符合性评审
			if(null != complianceParentItem && complianceParentItem.getChecked(0)){
				importEvalFactorList = initEvaluationFactorTree(complianceParentItem);
			}
		}else if(StringUtils.equals(EvaluationFactorEnum.AUDIT_SCORE, auditType)){// 打分评审
			if(null != detailParentItem && detailParentItem.getChecked(0)){
				importEvalFactorList = initEvaluationFactorTree(detailParentItem);
			}
		}else if(StringUtils.equals(EvaluationFactorEnum.AUDIT_PRICE, auditType)){
			if(null != priceParentItem && priceParentItem.getChecked(0)){
				importEvalFactorList = initEvaluationFactorTree(priceParentItem);
			}
		}else if(StringUtils.equals(EvaluationFactorEnum.AUDIT_BILL, auditType)){
			if(null != billParentItem && billParentItem.getChecked(0)){
				importEvalFactorList = initEvaluationFactorTree(billParentItem);
			}
		}
		return importEvalFactorList;
	}
	
	/**
	 * @Description: 获取评审指标节点
	 * @author guoyr
	 * @param topItem
	 * @return
	 */
	public List<EvaluationFactor> initEvaluationFactorTree(GridItem topItem){
		List<EvaluationFactor> importEvalFactorList = new ArrayList<EvaluationFactor>();
		// 已经是组装好的树型结构，所以只需要获取最外层节点的指标就可以
		if(topItem.getItemCount() > 0){
			for(GridItem item : topItem.getItems()){
				Object factorObj = (EvaluationFactor)item.getData("evaluationFactors");
				if(null != factorObj){
					EvaluationFactor factor = (EvaluationFactor)factorObj;
					importEvalFactorList.add(factor);
				}
			}
		}
		return importEvalFactorList;
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
