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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.ImageButton;
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.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class MenuGroupTipShell extends Shell {

//	private Composite composite;
	private Listener deactivateListener;
	private EvalLeftGroupButton groupMenuBtn;
	private Composite menuItemComposite;
	private ImageButton curBtn;
	private Composite curComposite;
	private Label itemTitleLabel;
	private Composite composite;
	private Label groupTitleLabel;

	/**
	 * Create the shell.
	 * @param display
	 */
	public MenuGroupTipShell(Shell shell, EvalLeftGroupButton shortMenu) {
		super(shell, SWT.NO_TRIM | SWT.TOP);
		this.groupMenuBtn = shortMenu;
//		super(parent.getShell(), SWT.NO_TRIM | SWT.ON_TOP | SWT.TOOL);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				removeListener(SWT.Deactivate, deactivateListener);
			}
		});
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		setBackground(shortMenu.getActiveBackgroundColor());
//		setBackground(shortMenu.getBackground());
		setBackgroundMode(SWT.INHERIT_FORCE);
		
		curComposite = new Composite(this, SWT.NONE);
		curComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 2));
		GridLayout gl_curComposite = new GridLayout(1, false);
		gl_curComposite.marginHeight = 0;
		gl_curComposite.marginWidth = 0;
		gl_curComposite.marginRight = 5;
		curComposite.setLayout(gl_curComposite);
		curComposite.setBackground(shortMenu.getActiveBackgroundColor());
//		curComposite.setBackground(SWTResourceManager.getColor(123, 13, 15));
		
		curBtn = new ImageButton(curComposite, SWT.NONE);
		curBtn.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		curBtn.setImage(shortMenu.getImage());
//		curBtn.setTopMargin(3);
//		curBtn.setBottomMargin(3);
		curBtn.setLeftMargin(5);
		curBtn.setRightMargin(5);
		
		
		composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 1;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		groupTitleLabel = new Label(composite, SWT.CENTER);
		groupTitleLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		groupTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		groupTitleLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		groupTitleLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		
		menuItemComposite = new Composite(composite, SWT.NONE);
		menuItemComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		RowLayout rl_menuItemComposite = new RowLayout(SWT.HORIZONTAL);
		rl_menuItemComposite.marginHeight = 0;
		rl_menuItemComposite.marginWidth = 0;
		rl_menuItemComposite.center = true;
		rl_menuItemComposite.justify = true;
		menuItemComposite.setLayout(rl_menuItemComposite);
		
		itemTitleLabel = new Label(composite, SWT.CENTER | SWT.WRAP);
		itemTitleLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		itemTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		itemTitleLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		itemTitleLabel.setForeground(ColorConstants.FONT_BLUE_COLOR);
		createContents();
		init();
	}
	private void init(){
		if(StringUtils.hasLength(groupMenuBtn.getTitle())) {
			groupTitleLabel.setText(groupMenuBtn.getTitle());
		}else {
			WidgetUtils.setControlVisible(groupTitleLabel, false);
		}
		deactivateListener = new Listener(){
			public void handleEvent(Event arg0) {
				close();
			}
		};
		addListener(SWT.Deactivate, deactivateListener);
		MouseTrackAdapter mouseExit = new MouseTrackAdapter() {
			public void mouseExit(MouseEvent e) {
				super.mouseExit(e);
				Point mousePoint = SwtUtils.getMouseLoaction();
				Point p = Display. getDefault ().map(composite, null , 0,0);
				Point p2 = Display. getDefault ().map(curComposite, null , 0,0);
				
				Rectangle rectangle1 = new Rectangle(p2.x + 1, p2.y + 0, curComposite.getBounds().width, curComposite.getBounds().height -0);
				Rectangle rectangle2 = new Rectangle(p.x + 1, p.y + 0, composite.getBounds().width -0, composite.getBounds().height -2);
//				System.out.println(rectangle1 +"\t"+rectangle2 +"\t"+ mousePoint +"\t"+ rectangle1.contains(mousePoint) +"\t"+rectangle2.contains(mousePoint));
				if(rectangle1.contains(mousePoint) || rectangle2.contains(mousePoint)) {
				}else {
					close();
				}
			}
		};
		
//		addMouseTrackListener(mouseExit);
		curBtn.addMouseTrackListener(mouseExit);
		curComposite.addMouseTrackListener(mouseExit);
		menuItemComposite.addMouseTrackListener(mouseExit);
		composite.addMouseTrackListener(mouseExit);
//		SwtUtils.setCircleRegion(this, 3);
		
		for(LeftMenuEnum menuItem : groupMenuBtn.getMenuItemList()) {
			ImageButton label = new ImageButton(menuItemComposite, SWT.NONE);
			label.setTopMargin(5);
			label.setBottomMargin(5);
			label.setLeftMargin(5);
			label.setRightMargin(5);
			label.addMouseTrackListener(new MouseTrackAdapter() {
				public void mouseEnter(MouseEvent e) {
					super.mouseEnter(e);
					itemTitleLabel.setText(ObjectUtils.getObjString(menuItem.getTitle()));
					composite.layout();
				}
			});
			label.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
			label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			label.setBackgroundColor(groupMenuBtn.getBackground());
			label.setActiveBgColor(groupMenuBtn.getActiveBackgroundColor());
			label.setActiveForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			label.setSelectionBgColor(SWTResourceManager.getColor(77, 163, 19));
			label.setArcWidth(6);
			if(null != groupMenuBtn.getCurMenu()){
				if(menuItem.equals(groupMenuBtn.getCurMenu())) {
					label.setSelection(true);
					itemTitleLabel.setText(ObjectUtils.getObjString(menuItem.getTitle()));
				}
			}else {
				itemTitleLabel.setText(ObjectUtils.getObjString(menuItem.getTitle()));
			}
			label.setImage(menuItem.getImage());
			label.setEnabled(groupMenuBtn.getEnabled());
			if(groupMenuBtn.getEnabled()) {
				label.addMouseListener(new MouseAdapter() {
					public void mouseUp(MouseEvent e) {
						if(!groupMenuBtn.getEnabled()) return;
						LeftMenuMouseAdapter museListener = groupMenuBtn.getMouseListener(menuItem);
						if(null != museListener) {
							if(museListener.isClose()) {
								close();
							}
							if(museListener.isUseItemImage()) {
								groupMenuBtn.setImage(menuItem.getImage());
							}
							museListener.mouseUp(e);
						}
					}
				});
			}
		}
		
		curBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if(null != groupMenuBtn.getCurMenu()) {
					LeftMenuMouseAdapter museListener = groupMenuBtn.getMouseListener(groupMenuBtn.getCurMenu());
					if(null != museListener) {
						museListener.mouseUp(e);
					}
				}
			}
		});
	}
	

	@Override
	public void open() {
		this.setMinimumSize(250, 80);
		this.setAlpha(250);
		this.pack();
		Point point = groupMenuBtn.toDisplay(new Point(0,0));
		Region region = new Region();
		region.add(0, 0,curComposite.getBounds().width, curComposite.getBounds().height + 2);
		region.add(curComposite.getBounds().width + 0, 0, this.getBounds().width, this.getBounds().height);
		this.setRegion(region);
		region.dispose();
		setLocation(point.x , point.y);
		super.open();
	}

	
//	@Override
//	public void close() {
//		super.close();
//	}
	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("");
		setSize(200, 61);

	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
