package artistmetrics.actions;


import java.util.Map;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.w3c.dom.Document;

import artistmetrics.views.LogChangesGui;

public class User 
{
/****Variables****/
/*------------------------------------------------------------------------------------------------------*/
	public String username;
	private String password;
	private boolean loggedIn;
	private boolean connected;
	
	private Log userLog;
	private FocusTracker runFocus;
	private ServerConnection serverConnection;
	
    private IWorkbenchPage activePage;
    
    private static User usr = null;
/****Constructors****/	
/*------------------------------------------------------------------------------------------------------*/	

	public User(boolean server)
	{
        IWorkbench editors = PlatformUI.getWorkbench();
        IWorkbenchWindow window = editors.getActiveWorkbenchWindow();
        activePage = window.getActivePage();
        
		connected = false;
		loggedIn = true;
		userLog = new Log(".artistmetricslog");
		runFocus = new FocusTracker(this, false);
        new AFKTracker(this);
        new DebugTracker(this);
		if (server){
			try {
				serverConnection = new ServerConnection();
				connected = true;
			}
			catch(Exception e) {
				System.out.println(e.toString());
                server = false;
			}
		}
		
		User.usr = this;
	}
	
	public Document getMetrics()
	{
		return serverConnection.getAllMetrics();
	}
	
	public static User getSystemUser()
	{
		return usr;
	}

	public void writeToLog(String message)
	{
		userLog.write(message);
        
        try
        {
            if (activePage.findView("artistmetrics.views.LogChangesGui") != null)
            {
                LogChangesGui gui = (LogChangesGui) activePage.findView("artistmetrics.views.LogChangesGui");
                gui.printText(message);
            }
        }
        catch(Exception e)
        {
            //This can fail if this method is being called from another thread.
        }
	}
	
	public void sendMetric(String type, Map<String, String> data)
	{
		if (connected)
		{
			serverConnection.sendMetric(type, data);
		}
	}
	
	public void dispose() {
		runFocus.endTracking(this);
		writeToLog(runFocus.lastUpdatedTimer());
	}
	
/****Methods****/
	/***Setter***/
	public void setUser(String userName)
	{
		if(this.username == null && userName != null)
		{
			this.username = userName;
		}
	}
	public void setPass(String passWord)
	{
		if(this.password == null && passWord != null)
		{
			this.password = passWord;
		}
	}
	
	/***Getter***/
	public String getUser()
	{
		return username;
	}
	public boolean getLogInStates()
	{
		return loggedIn;
	}
	/*
	 *  To change user name you need to prove you are the user by having the password.
	 */
	public boolean changeUser(String userName,String passWord)
	{
		if(this.password.equals(passWord) && userName != null)
		{
			this.username = userName;
			return true;
		}
		else
			return false;
	}
	
	/*
	 *  To change the password you must have the old password 
	 */
	public boolean changePass(String passWord)
	{
		if(passWord != null && this.password != null && this.password.equals(passWord))
		{
			this.password = passWord;
			return true;
		}
		else
			return false;
	}
	
	/***Main***/
	// Only for testing the connection between user and timer
	public static void main(String [] args)
	 {
		 /* User us = new User("user","ff","pass");
		 //Valid log in
		 boolean bb = us.logIn("user", "pass");
		 System.out.println("Login states = " + bb); 
		 System.out.println("Logout states = " + us.logOut() );
		 //No valid log in
		 bb = us.logIn("userd", "pass");
		 System.out.println("Logistates = " + bb); 
		 System.out.println("Logout states = " + us.logOut() );
		 */
	 }

	public void selectionChanged(IAction action, ISelection selection) {
		if (runFocus.focusChanged(this)) {
			String update = runFocus.lastUpdatedTimer();

			if (update == null)
			{
				runFocus.focusChanged(this);
				return;
			}
			writeToLog(update);
		}
	}

	public boolean hasServer() {
		return connected;
	}
	
}
