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

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.gx.obe.ColorConstants;
import com.gx.obe.DictEnum;
import com.gx.obe.DictValueEnum;
import com.gx.obe.common.widget.button.DwgcItemButton;
import com.gx.obe.evaluation.step.inter.IDwgcDetail;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.DictionaryValue;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.DictionaryService;

/**
 * @Description: 单位工程明细
 * @author guoyr
 */
public class DwgcDetailComposite extends Composite {
	
	private DictionaryService dictionaryService = new DictionaryService();
	private DwgcSelectListener listener;
	private String curItemCode;// 记录当前选中的单位工程的项目
	private ConstructProject unitConstructProject;
	private Composite tabItem_composite;
	private Composite expert_list_composite;
	private DwgcItemButton curDwgcItem;
	private Composite load_composite;
	private StackLayout stackLayout = new StackLayout();
	
	{
		LogUtils.logClass(DwgcDetailComposite.class);
	}
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public DwgcDetailComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		tabItem_composite = new Composite(this, SWT.NONE);
		tabItem_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_tabItem_composite = new GridLayout(1, false);
		gl_tabItem_composite.verticalSpacing = 0;
		gl_tabItem_composite.marginWidth = 0;
		gl_tabItem_composite.marginHeight = 0;
		tabItem_composite.setLayout(gl_tabItem_composite);
		
		expert_list_composite = new Composite(tabItem_composite, SWT.NONE);
		expert_list_composite.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		expert_list_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, false, 1, 1));
		RowLayout rl_expert_list_composite = new RowLayout(SWT.HORIZONTAL);
		rl_expert_list_composite.marginRight = 0;
		rl_expert_list_composite.marginTop = 0;
		rl_expert_list_composite.marginLeft = 0;
		rl_expert_list_composite.marginBottom = 0;
		rl_expert_list_composite.spacing = 1;
		expert_list_composite.setLayout(rl_expert_list_composite);
		
		DwgcItemButton item_label = new DwgcItemButton(expert_list_composite, SWT.NONE);
		item_label.setText("郭永荣");
		
		ESepator expert_tab = new ESepator(tabItem_composite, SWT.NONE);
		expert_tab.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		expert_tab.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		expert_tab.setLineWidth(2);
		
		load_composite = new Composite(this, SWT.NONE);
		load_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		load_composite.setLayout(stackLayout);
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init() {
		List<DictionaryValue> dictValueList = dictionaryService.getDictionaryValueList(DictEnum.BUSSINES_TENDER);
		for (Control control : expert_list_composite.getChildren()) {
			control.dispose();
		}
		
		if (null != dictValueList && dictValueList.size() > 0) {
			for (int i = 0; i < dictValueList.size(); i++) {
				final DictionaryValue dictionaryValue = dictValueList.get(i);
				final DwgcItemButton label = new DwgcItemButton(expert_list_composite, SWT.NONE);
				label.setText(ObjectUtils.getObjString(dictionaryValue.getDictValueName()));
				label.setData("dictionaryValueCode", dictionaryValue.getDictValueCode());
				label.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent e) {
						selectItemAction(label, dictionaryValue);
					}
				});
				if (i == 0) {
					curDwgcItem = label;
					curDwgcItem.setSelection(true);
				}
			}
		}
		
		listener = new DwgcSelectListener() {
			
			public void select(TenderProjectEntity tenderProject, Supplier supplier, ConstructProject _unitConstructProject, String _curItemCode) {
				unitConstructProject = _unitConstructProject;
				if (null != _curItemCode) {
					curItemCode = _curItemCode;
					if (null != _curItemCode) {
						for (Control control : expert_list_composite.getChildren()) {
							if (null != control.getData("dictionaryValueCode") && control.getData("dictionaryValueCode").equals(_curItemCode)) {
								control.notifyListeners(SWT.MouseUp, null);
								break;
							}
						}
					}
				} else if (null != curDwgcItem) {
					curDwgcItem.notifyListeners(SWT.MouseUp, null);
				}
			}
			
			public String getItemCode() {
				return curItemCode;
			}
			
		};
	}
	
	/**
	 * @Description: 选中单位工程明细项
	 * @author guoyr
	 */
	private void selectItemAction(DwgcItemButton itemButton, DictionaryValue dictionaryValue) {
		if (null != curDwgcItem) {
			curDwgcItem.setSelection(false);
//			curDwgcItem.setImage(null);
		}
		curDwgcItem = itemButton;
		curDwgcItem.setSelection(true);
//		curDwgcItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/television.png"));
		curItemCode = dictionaryValue.getDictValueCode();
		IDwgcDetail dwgcDetail = (IDwgcDetail) itemButton.getData("dwgcDetail");
		if (null == dwgcDetail) {
			if (StringUtils.equals(DictValueEnum.BusinessTender.BILL_TABLE, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new FbfxDetailComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.MEASURE_TABLE, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new MeasureTableDetailComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.EXTRA_TABLE, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new OtherItemDetailComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.MATERIAL_SUMMARY, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new GljHzDetailComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.COST_TABLE, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new CostTableComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.FEES_TAXES, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new FeesItemDetailComposite(load_composite, SWT.NONE);
//				dwgcDetail = new GfSjDetailComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.ZC_MATERIAL_SUMMARY, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new GljItemDetailComposite(load_composite, SWT.NONE);
			} else if (StringUtils.equals(DictValueEnum.BusinessTender.SUPPLY_MATERIAL_DETAIL, dictionaryValue.getDictValueCode())) {
				dwgcDetail = new SupplyMaterialDetailComposite(load_composite, SWT.NONE);
			} else {
				return;
			}
			dwgcDetail.refreshDetail(unitConstructProject);
			itemButton.setData("dwgcDetail", dwgcDetail);
		}
		stackLayout.topControl = dwgcDetail.getComposite();
		load_composite.layout();
	}
	
	public interface DwgcSelectListener {
		
		public void select(TenderProjectEntity tenderProject, Supplier supplier, ConstructProject constructProject, String curItemCode);
		
		public String getItemCode();
	}
	
	public DwgcSelectListener getListener() {
		return listener;
	}
	
	public void setListener(DwgcSelectListener listener) {
		this.listener = listener;
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
