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

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
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.common.widget.button.DwgcItemRoundButton;
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.web.entity.DictionaryValue;
import com.gx.obe.web.entity.boq.ConstructProject;

/**
 * @Description: 其他项目明细
 * @author guoyr
 */
public class FeesItemDetailComposite extends Composite implements IDwgcDetail {
	
	{
		LogUtils.logClass(FeesItemDetailComposite.class);
	}
	private ConstructProject unitConstructProject;
	private List<DictionaryValue> detItemList;
	private Composite tabItem_composite;
	private Composite expert_list_composite;
	private DwgcItemRoundButton curDwgcItem;
	private Composite composite;
	private StackLayout stackLayout = new StackLayout();
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public FeesItemDetailComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 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.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		RowLayout rl_expert_list_composite = new RowLayout(SWT.HORIZONTAL);
		rl_expert_list_composite.marginHeight = 5;
		rl_expert_list_composite.marginRight = 0;
		rl_expert_list_composite.marginTop = 0;
		rl_expert_list_composite.marginLeft = 5;
		rl_expert_list_composite.marginBottom = 0;
		rl_expert_list_composite.spacing = 5;
		expert_list_composite.setLayout(rl_expert_list_composite);
		
		DwgcItemRoundButton item_label = new DwgcItemRoundButton(expert_list_composite, SWT.NONE);
		item_label.setText("郭永荣");
		
		composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite.setLayout(stackLayout);
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init() {
		for (Control control : expert_list_composite.getChildren()) {
			control.dispose();
		}
		detItemList = new ArrayList<DictionaryValue>();
		DictionaryValue gfDictionaryValue = new DictionaryValue();
		gfDictionaryValue.setDictValueName(" 规费 ");
		gfDictionaryValue.setDictValueCode("GF");
		detItemList.add(gfDictionaryValue);
		DictionaryValue aqwmsgfdDictionaryValue = new DictionaryValue();
		aqwmsgfdDictionaryValue.setDictValueCode("AQWMSGF");
		aqwmsgfdDictionaryValue.setDictValueName("安全文明施工费");
		detItemList.add(aqwmsgfdDictionaryValue);
//		detItemList = dictionaryService.getDictionaryValueList(DictEnum.EXTRA_TABLE);
		if (null != detItemList && detItemList.size() > 0) {
			for (int i = 0; i < detItemList.size(); i++) {
				final DictionaryValue dictionaryValue = detItemList.get(i);
				final DwgcItemRoundButton label = new DwgcItemRoundButton(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);
				}
			}
		}
	}
	
	/**
	 * @Description: 选中单位工程明细项
	 * @author guoyr
	 */
	private void selectItemAction(DwgcItemRoundButton 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"));
		IDwgcDetail dwgcDetail = (IDwgcDetail) itemButton.getData("dwgcDetail");
		if (null == dwgcDetail) {
			if ("GF".equals(dictionaryValue.getDictValueCode())) {
				dwgcDetail = new RateGFItemListComposite(composite, SWT.NONE);
			} else if ("AQWMSGF".equals(dictionaryValue.getDictValueCode())) {
				dwgcDetail = new RateAQWMSGFItemListComposite(composite, SWT.NONE);
			}
			dwgcDetail.refreshDetail(unitConstructProject);
			itemButton.setData("dwgcDetail", dwgcDetail);
		}
		stackLayout.topControl = dwgcDetail.getComposite();
		composite.layout();
	}
	
	/**
	 * @Description: 刷新分布分项明细
	 * @author guoyr
	 * @param xmlResolver
	 * @param dxgcCode
	 * @param dwgcCode
	 */
	public void refreshDetail(ConstructProject unitConstructProject) {
		this.unitConstructProject = unitConstructProject;
		if (null != curDwgcItem) {
			curDwgcItem.notifyListeners(SWT.MouseUp, null);
		}
	}
	
	public Composite getComposite() {
		return this;
	}
	
	protected void checkSubclass() {}
}
