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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.SwtConstants;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StackLayout;
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.events.MouseTrackAdapter;
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 org.eclipse.swt.widgets.Display;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.widget.button.CleanItemButton;
import com.gx.obe.common.widget.button.EvalLeftButton;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.composite.clear.BusinessClearTenderItemComposite;
import com.gx.obe.evaluation.step.composite.clear.CleanTenderResultKtableComposite;
import com.gx.obe.evaluation.step.composite.clear.CleanTenderResultKtableComposite.CleanTenderListener;
import com.gx.obe.evaluation.step.dialog.ViewBoqDialog;
import com.gx.obe.evaluation.step.inter.BillCheckerCache;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.evaluation.step.utils.BillCheckerCacheUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.web.entity.DictionaryValue;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.BaseDictionaryValueService;
import com.gx.obe.web.service.CheckResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.utils.ActionLogUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 清标评审
 * @author guoyr
 */
public class BusinessClearTenderComposite extends Composite {
	{
		LogUtils.logClass(BusinessClearTenderComposite.class);
	}
	private TenderProjectService tenderProjectService = new TenderProjectService();
	private SupplierService supplierService = new SupplierService();
	private BaseDictionaryValueService dictionaryValueService = new BaseDictionaryValueService();
	private CheckResultService checkResultService = new CheckResultService();
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	private List<Supplier> supplierList = new ArrayList<Supplier>();
	private List<DictionaryValue> cleanItemList = new ArrayList<DictionaryValue>();
	private ImageResolver imageResolver = new ImageResolver();
	private Composite contentComposite;
	private Composite right_composite;
	private StackLayout stackLayout = new StackLayout();
	private List<DictionaryValue> dictionarValueyList;
	private Map<String, Composite> contentCompositeMap = new HashMap<String, Composite>();
	private ExpertEvaluationListener expertEvaluationListener;
	private CLabel curAction;
	private CleanItemButton curCleanItemBtn;
	private Composite tabItem_composite;
	private Composite expert_list_composite;
	private Composite left_tool_composite;
	private String expertUserId;
	private BillCheckerCache billItemCache;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public BusinessClearTenderComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				imageResolver.disposite();
			}
		});
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		tenderProject = (TenderProjectEntity) parent.getData("tenderProject");
		evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		
		left_tool_composite = new Composite(this, SWT.NONE);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		left_tool_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		left_tool_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		left_tool_composite.setLayout(gl_left_tool_composite);
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.NONE);
		goto_back.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		goto_back.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/back.png"));
		goto_back.setToolTipText("返回");
		goto_back.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		
		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.NONE);
		refresh_evaluation.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refresh_evaluation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		refresh_evaluation.setToolTipText("刷新");
		refresh_evaluation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				refreshAction();
			}
		});
		
		ESepator menu_sep1 = new ESepator(left_tool_composite, SWT.NONE);
		menu_sep1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		EvalLeftButton viewTenderDetail_btn = new EvalLeftButton(left_tool_composite, SWT.NONE);
		viewTenderDetail_btn.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/boq_1.png"));
		viewTenderDetail_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		viewTenderDetail_btn.setToolTipText("工程量清单");
		viewTenderDetail_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				viewBoqAction();
			}
		});
		
		ESepator toolv_sepator = new ESepator(this, SWT.VERTICAL);
		toolv_sepator.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		toolv_sepator.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		right_composite = new Composite(this, SWT.NONE);
		right_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_right_composite = new GridLayout(1, false);
		gl_right_composite.verticalSpacing = 0;
		gl_right_composite.horizontalSpacing = 0;
		gl_right_composite.marginHeight = 0;
		gl_right_composite.marginWidth = 0;
		right_composite.setLayout(gl_right_composite);
		
		tabItem_composite = new Composite(right_composite, 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.marginTop = 5;
		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.marginLeft = 0;
		rl_expert_list_composite.marginBottom = 0;
		rl_expert_list_composite.spacing = 5;
		expert_list_composite.setLayout(rl_expert_list_composite);
		
		CleanItemButton item_label = new CleanItemButton(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(SWTResourceManager.getColor(60, 138, 226));
		expert_tab.setLineWidth(2);
		
		contentComposite = new Composite(right_composite, SWT.NONE);
		contentComposite.setLayout(stackLayout);
		contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init() {
		expertUserId = Constants.USER.getId();
		billItemCache = BillCheckerCacheUtils.createBillCheckerCache();
		// 修改评标专家的评审记录
		ActionLogUtils.addServerLog(tenderProject.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProject.getId(), evaluationStep, expertUserId);
		refreshAction();
	}
	
	/**
	 * @Description: 返回
	 * @author guoyr
	 */
	private void goBackAction() {
		if (null != expertEvaluationListener) {
			expertEvaluationListener.complete();
		}
	}
	
	/**
	 * @Description: 刷新项目信息
	 * @author guoyr
	 */
	private void refreshAction() {
		tenderProject = tenderProjectService.getById(tenderProject.getId());
//		if(StringUtils.isNotEmpty(tenderProject.getParentId())){
//			TenderProject parent = tenderProjectService.getById(tenderProject.getParentId(), TenderProject.class);
//		}
		
		supplierList.clear();
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProject.getId(), evaluationStep);
		getDictValueList();
		refreshClearTenderItem();
	}
	
	/**
	 * @Description: 获得进行清标检查的项目
	 * @author guoyr
	 */
	private void getDictValueList() {
		cleanItemList.clear();
		
		// 获得进行清标检查的项目
		dictionarValueyList = dictionaryValueService.getCleanTenderItemList(tenderProject.getId());
		if (null != dictionarValueyList && dictionarValueyList.size() > 0) {
			cleanItemList.addAll(dictionarValueyList);
		}
	}
	
	/**
	 * @Description: 鼠标移动至菜单上的监听
	 * @author guoyr
	 * @param composite
	 * @param actionLabel
	 */
	class ActionMouseTrackListener extends MouseTrackAdapter {
		private Composite actionComposite;
		private CLabel actionLabel;
		
		public ActionMouseTrackListener(Composite actionComposite, CLabel actionLabel) {
			this.actionComposite = actionComposite;
			this.actionLabel = actionLabel;
		}
		
		public void mouseEnter(MouseEvent e) {
			if (null != curAction && !curAction.equals(actionLabel)) {
				actionLabel.setCursor(SwtConstants.CURSOR_HAND);
				actionComposite.setBackground(SWTResourceManager.getColor(255, 255, 255));
			}
		}
		
		public void mouseExit(MouseEvent e) {
			if (null != curAction && !curAction.equals(actionLabel)) {
				actionLabel.setCursor(null);
				actionComposite.setBackground(null);
			}
		}
	}
	
	/**
	 * @Description: 刷新清标项
	 * @author guoyr
	 */
	private void refreshClearTenderItem() {
		for (Control control : expert_list_composite.getChildren()) {
			control.dispose();
		}
		curCleanItemBtn = null;
		if (null != dictionarValueyList && dictionarValueyList.size() > 0) {
			for (final DictionaryValue dictValue : dictionarValueyList) {
				final CleanItemButton label = new CleanItemButton(expert_list_composite, SWT.NONE);
				
				label.setText(" " + dictValue.getDictValueName());
				label.setData(dictValue.getDictValueName());
				// 启动验程去验证该项检查是否已经完成
				
				final int count = checkResultService.getQueryCount(tenderProject.getId(), expertUserId, dictValue.getId());
				
				final boolean checkComplate = count > 0;
				if (checkComplate) {
					cleanItemList.remove(dictValue);
					label.setComplate(true);
				}
				label.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent e) {
						loadCleanItemResult(label, dictValue, checkComplate);
					}
				});
				if (null == curCleanItemBtn) {
					curCleanItemBtn = label;
				}
			}
			final CleanItemButton resultLabel = new CleanItemButton(expert_list_composite, SWT.NONE);
			resultLabel.setText(evaluationStep.getStepName() + "结果");
			resultLabel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseUp(MouseEvent e) {
					loadCleanItemResult(resultLabel, null, false);
				}
			});
			expert_list_composite.layout();
			if (null != curCleanItemBtn) {
				curCleanItemBtn.notifyListeners(SWT.MouseUp, null);
			}
		}
	}
	
	/**
	 * @Description: 加载清标项的清标结果
	 * @author guoyr
	 * @param dictValue
	 */
	private void loadCleanItemResult(final CleanItemButton cleanItemBtn, final DictionaryValue dictValue, boolean checkComplate) {
		if (null != dictValue) {
			if (checkComplate) {
				cleanItemList.remove(dictValue);
			}
			if (null == contentCompositeMap.get(dictValue.getId())) {
				contentComposite.setData("tenderProject", tenderProject);
				contentComposite.setData("evaluationStep", evaluationStep);
				contentComposite.setData("dictionaryValue", dictValue);
				contentComposite.setData("expertUserId", expertUserId);
				
				BusinessClearTenderItemComposite composite = new BusinessClearTenderItemComposite(contentComposite, SWT.NONE);
				composite.addCallBack(new CallBack() {
					public void after() {
						cleanItemList.remove(dictValue);
						Display.getDefault().syncExec(new Runnable() {
							public void run() {
								if (!cleanItemBtn.isDisposed()) {
									cleanItemBtn.setComplate(true);
								}
							}
						});
					}
				});
				// 如果已经完成清标，则不重新清标，只加载结果
				composite.startCleanTender(checkComplate ? false : true, billItemCache);
				stackLayout.topControl = composite;
				contentCompositeMap.put(dictValue.getId(), composite);
				
			} else {
				stackLayout.topControl = contentCompositeMap.get(dictValue.getId());
			}
		} else {
			if (cleanItemList.size() > 0) {
				MessageDialog.openWarning(getShell(), "请先进行" + cleanItemList.get(0).getDictValueName() + "！");
				return;
			}
			CleanTenderResultKtableComposite composite = new CleanTenderResultKtableComposite(contentComposite, SWT.NONE);
			composite.setCleanTenderListener(new CleanTenderListener() {
				public void complate() {
					rebackAction();
				};
			});
			stackLayout.topControl = composite;
			cleanItemBtn.setComplate(true);
		}
		if (null != curCleanItemBtn && !curCleanItemBtn.isDisposed()) {
			curCleanItemBtn.setSelection(false);
		}
		curCleanItemBtn = cleanItemBtn;
		curCleanItemBtn.setSelection(true);
		contentComposite.layout();
		
	}
	
	/**
	 * @Description: 查看工程量清单
	 * @author guoyr
	 */
	private void viewBoqAction() {
		ViewBoqDialog dialog = new ViewBoqDialog(getShell(), tenderProject, null, evaluationStep);
		dialog.open();
	}
	
	/**
	 * @Description: 返回
	 * @author guoyr
	 */
	private void rebackAction() {
		if (null != expertEvaluationListener) {
			// 刷新专家评审节点
			expertEvaluationListener.complete();
		}
	}
	
	public void dispose() {
		imageResolver.disposite();
		super.dispose();
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	protected void checkSubclass() {}
}
