package com.gx.obe.management.composite;

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
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.action.bean.Action;
import com.gx.obe.action.listener.ActionListener;
import com.gx.obe.action.utils.ActionUtil;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.management.action.LoadEvaluationResultCommonAction;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.ProjectRuleService;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评标结果
 * @author guoyr
 */
public class TenderProjectEvaluationResultTabComposite extends Composite implements ActionListener{

	private Composite loadComposite;
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectService tenderProjectService = new TenderProjectService();
	private Composite eTabFolder;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private List<EvaluationStep> nodeList ;
	private ProjectRuleService projectRuleService ;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private Action curAction;
	private String showResult = null;
	private Action projectAction;
	private Menu menu;
	private ImageButton curStepImage;
	private Map<String, ImageButton> evaluationStepItemMap;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TenderProjectEvaluationResultTabComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		tenderProjectEntity = projectAction.getTenderProject();
		menu = projectAction.getMenu();
		showResult = (String)parent.getData("result");
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		eTabFolder = new Composite(this, SWT.NONE);
		RowLayout rl_eTabFolder = new RowLayout(SWT.HORIZONTAL);
		rl_eTabFolder.justify = false;
		rl_eTabFolder.fill = true;
		rl_eTabFolder.marginLeft = 0;
		rl_eTabFolder.marginWidth = 1;
		rl_eTabFolder.marginRight = 0;
		rl_eTabFolder.marginHeight = 0;
		rl_eTabFolder.marginBottom = 0;
		rl_eTabFolder.marginTop = 1;
		rl_eTabFolder.spacing = 0;
		eTabFolder.setLayout(rl_eTabFolder);
		eTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		eTabFolder.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR, 0, 0));
//		eTabFolder.setBackground(ColorConstants.BORDER_LINE_COLOR);
//		eTabFolder.setBackgroundMode(SWT.INHERIT_FORCE);
		
		Composite topComposite = new Composite(this, SWT.NONE);
		topComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_topComposite = new GridLayout(1, false);
		gl_topComposite.marginBottom = 1;
		gl_topComposite.marginRight = 1;
		gl_topComposite.marginLeft = 1;
		gl_topComposite.horizontalSpacing = 0;
		gl_topComposite.marginHeight = 0;
		gl_topComposite.marginWidth = 0;
		gl_topComposite.verticalSpacing = 0;
		topComposite.setLayout(gl_topComposite);
		
		loadComposite = new Composite(topComposite, SWT.NONE);
		loadComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		loadComposite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		FillLayout fl_loadComposite = new FillLayout(SWT.HORIZONTAL);
		fl_loadComposite.marginWidth = 10;
		fl_loadComposite.marginHeight = 5;
		loadComposite.setLayout(fl_loadComposite);
		GridData gd_loadComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_loadComposite.heightHint = 300;
		loadComposite.setLayoutData(gd_loadComposite);
