package org.jivesoftware.sparkimpl.plugin.idle;

import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.util.Timer;
import java.util.TimerTask;

import org.jivesoftware.Spark;
import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

public class UserIdlePlugin extends TimerTask implements Plugin {

	private final int CHECKTIME = 5;
	private double x = 0;
	private double y = 0;
	private boolean hasChanged = false;
	private int counter = 0;
	private LocalPreferences pref = SettingsManager.getLocalPreferences();
	private Presence latestPresence;
	
	@Override
	public boolean canShutDown() {

		return false;
	}

	@Override
	public void initialize() {
		 Timer timer = new Timer();
		 // Check all 5 secounds
		 timer.schedule  ( this, (1000 * 10), (1000 * CHECKTIME)  );
	}

	@Override
	public void shutdown() {
		
	}

	@Override
	public void uninstall() {
		
	}
	
	private void setIdle()
	{
		latestPresence = SparkManager.getWorkspace().getStatusBar().getPresence();
		Presence presence = new Presence(Presence.Type.available, Res.getString("status.away"), 1, Presence.Mode.away);			
		SparkManager.getSessionManager().changePresence(presence);
	}
	
	private void setOnline()
	{
		SparkManager.getSessionManager().changePresence(latestPresence);
	}
	
	@Override
	public void run() {
		if(pref.isIdleOn()){
			PointerInfo info = MouseInfo.getPointerInfo();
			//DecimalFormat format = new DecimalFormat("0.00");
			//System.out.println(format.format(info.getLocation().getY()).toString() + "-" + 7.24288464E8 + "-" + (info.getLocation().getY() == 7.24288464E8));
			//System.out.println(format.format(info.getLocation().getX()).toString() + "-" + 7.24288464E8 + "-" + (info.getLocation().getX() == 7.24288464E8));
			int automaticIdleTime = (pref.getIdleTime() * 60) / CHECKTIME;
	
			// Windows Desktop Lock
			if (Spark.isWindows()) {
				
				
				if (info != null)
				{
					if (info.getLocation().getX() > 50000000 || 
					    info.getLocation().getY() > 50000000) {
						if (!hasChanged) {
							System.out.println("Desktop Locked .. ");
							hasChanged = true;
							setIdle();
							y = info.getLocation().getY();
							x = info.getLocation().getX();
						}
					}
				}
				else
				{
					if (!hasChanged) {
						System.out.println("Desktop Locked .. ");
						hasChanged = true;
						setIdle();
						y = -1;
						x = -1;
					}				
				}
			}
		
			// Default Idle
			if (info != null)
			{
				if (x == info.getLocation().getX() && 
					y == info.getLocation().getY()) {
					if (counter > automaticIdleTime) {
						if (!hasChanged) {
							setIdle();
						}
						hasChanged = true;
					}
					counter++;
				} else {
					if (hasChanged) {
						setOnline();
						hasChanged = false;
					}
					counter = 0;
				}
				
				y = info.getLocation().getY();
				x = info.getLocation().getX();
			}
		}
	}
}

@@ -62,46 +62,157 @@
 awt.
-MouseInfo;%0Aimport java.awt.PointerInfo
+AWTEvent;%0Aimport java.awt.EventQueue;%0Aimport java.awt.MouseInfo;%0Aimport java.awt.PointerInfo;%0Aimport java.awt.Toolkit;%0Aimport java.awt.event.KeyEvent
 ;%0Aim
@@ -962,17 +962,16 @@
 own() %7B%0A
-%0A
 %09%09return
@@ -1022,17 +1022,19 @@
 ze() %7B%0A%09
-%09
+   
  Timer t
@@ -1054,17 +1054,19 @@
 mer();%0A%09
-%09
+   
  // Chec
@@ -1083,17 +1083,19 @@
 counds%0A%09
-%09
+   
  timer.s
@@ -1147,16 +1147,48 @@
 ME)  );%0A
+%09%09 %0A%09    addGlobalListener();%09 %0A
 %09%7D%0A%0A%09@Ov
@@ -3244,11 +3244,392 @@
 %7D%0A%09%09%7D%0A%09%7D
+%0A%0A%09private void addGlobalListener()%0A%09%7B%0A%09    EventQueue e = Toolkit.getDefaultToolkit().getSystemEventQueue();%0A%09    e.push(new EventQueue()%0A%09    %7B%0A%09%09protected void dispatchEvent(AWTEvent event)%0A%09%09%7B%0A%09%09    if(event instanceof KeyEvent)%0A%09%09    %7B%0A%09%09%09counter = 0;%0A%09%09%09if (hasChanged) %7B%0A%09%09%09%09setOnline();%0A%09%09%09%09hasChanged = false;%0A%09%09%09%7D%0A%09%09    %7D%0A%09%09    super.dispatchEvent(event);%0A%09%09%7D%0A%09    %7D);%0A%09%7D
 %0A%7D%0A

fix SPARK-1063 - Spark is not reacting to keyboard events when setting the auto Away status