package com.gx.obe.evaluation.step.widgets;

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.widgets.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.shell.MenuTipShell;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;


public class EvalLeftGroupButton extends ImageButton{
	
	private List<LeftMenuEnum> groupMenu = new ArrayList<LeftMenuEnum>();
	private Map<LeftMenuEnum, LeftMenuMouseAdapter> menuListenerMap = new HashMap<LeftMenuEnum, LeftMenuMouseAdapter>();
	private MouseListener listener;
	private LeftMenuEnum curMenuEnum;
	private String title;
	public static Color BG = ColorConstants.SKIN_BG;
	public EvalLeftGroupButton(Composite parent, int style) {
		super(parent, SWT.CENTER|SWT.NONE);
		this.setTopMargin(3);
		this.setBottomMargin(3);
		this.setLeftMargin(5);
		this.setRightMargin(5);
		this.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/evaluaton_step/default.png"));
		
//		button.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
//		button.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		this.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		this.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
//		this.setBorderColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.setActiveBgColor(SWTResourceManager.getColor(255, 153, 0));
//		this.setActiveSurroundColor(SWTResourceManager.getColor(255, 153, 0));
		
		this.setActiveForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
//		this.setActiveSurroundColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.setSelectionBgColor(ColorConstants.SKIN_SELECTION_BG);
//		this.setSelectionBorderColor(SWTResourceManager.getColor(255, 153, 0));
		this.setArcWidth(6);
//		button.setText(Messages.Button_reduce);
//		button.setSurroundMargin(2);
//		button.setFillSurround(false);
		init();
	}

	public void setTitle(String title) {
		this.title = title;
		
	}
	private void init(){
		final EvalLeftGroupButton groupButton = EvalLeftGroupButton.this;
		addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if(groupMenu.size() > 1) {
					MenuGroupTipShell menuGroupTipShell = new MenuGroupTipShell(getShell(), groupButton);
					if(null != menuGroupTipShell && !menuGroupTipShell.isDisposed()){
						menuGroupTipShell.open();
						menuGroupTipShell.setVisible(true);
					}
				}else if(groupMenu.size() == 1) {
					groupButton.setToolTipText(groupMenu.get(0).getTitle());
					if(null == listener) {
						listener = new MouseAdapter() {
							public void mouseUp(MouseEvent e) {
								if(!groupButton.getEnabled()) return;
								LeftMenuMouseAdapter museListener = groupButton.getMouseListener(groupMenu.get(0));
								if(null != museListener) {
									if(museListener.isUseItemImage()) {
										groupButton.setImage(groupMenu.get(0).getImage());
									}
									museListener.mouseUp(e);
								}
							}
						};
						groupButton.addMouseListener(listener);
					}
					MenuTipShell menuTipShell = new MenuTipShell(getShell(), groupButton);
					if(null != menuTipShell && !menuTipShell.isDisposed()){
						menuTipShell.open();
						menuTipShell.setVisible(true);
					}
				}
			}
		});
	}
	
	public void addMenuItem(LeftMenuEnum leftMenuEnum, LeftMenuMouseAdapter listener) {
		groupMenu.add(leftMenuEnum);
		menuListenerMap.put(leftMenuEnum, listener);
	}
	
	public void setCurMenu(LeftMenuEnum leftMenuEnum) {
		this.curMenuEnum = leftMenuEnum;
		if(null != leftMenuEnum) {
			this.setImage(leftMenuEnum.getImage());
		}
	}
	public LeftMenuEnum getCurMenu() {
		return curMenuEnum;
	}
	public LeftMenuMouseAdapter getMouseListener(LeftMenuEnum leftMenuEnum) {
		return menuListenerMap.get(leftMenuEnum);
	}
	public List<LeftMenuEnum> getMenuItemList() {
		return groupMenu;
	}

	public String getTitle() {
		return title;
	}
}
