/**
Khalid
*/
package org.sikuli.slides.sikuli;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.sikuli.api.DesktopScreenRegion;
import org.sikuli.api.ScreenRegion;
import org.sikuli.api.visual.Canvas;
import org.sikuli.api.visual.DesktopCanvas;
import org.sikuli.slides.listeners.GlobalKeyboardListeners;
import org.sikuli.slides.listeners.GlobalMouseListeners;
import org.sikuli.slides.shapes.SlideShape;
import org.sikuli.slides.utils.Constants;
import org.sikuli.slides.utils.Constants.DesktopEvent;

public class SlideTutorial {
	private ScreenRegion targetRegion;
	private Constants.DesktopEvent desktopEvent;
	private SlideShape slideShape;
	
	public SlideTutorial(ScreenRegion targetRegion, SlideShape slideShape, Constants.DesktopEvent desktopEvent){
		this.targetRegion=targetRegion;
		this.slideShape=slideShape;
		this.desktopEvent=desktopEvent;
	}
	
	private String getActionDisplayName(){
		if(desktopEvent==DesktopEvent.LEFT_CLICK){
			return "Click here";
		}
		else if(desktopEvent==DesktopEvent.DOUBLE_CLICK){
			return "Double click here";
		}
		else if(desktopEvent==DesktopEvent.RIGHT_CLICK){
			return "Right click here";
		}
		else if(desktopEvent==DesktopEvent.DRAG_N_DROP){
			if(slideShape.getOrder()==0)
				return "Drag this";
			else if(slideShape.getOrder()==1)
				return "and drop it here";
		}
		else if(desktopEvent==DesktopEvent.KEYBOARD_TYPING){
			return "Type: "+ slideShape.getText()+" here";
		}
		return "";
	}
	
	public void performTutorialSlideAction() {
		Canvas canvas=new DesktopCanvas();
		canvas.addBox(targetRegion);
		int x=targetRegion.getBounds().x;
		int y=targetRegion.getBounds().y;
		int w=targetRegion.getBounds().width;
		int h=targetRegion.getBounds().height;
		ScreenRegion labelRegion=new DesktopScreenRegion(x,y-h,w,h);
		canvas.addLabel(labelRegion, getActionDisplayName());
        System.out.println("Waiting for the user to pefrom "+this.desktopEvent.toString()+" on the highlighted target.");
		try {
        	if(!GlobalScreen.isNativeHookRegistered()){
        		GlobalScreen.registerNativeHook();
        	}
            if(this.desktopEvent==DesktopEvent.KEYBOARD_TYPING){
            	GlobalKeyboardListeners globalKeyboardListener=new GlobalKeyboardListeners(targetRegion,slideShape.getText(),desktopEvent);
            	GlobalScreen.getInstance().addNativeKeyListener(globalKeyboardListener);
            	canvas.displayWhile(globalKeyboardListener);
            }
            else{
            	GlobalMouseListeners globalMouseListener=new GlobalMouseListeners(targetRegion,desktopEvent);
            	GlobalScreen.getInstance().addNativeMouseListener(globalMouseListener);
            	canvas.displayWhile(globalMouseListener);
            }
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem running the tutorial mode.");
            System.err.println(ex.getMessage());
            System.exit(1);
        }

		
	}
}

@@ -2178,16 +2178,61 @@
 YPING)%7B%0A
+            %09// add native keyboard listener%0A
         
@@ -2463,331 +2463,1325 @@
    %09
-canvas.displayWhile(globalKeyboardListener);%0A            %7D%0A            else%7B%0A            %09GlobalMouseListeners globalMouseListener=new GlobalMouseListeners(targetRegion,desktopEvent);%0A            %09GlobalScreen.getInstance().addNativeMouseListener(globalMouseListener);%0A            %09canvas.displayWhile(globalMouseListener);
+// display canvas around the target until the user performs the appropriate action%0A            %09canvas.displayWhile(globalKeyboardListener);%0A            %09// Remove native keyboard listener%0A            %09GlobalScreen.getInstance().removeNativeKeyListener(globalKeyboardListener);%0A            %09%0A            %7D%0A            else if(this.desktopEvent==DesktopEvent.LEFT_CLICK %7C%7C%0A            %09%09this.desktopEvent==DesktopEvent.RIGHT_CLICK %7C%7C%0A            %09%09this.desktopEvent==DesktopEvent.DOUBLE_CLICK %7C%7C%0A            %09%09this.desktopEvent==DesktopEvent.DRAG_N_DROP)%7B%0A            %09// add native keyboard listener%0A            %09GlobalMouseListeners globalMouseListener=new GlobalMouseListeners(targetRegion,desktopEvent);%0A            %09GlobalScreen.getInstance().addNativeMouseListener(globalMouseListener);%0A            %09// display canvas around the target until the user performs the appropriate action%0A            %09canvas.displayWhile(globalMouseListener);%0A            %09// Remove native mouse listener%0A            %09GlobalScreen.getInstance().removeNativeMouseListener(globalMouseListener);%0A            %7D%0A            else if(this.desktopEvent==DesktopEvent.LAUNCH_BROWSER)%7B%0A            %09//TODO:%0A            %7D%0A            else if(this.desktopEvent==DesktopEvent.EXIST%7C%7Cthis.desktopEvent==DesktopEvent.NOT_EXIST)%7B%0A            %09//TODO:
 %0A   
@@ -4014,12 +4014,8 @@
    %7D
-%0A%0A%09%09
 %0A%09%7D%0A

fix mulitiple threads running in tutorial mode