/**
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() {
		if(targetRegion==null){
			return;
		}
		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){
            	// add native keyboard listener
            	GlobalKeyboardListeners globalKeyboardListener=new GlobalKeyboardListeners(targetRegion,slideShape.getText(),desktopEvent);
            	GlobalScreen.getInstance().addNativeKeyListener(globalKeyboardListener);
            	// display canvas around the target until the user performs the appropriate action
            	canvas.displayWhile(globalKeyboardListener);
            	// Remove native keyboard listener
            	GlobalScreen.getInstance().removeNativeKeyListener(globalKeyboardListener);
            	
            }
            else if(this.desktopEvent==DesktopEvent.LEFT_CLICK ||
            		this.desktopEvent==DesktopEvent.RIGHT_CLICK ||
            		this.desktopEvent==DesktopEvent.DOUBLE_CLICK ||
            		this.desktopEvent==DesktopEvent.DRAG_N_DROP){
            	// add native keyboard listener
            	GlobalMouseListeners globalMouseListener=new GlobalMouseListeners(targetRegion,desktopEvent);
            	GlobalScreen.getInstance().addNativeMouseListener(globalMouseListener);
            	// display canvas around the target until the user performs the appropriate action
            	canvas.displayWhile(globalMouseListener);
            	// Remove native mouse listener
            	GlobalScreen.getInstance().removeNativeMouseListener(globalMouseListener);
            }
            else if(this.desktopEvent==DesktopEvent.LAUNCH_BROWSER){
            	//TODO:
            }
            else if(this.desktopEvent==DesktopEvent.EXIST||this.desktopEvent==DesktopEvent.NOT_EXIST){
            	//TODO:
            }
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem running the tutorial mode.");
            System.err.println(ex.getMessage());
            System.exit(1);
        }
	}
}

@@ -1,14 +1,30 @@
 /**%0A
-Khalid
+@author Khalid Alharbi
 %0A*/%0A
@@ -57,16 +57,40 @@
 sikuli;%0A
+import java.awt.Color;%0A%0A
 import o
@@ -726,16 +726,244 @@
 hape;%0A%09%0A
+%09/**%0A%09 * A slide tutorial to be used in tutorial mode and help mode%0A%09 * @param targetRegion%0A%09 * @param slideShape%0A%09 * @param desktopEvent%0A%09 * @param observable UI observable used to observe tutorial mode navigation status.%0A%09 */%0A
 %09public 
@@ -1909,16 +1909,33 @@
 tRegion)
+.withLineWidth(5)
 ;%0A%09%09int 
@@ -2194,24 +2194,88 @@
 splayName())
+.withColor(Color.black).withFontSize(Constants.Label_Font_Size);
 ;%0A        Sy
@@ -3427,32 +3427,29 @@
  add native 
-keyboard
+mouse
  listener%0A  
@@ -4229,16 +4229,16 @@
 n ex) %7B%0A
-
         
@@ -4281,16 +4281,19 @@
 problem 
+in 
 running 

change font size and line width in tutorial mode