//		new Label(topComposite, SWT.NONE);
		
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		projectRuleService = new ProjectRuleService();
		tenderProjectService = new TenderProjectService();
		tenderProjectEntity = tenderProjectService.getById(tenderProjectEntity.getId());
		tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		
		
		final ExpertEvaluationListener expertEvaluationListener = new ExpertEvaluationListener(){
			public void complete() {
				if(null != curAction){
					curAction.run();
					refreshEvaluationStepStatus();
				}
			}
			public void refresh(){
				refreshEvaluationStepStatus();
			}
		};
		loadComposite.setData("tenderProjectEntity", tenderProjectEntity);
		nodeList = evaluationStepService.getEvaluationStepList(tenderProjectEntity.getId());
		if(null != nodeList && nodeList.size() > 0){
			evaluationStepItemMap = new HashMap<String, ImageButton>();
			int  index = 0;
			for(final EvaluationStep node : nodeList){
				ImageButton baseInfoItem = new ImageButton(eTabFolder, SWT.NONE);
				baseInfoItem.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
				baseInfoItem.setForeground(ColorConstants.NOTE_TIPS_COLOR);
				baseInfoItem.setSelectionForeground(ResourceManager.getColor(SWT.COLOR_WHITE));
				baseInfoItem.setBorderColor(SWTResourceManager.getColor(224, 224, 224));
				baseInfoItem.setBackgroundColor(SWTResourceManager.getColor(247, 247, 247));
//				baseInfoItem.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tab_default.png"));
				baseInfoItem.setSelectionBgColor(SWTResourceManager.getColor(60, 138, 226));;
//				baseInfoItem.setSelectionBgImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tab_selected.png"));
				baseInfoItem.setTopMargin(10);
				baseInfoItem.setBottomMargin(10);
				baseInfoItem.setRightMargin(15);
				baseInfoItem.setLeftMargin(15);
				baseInfoItem.setTopBorderVisible(false);
				baseInfoItem.setLeftBorderVisible(false);
				baseInfoItem.setText(node.getStepName());
				final Action action = ActionUtil.getAction(LoadEvaluationResultCommonAction.class.getName());
				action.setMenu(menu);
				action.setTenderProject(tenderProjectEntity);
				action.setTenderProjectRule(tenderProjectRuleEntity);;
				action.setData("result", showResult);
				action.setData("tenderProjectRuleEntity", tenderProjectRuleEntity);
				action.setData("expertEvaluationListener", expertEvaluationListener);
				action.addActionListener(this);
				action.setData("evaluationStep", node);
				baseInfoItem.addMouseListener(new MouseAdapter() {
					public void mouseUp(MouseEvent e) {
						if(null != curStepImage && !curStepImage.isDisposed()) {
							curStepImage.setSelection(false);
						}
						curStepImage = baseInfoItem;
						curStepImage.setSelection(true);
						curAction = action;
						refreshEvaluationStepStatus();
						action.run();
					}
				});
				
				evaluationStepItemMap.put(node.getId(), baseInfoItem);
				if(index == 0){
					curStepImage = baseInfoItem;
				}
				index++;
			}
//			
//			ETabItem baseInfoItem = new ETabItem(eTabFolder, SWT.NONE);
//			baseInfoItem.setRightMargin(15);
//			baseInfoItem.setLeftMargin(15);
//			baseInfoItem.setText("评（废）标意见");
//			final Action action = ActionUtil.getAction(LoadEvaluationOpinionAction.class.getName());
//			action.setTenderProject(tenderProjectEntity);
//			action.addActionListener(this);
//			baseInfoItem.addSelectionListener(new ETabFolder.SelectionListener() {
//				public void selected(MouseEvent e) {
//					curAction = action;
//					action.run();
//				}
//			});
			
			if(null != curStepImage && !curStepImage.isDisposed()){
				curStepImage.notifyListeners(SWT.MouseUp, null);
			}
		}
	}
	
	private void refreshEvaluationStepStatus(){
		nodeList = evaluationStepService.getEvaluationStepList(tenderProjectEntity.getId());
		if(null != nodeList && nodeList.size() > 0 && null != evaluationStepItemMap){
			boolean refresh = false;
			for(final EvaluationStep node : nodeList){
				ImageButton tabItem = evaluationStepItemMap.get(node.getId());
				if(null != tabItem && !tabItem.isDisposed()){
					if(null != node.getStepStatus() && node.getStepStatus().equals(CommonEnum.COMPLETE)){
						if(null == tabItem.getImage()){
							tabItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/green/ok.png"));
							refresh =  true;
						}
					}else if(null != tabItem.getImage()){
						tabItem.setImage(null);
						refresh =  true;
					}
				}
			}
			if(refresh){
				eTabFolder.layout();
			}
		}
	}

	public void onLoad(Action action) {
		for(Control control : loadComposite.getChildren()){
			control.dispose();
		}
	}

	public void loadComposite(Action action, Composite composite) {
		loadComposite.layout();
	}

	public Composite getParentComposite() {
		return loadComposite;
	}
	@Override
	protected void checkSubclass() {
	}

}
