﻿using UnityEngine;
using System.Collections;
using System;

public class InputFlow : uLink.MonoBehaviour {
	/// <summary>
	/// name of the Unity game scene to be run on the Uniquitous server
	/// </summary>
	public string InitializedGameScene; 

	/// <summary>
	/// input value for horizontal and vertical axis
	/// </summary>
    public float serverCurrentHInput = 0f; 
	public float serverCurrentVInput = 0f; 

	/// <summary>
	/// position of the cursor on the client's screen
	/// </summary>
	public Vector3 serverCurrentMousePos; 

	/// <summary>
	/// exact position of the "fake" cursor on the Uniquitous server
	/// </summary>
	private Vector3 pos;

	/// <summary>
	/// boolean value indicates the status of left mouse click
	/// </summary>
	public bool serverMouseButtonDown;

	/// <summary>
	/// default value for key input if there is no key strokes sent from the client
	/// </summary>
	public string clientKeyInput = "None";

	/// <summary>
	/// variables used by customised GUI provided by Uniquitous
	/// </summary>
	public bool showMenu, showHelp = true, showBack;
	Rect _button1, _backButton, _menuArea, _textField;
	int colorIndex1 = 0, colorIndex2 = 0, colorIndex3 = 0;
	bool textMode = false;
	string content, stringToEdit="";

	/// <summary>
	/// Parameter referenced by ImageFlow script to change the JPEG encoding quality factor
	/// </summary>
	public int qualityFactor;


	// Use this for initialization
	void Start () {	
	  
	    /*load the game scene into the running scene of Uniquitous*/
		Application.LoadLevelAdditive(InitializedGameScene);

		/*rect property for each customized GUI element provided by Uniquitous*/
		_menuArea = new Rect(10,Screen.height/6,Screen.width/6, Screen.height/2);
		_button1 = new Rect(10,Screen.height/6+20,Screen.width/6,Screen.height/12);
		_textField = new Rect(10,Screen.height/6+Screen.height/4,Screen.width/6,Screen.height/12);
		_backButton = new Rect(10,Screen.height/6+Screen.height/2,Screen.width/6,Screen.height/12);
		content = "Menu";

	}

	/// <summary>
	/// Raises the GU event.
	/// </summary>
	void OnGUI(){
	 if(uLink.Network.isServer){
		
	  /*Customised GUI provided by Uniquitous*/
   	  if(showMenu){
	  
		GUI.depth = 1;
		GUI.Box(_menuArea,content);
		
		if(showHelp){
		  /*Button1*/
		  switch(colorIndex1){
				case 0: GUI.color = Color.blue; break;
				case 1: GUI.color = Color.yellow; break;
				case 2: GUI.color = Color.gray; break;
				default: break;
		 
		  }					
			GUI.Button(_button1,"Button1");
			  if(_button1.Contains(pos)){
				colorIndex1 = 1;
				if(serverMouseButtonDown){
					colorIndex1 = 2;
					content = "Button1";
					showHelp = false;
					showBack = true;
					
				}
			}
			else{
				 colorIndex1 = 0;
			}

		}

		/*text field*/
		switch(colorIndex2){
		case 0: GUI.color = Color.blue; break;
		case 1: GUI.color = Color.yellow; break;
		case 2: GUI.color = Color.gray; break;
		default: break;
			
		}
		GUI.SetNextControlName ("MyTextField");
		stringToEdit = GUI.TextField(_textField, stringToEdit, 25);
		if(_textField.Contains(pos)){
		  
		  if(serverMouseButtonDown){
			GUI.FocusControl ("MyTextField");
			colorIndex2 = 1;
			textMode = true;
		  }	          

		}
		else{
		  if(serverMouseButtonDown){
			colorIndex2 = 0;
			GUI.SetNextControlName("");
			GUI.FocusControl("");
			textMode = false;
		  }
		}
		
		/*back button*/
		 if(showBack){
		  switch(colorIndex3){
				case 0: GUI.color = Color.blue; break;
				case 1: GUI.color = Color.yellow; break;
				case 2: GUI.color = Color.gray; break;
				default: break;
		
		   }
			GUI.Button(_backButton,"Back");
			if(_backButton.Contains(pos)){
			   colorIndex3 = 1;
				if(serverMouseButtonDown){
					colorIndex3 = 2;
					content = "Menu";
					showHelp = true;
					showBack = false;
				}
			}
			else{
				colorIndex3 = 0;	
			}
			
	     }




	  }

	
	 }

	}


	void FixedUpdate(){
	  if(uLink.Network.isServer){
		   /*text mode is enabled when the textfield is active*/
			 if(!textMode){
		      if(clientKeyInput == "Space"){
				
		    	if(showMenu)
		    	{showMenu = false;}
				else
				{showMenu = true;}
				clientKeyInput = "None";	
		      }
			 }
			else{
			  if(clientKeyInput!="None"){
					if(clientKeyInput=="Backspace"){					
						stringToEdit = stringToEdit.Remove(stringToEdit.Length-1);
					}
					else{
				     stringToEdit = stringToEdit + clientKeyInput;
				     
					}
					clientKeyInput = "None";
			  }
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
	 
		if(uLink.Network.isServer){

			
			   pos = new Vector3(0,0,0);
			   /*update the position of the "fake" cursor on the server*/
			   pos.x = serverCurrentMousePos.x;
			   pos.y = Screen.height - serverCurrentMousePos.y;

		  
	     }
	}

	/// <summary>
	/// RPC method used to transmit player inputs from the client
	/// </summary>
	[RPC]
    void SendInput(float HInput, float VInput, Vector3 mouseInput, int mouseDown)
    {  
            serverCurrentHInput = HInput;
            serverCurrentVInput = VInput;
		    serverCurrentMousePos = mouseInput;
		    serverMouseButtonDown = (mouseDown == 1) ? true : false;
		    
    }

	/// <summary>
	/// RPC method used to transmit player inputs from the client
	/// </summary>
	[RPC]
	void SendInput2(string keyPressed){
		clientKeyInput = keyPressed;
	}
 
  
	
}
