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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ImageButton;
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.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.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.evaluation.step.composite.AiEvalLeftButton;
import com.gx.obe.message.Messages;
import com.swtdesigner.SWTResourceManager;

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

//	private Composite composite;
	private AiEvalLeftButton groupMenuBtn;
	private ImageButton curBtn;
	private Composite curComposite;
	private Composite composite;
	private Composite messageComposite;
	private Label lblNewLabel_1;
	private Listener deactivateListener;
//	private  int process;
//	private String needTime;

	/**
	 * Create the shell.
	 * @param display
	 */
	public ShowAiProcessShell(final Shell shell, AiEvalLeftButton shortMenu, int process, String needTime) {
		super(shell, SWT.NO_TRIM | SWT.TOP);
		initStruct(shortMenu, process, needTime, "标书相似度分析进度", "   预计还需要:");
	}
	
	public ShowAiProcessShell(final Shell shell, AiEvalLeftButton shortMenu, int process, String needTime, String title, String todo) {
		super(shell, SWT.NO_TRIM | SWT.TOP);
		initStruct(shortMenu, process, needTime, title, todo);
	}
	
	private void init(){
		
		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);
		composite.addMouseTrackListener(mouseExit);
		composite.addMouseTrackListener(mouseExit);
//		SwtUtils.setCircleRegion(this, 3);
		
		
	}

	@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(480, 68);

	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	private void initStruct(AiEvalLeftButton shortMenu, int process, String needTime, String title, String todo) {
		this.groupMenuBtn = shortMenu;
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		setBackground(shortMenu.getActiveBackgroundColor());
		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());
		
		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, 2));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginWidth = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		Label labe = new Label(composite, SWT.None);
		labe.setText(title);
		messageComposite = new Composite(composite, SWT.NONE);
		messageComposite.setLayout(new GridLayout(4, false));
		messageComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
//		messageComposite.setVisible(false);
		
		Label lblNewLabel = new Label(messageComposite, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText(Messages.ShowViewAiProcessDialog_lblNewLabel_text);
		
		lblNewLabel_1 = new Label(messageComposite, SWT.NONE);
		lblNewLabel_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblNewLabel_1.widthHint = 45;
		lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
		lblNewLabel_1.setText(process + "%");
		
		Label lblNewLabel_2 = new Label(messageComposite, SWT.NONE);
		lblNewLabel_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText(todo);
		
		Label lblNewLabel_3 = new Label(messageComposite, SWT.NONE);
		lblNewLabel_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_3.setText(needTime);
		
		ProgressBar progressBar = new ProgressBar(composite, SWT.NONE);
		progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		progressBar.setSelection(process);
		init();
		createContents();
	}
}