package com.gx.obe.management.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.SwtConstants;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.listener.StartEvaluationListener;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.swtdesigner.SWTResourceManager;

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

	private ImageResolver imageResolver = new ImageResolver();
	private Shell shell;
	private EvaluationStep evaluationStep;
	private StartEvaluationListener listener;
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public StartEvaluationStepShell(Shell parentShell, EvaluationStep evaluationStep, StartEvaluationListener listener) {
		super(parentShell, SWT.NO_TRIM | SWT.CURSOR_HAND);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell = this;
		this.evaluationStep = evaluationStep;
		this.listener = listener;
		createContents();
		init();
	}
	
	private void init(){
		
		createShell();
		
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("");
		this.setToolTipText("开始"+evaluationStep.getStepName());
		setSize(450, 300);

	}

	
	@Override
	public void open() {
		this.setVisible(false);
		super.open();
	}

	private void createShell() {
		
		int r = 20;
		Region region = new Region(); 
		region.add(circle(r, r, r)); 
		
		final Image image = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/start.png");
//		final int imgX = (r*2 - image.getImageData().width)/2;
//		final int imgY = (r*2 - image.getImageData().height)/2;
		final int imgX = (region.getBounds().width - image.getImageData().width)/2 + 1;
		final int imgY = (region.getBounds().height - image.getImageData().height)/2 +2;
		this.setRegion(region);
		final Rectangle size = region.getBounds(); 
		this.setSize(size.width, size.height); 
		
		Listener shellListener = new Listener() {
			int startX, startY;
			
			public void handleEvent(Event e) {
				// 注入鼠标事件
				if (e.type == SWT.MouseDown && e.button == 1) {
					startX = e.x;
					startY = e.y;
				}
				if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
					Point p = shell.toDisplay(e.x, e.y);
					p.x -= startX;
					p.y -= startY;
					shell.setLocation(p);
				}
				if(e.type == SWT.MouseUp){
					if(!MessageDialog.openConfirm(getShell(), evaluationStep.getStepName(), "确认要开始["+evaluationStep.getStepName()+"]吗？", new String[]{"开始", "不开始"})){
						return;
					}
					imageResolver.disposite();
					shell.close();
					listener.start(evaluationStep);
				}
				if(e.type == SWT.MouseEnter){
					shell.setCursor(SwtConstants.CURSOR_HAND);
					shell.setAlpha(240);
				}
				if(e.type == SWT.MouseExit){
					shell.setCursor(SwtConstants.CURSOR_NO);
					shell.setAlpha(200);
				}
				if (e.type == SWT.Paint) {
					e.gc.setAlpha(-1);
					e.gc.drawImage(image, imgX, imgY);
				}
			}
		};
		shell.addListener(SWT.Paint, shellListener);
		shell.addListener(SWT.MouseUp, shellListener);
		shell.addListener(SWT.MouseEnter, shellListener);
		shell.addListener(SWT.MouseExit, shellListener);
	}
	
	private int[] circle(int r, int offsetX, int offsetY) {
		int[] polygon = new int[8 * r + 4];
		// x^2 + y^2 = r^2
		for (int i = 0; i < 2 * r + 1; i++) {
			int x = i - r;
			int y = (int) Math.sqrt(r * r - x * x);
			polygon[2 * i] = offsetX + x;
			polygon[2 * i + 1] = offsetY + y;
			polygon[8 * r - 2 * i - 2] = offsetX + x;
			polygon[8 * r - 2 * i - 1] = offsetY - y;
		}
		return polygon;
	}
	 
	@Override
	protected void checkSubclass() {
	}
}
