package com.gx.obe.management.composite;

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.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
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.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
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.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;

/**
 * @Description: 导入项目专家界面——项目列表界面
 * @author guoyr
 */
public class ExpertImportProjectListComposite extends Composite {
	
	private Grid projectGrid ;
	private GridColumn tenderNoColumn;
	private TenderProjectEntity tenderProjectEntity;
	private ImageResolver imageResolver = new ImageResolver();
	private Image projectImage ;
	private Image singleProjectImage ;
	private Image subPackageImage;
	private TenderProjectService tenderProjectService ;
	private CallBack callBack;
	private SelectTenderProjectExpertListener addSelectTenderProjectExpertListener;
	private Image phaseTypeImage;

	{
		LogUtils.logClass(ExpertImportProjectListComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ExpertImportProjectListComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		setLayout(new FillLayout(SWT.HORIZONTAL));
		this.tenderProjectEntity = (TenderProjectEntity)parent.getData("tenderProjectEntity");
		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);
		
		projectGrid = new Grid(composite, SWT.H_SCROLL | SWT.V_SCROLL);
		projectGrid.setHeaderVisible(true);
		projectGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		tenderNoColumn = new GridColumn(projectGrid, SWT.NONE);
		tenderNoColumn.setText("编号");
		tenderNoColumn.setWordWrap(true);
		tenderNoColumn.setWidth(50);
		
		GridColumn gridColumn = new GridColumn(projectGrid, SWT.NONE);
		gridColumn.setText(Messages.Name);
		gridColumn.setWidth(120);
		gridColumn.setWordWrap(true);
		init();
	}

	private void init(){
		tenderProjectService = new TenderProjectService();
		projectImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/project_parent.png");
		subPackageImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/project_package.png");
		singleProjectImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/project_project.png");
		projectGrid.setItemHeight(30);
		projectGrid.setAutoWidth(true);
		projectGrid.setAutoHeight(true);
//		gridColumn.setTreeTogglePic(ImageResource.GLOBAL_RESOURCE.loadImage(SWTCoreActivator.PLUGIN_ID,"icons/\add.png"), ImageResource.GLOBAL_RESOURCE.loadImage(SWTCoreActivator.PLUGIN_ID,"icons/\del.png"));
		tenderNoColumn.setShowTreeTogglePic(false);
		tenderNoColumn.setTree(true);
		
		// 添加项目表格的选中事件和右键菜单事件
		projectGrid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				selectTenderProjectAction();
			}
		});
		refreshTenderProjectTree();
	}

	/**
	 * @Description: 刷新项目树
	 * @author guoyr
	 */
	private void refreshTenderProjectTree(){
		String tenderId = null != tenderProjectEntity?tenderProjectEntity.getId():null;
		List<TenderProjectEntity> tenderProjectList = tenderProjectService.getTenderProjectByExpert();
		
		if(null != tenderProjectList){
			for(TenderProjectEntity tenderProjectEntity : tenderProjectList){
//				if(StringUtils.isEmpty(tenderProjectEntity.getParentId())){
					// 不显示当前项目
					if(null != tenderId && tenderId.equals(tenderProjectEntity.getId())){
//						projectGrid.setSelection(new GridItem[]{gridItem});
						continue;
					}
					GridItem gridItem = new GridItem(projectGrid, SWT.NONE);
					gridItem.setText(0, tenderProjectEntity.getTenderNo());
					gridItem.setText(1, tenderProjectEntity.getTenderName());
					if(null != tenderProjectEntity.getPhaseType() && tenderProjectEntity.getPhaseType().equals(TenderProjectEnum.PHASE_TYPE_PRE)){
						if(null == phaseTypeImage){
							phaseTypeImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/phase_type.png");
						}
						gridItem.setImage(1, phaseTypeImage);
					}
					int childCount = 0;
					GridItem curTenderItem = null;//记录是不是当前包件
					for(TenderProjectEntity subPackage : tenderProjectList){
						if(StringUtils.isNotEmpty(subPackage.getParentId()) && subPackage.getParentId().equals(tenderProjectEntity.getId())){
							GridItem subPackGridItem = new GridItem(gridItem, SWT.NONE);
							subPackGridItem.setText(0, subPackage.getTenderNo());
							subPackGridItem.setData("tenderProjectEntity", subPackage);
							subPackGridItem.setText(1, subPackage.getTenderName());
							childCount ++;
							subPackGridItem.setImage(0, subPackageImage);
							if(null != tenderId && tenderId.equals(subPackage.getId())){
//								projectGrid.setSelection(new GridItem[]{subPackGridItem});
								curTenderItem = subPackGridItem;
							}
							if(null != subPackage.getPhaseType() && subPackage.getPhaseType().equals(TenderProjectEnum.PHASE_TYPE_PRE)){
								if(null == phaseTypeImage){
									phaseTypeImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/phase_type.png");
								}
								subPackGridItem.setImage(1, phaseTypeImage);
							}
						}
					}
					tenderProjectEntity.setChildrenCount(childCount);
					gridItem.setData("tenderProjectEntity", tenderProjectEntity);
					
					// 不显示当前包件，如果只有当前包件，则不显示其父级项目
					if(null != curTenderItem && childCount == 1){
						curTenderItem.dispose();
						gridItem.dispose();
					}else if(null != curTenderItem){
						curTenderItem.dispose();
					}
					
					if(!gridItem.isDisposed()){
						if(childCount > 0){
							gridItem.setExpanded(true);
							gridItem.setImage(0, projectImage);
						}else {
							gridItem.setImage(0, singleProjectImage);
						}
					}
//				}
			}
		}
		
		// 默认选中第一个项目或第一个项目的第一个包件
		if(projectGrid.getItemCount() >0){
			if(projectGrid.getItem(0).getItemCount() > 0){
				projectGrid.setSelection(1);
			}else {
				projectGrid.setSelection(0);
			}
		}
		
		selectTenderProjectAction();
	}
	
	/**
	 * @Description: 选中项目事件
	 * @author guoyr
	 */
	private void selectTenderProjectAction(){
		if(projectGrid.getSelectionCount() > 0){
			GridItem gridItem = projectGrid.getSelection()[0];
			TenderProjectEntity selectTenderProject = (TenderProjectEntity)gridItem.getData("tenderProjectEntity");
			if(null != callBack){
				callBack.callBackObject(selectTenderProject);
			}
		}
		
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	interface SelectTenderProjectExpertListener{
		public void selectTenderProject(TenderProjectEntity tenderProjectEntity, boolean isParentProject);
		
	}
	
//	public void addSelectTenderProjectExpertListener(){
//		this.selectTenderProjectExpertListener = selectTenderProjectExpertListener;
//	}
	public CallBack getCallBack() {
		return callBack;
	}

	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
		selectTenderProjectAction();
	}

}
