using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class StateManager : Photon.MonoBehaviour {

	private Vector3 currentColor;
	private Color myColor = new Color32(0, 38, 255, 255);
	public string hitTag;
	public string newColorName;
	public string oldColorName;
	private Color32 colorPlaceholder;
	public string currentTime;
	private string actorID;
	private int specialID;
	public UnityEngine.UI.Toggle toggleColor1;
	public UnityEngine.UI.Toggle toggleColor2;
	public UnityEngine.UI.Toggle toggleColor3;
	public UnityEngine.UI.Toggle toggleColor4;
	public UnityEngine.UI.Toggle toggleColor5;
	public UnityEngine.UI.Toggle toggleColor6;
	public UnityEngine.UI.Toggle toggleColor7;
	public UnityEngine.UI.Toggle toggleColor8;
	public UnityEngine.UI.Toggle toggleColor9;
	public UnityEngine.UI.Toggle toggleColor10;
	public UnityEngine.UI.Toggle toggleColor11;
	public UnityEngine.UI.Toggle toggleColor12;
	public UnityEngine.UI.Toggle toggleColor13;
	public UnityEngine.UI.Toggle toggleColor14;
	public UnityEngine.UI.Toggle toggleColor15;
	public UnityEngine.UI.Toggle toggleColor16;
	public UnityEngine.UI.Toggle toggleColor17;
	public UnityEngine.UI.Toggle toggleClear;
	public UnityEngine.UI.Toggle menuOn;
	public UnityEngine.UI.Text dataText;
	private int i = 0;
	private Vector3 whiteColor = new Vector3(1, 1, 1);


	void Start() {
		toggleColor1 = GameObject.Find ("ToggleColor1").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor2 = GameObject.Find ("ToggleColor2").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor3 = GameObject.Find ("ToggleColor3").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor4 = GameObject.Find ("ToggleColor4").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor5 = GameObject.Find ("ToggleColor5").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor6 = GameObject.Find ("ToggleColor6").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor7 = GameObject.Find ("ToggleColor7").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor8 = GameObject.Find ("ToggleColor8").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor9 = GameObject.Find ("ToggleColor9").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor10 = GameObject.Find ("ToggleColor10").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor11 = GameObject.Find ("ToggleColor11").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor12 = GameObject.Find ("ToggleColor12").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor13 = GameObject.Find ("ToggleColor13").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor14 = GameObject.Find ("ToggleColor14").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor15 = GameObject.Find ("ToggleColor15").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor16 = GameObject.Find ("ToggleColor16").GetComponent<UnityEngine.UI.Toggle> ();
		toggleColor17 = GameObject.Find ("ToggleColor17").GetComponent<UnityEngine.UI.Toggle> ();
		toggleClear = GameObject.Find ("ToggleClear").GetComponent<UnityEngine.UI.Toggle> ();
		menuOn = GameObject.Find ("ToggleMenu").GetComponent<UnityEngine.UI.Toggle>();
		dataText = GameObject.Find ("DataText").GetComponent<UnityEngine.UI.Text>();

	}
	void OnPhotonPlayerConnected() {

	
		if (photonView.isMine == true) {
			currentColor = new Vector3(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b);
			photonView.RPC ("SyncEmUpNoLog", PhotonTargets.Others, currentColor);
		}
	}
	void Update () {
		//Debug.Log (toggleColor.gameObject);
	if (toggleClear.isOn == true) {
			actorID = GameObject.Find("NetworkManager").GetComponent<NetworkManagerPhoton>().actorID;
			specialID = this.photonView.viewID;
			if (specialID == 163) {
				photonView.RPC ("ClearEmAllSpecial", PhotonTargets.All, whiteColor, actorID);
			} else {
				photonView.RPC ("ClearEmAll", PhotonTargets.All, whiteColor, actorID);
			}
			i++;
		}
	if (Input.GetMouseButtonDown (0)) {
				InputColorChange ();
		}
	}
	void FixedUpdate () {
		if (i >= 1) {
			toggleClear.isOn = false;
			i = 0;
		}
	}
	private void InputColorChange()
	{
		actorID = GameObject.Find("NetworkManager").GetComponent<NetworkManagerPhoton>().actorID;

		string myTag = gameObject.transform.name; //Object must have a tag in the inspector
		RaycastHit hit;
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast (ray, out hit) && menuOn.isOn == true) {string hitTag = hit.transform.name;
			if (hitTag == "Skyline 1" || toggleColor1.isOn == true)		 {myColor = new Color32(255, 0, 110, 255); toggleColor1.isOn = true; newColorName = "Magenta";} //Pink
			if (hitTag == "Skyline 2" || toggleColor2.isOn == true)		 {myColor = new Color32(192, 0, 130, 255); toggleColor2.isOn = true; newColorName = "Violet Red";} //MediumVioletRed
			if (hitTag == "Skyline 3" || toggleColor3.isOn == true)		 {myColor = new Color32(128, 0, 170, 255); toggleColor3.isOn = true; newColorName = "Violet";} //DarkViolet
			if (hitTag == "Skyline 4" || toggleColor4.isOn == true)		 {myColor = new Color32(64, 0, 210, 255); toggleColor4.isOn = true; newColorName = "Indigo";} //Indigo
			if (hitTag == "Skyline 5" || toggleColor5.isOn == true)		 {myColor = new Color32(0, 38, 255, 255); toggleColor5.isOn = true; newColorName = "Blue";} //Blue
			if (hitTag == "Skyline 6" || toggleColor6.isOn == true)		 {myColor = new Color32(0, 51, 204, 255); toggleColor6.isOn = true; newColorName = "Dark Blue";} //
			if (hitTag == "Skyline 7" || toggleColor7.isOn == true)		 {myColor = new Color32(0, 102, 153, 255); toggleColor7.isOn = true; newColorName = "Dark Cyan";} //RoyalBlue
			if (hitTag == "Skyline 8" || toggleColor8.isOn == true)		 {myColor = new Color32(0, 153, 102, 255); toggleColor8.isOn = true; newColorName = "Teal";} //
			if (hitTag == "Skyline 9" || toggleColor9.isOn == true)		 {myColor = new Color32(0, 204, 51, 255); toggleColor9.isOn = true; newColorName = "Lime Green";} //
			if (hitTag == "Skyline 10" || toggleColor10.isOn == true)		 {myColor = new Color32(0, 255, 33, 255); toggleColor10.isOn = true; newColorName = "Green";} //Green
			if (hitTag == "Skyline 11" || toggleColor11.isOn == true)		 {myColor = new Color32(64, 255, 0, 255); toggleColor11.isOn = true; newColorName = "Lawn Green";} //LawnGreen
			if (hitTag == "Skyline 12" || toggleColor12.isOn == true)		 {myColor = new Color32(128, 255, 0, 255); toggleColor12.isOn = true; newColorName = "Chartreuse";} //Chartreuse
			if (hitTag == "Skyline 13" || toggleColor13.isOn == true)		 {myColor = new Color32(192, 255, 0, 255); toggleColor13.isOn = true; newColorName = "Green Yellow";} //GreenYellow
			if (hitTag == "Skyline 14" || toggleColor14.isOn == true)		 {myColor = new Color32(255, 216, 0, 255); toggleColor14.isOn = true; newColorName = "Yellow";} //Gold
			if (hitTag == "Skyline 15" || toggleColor15.isOn == true)		 {myColor = new Color32(255, 144, 0, 255); toggleColor15.isOn = true; newColorName = "Orange";} //DarkOrange
			if (hitTag == "Skyline 16" || toggleColor16.isOn == true)		 {myColor = new Color32(255, 72, 0, 255); toggleColor16.isOn = true; newColorName = "Orange Red";} //OrangeRed
			if (hitTag == "Skyline 17" || toggleColor17.isOn == true)		 {myColor = new Color32(255, 0, 0, 255); toggleColor17.isOn = true; newColorName = "Red";} //Red
			if (hitTag == myTag) {
				NameOldColor ();
				if (hit.transform != null && photonView.isMine == true) {
					if (renderer.material.color == Color.white) {
						currentColor = new Vector3(myColor.r, myColor.g, myColor.b);
						photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
					} else {
						currentColor = new Vector3(1f,1f,1f);
						newColorName = "White";
						photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
					}
				} else {
					if (renderer.material.color == Color.white) {
						currentColor = new Vector3(myColor.r, myColor.g, myColor.b);
						photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
					} else {
						currentColor = new Vector3(1f,1f,1f);
						newColorName = "White";
						photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
					}
				}
			}
		}else if (Physics.Raycast (ray, out hit) && menuOn.isOn != true) {string hitTag = hit.transform.name;
			if (hitTag == "Skyline 1")		 {myColor = new Color32(255, 0, 110, 255); newColorName = "Magenta";} //Pink
			if (hitTag == "Skyline 2")		 {myColor = new Color32(192, 0, 130, 255); newColorName = "Violet Red";} //MediumVioletRed
			if (hitTag == "Skyline 3")		 {myColor = new Color32(128, 0, 170, 255); newColorName = "Violet";} //DarkViolet
			if (hitTag == "Skyline 4")		 {myColor = new Color32(64, 0, 210, 255); newColorName = "Indigo";} //Indigo
			if (hitTag == "Skyline 5")		 {myColor = new Color32(0, 38, 255, 255); newColorName = "Blue";} //Blue
			if (hitTag == "Skyline 6")		 {myColor = new Color32(0, 51, 204, 255); newColorName = "Dark Blue";} //
			if (hitTag == "Skyline 7")		 {myColor = new Color32(0, 102, 153, 255); newColorName = "Dark Cyan";} //RoyalBlue
			if (hitTag == "Skyline 8")		 {myColor = new Color32(0, 153, 102, 255); newColorName = "Teal";} //
			if (hitTag == "Skyline 9")		 {myColor = new Color32(0, 204, 51, 255); newColorName = "Lime Green";} //
			if (hitTag == "Skyline 10")		 {myColor = new Color32(0, 255, 33, 255); newColorName = "Green";} //Green
			if (hitTag == "Skyline 11")		 {myColor = new Color32(64, 255, 0, 255); newColorName = "Lawn Green";} //LawnGreen
			if (hitTag == "Skyline 12")		 {myColor = new Color32(128, 255, 0, 255); newColorName = "Chartreuse";} //Chartreuse
			if (hitTag == "Skyline 13")		 {myColor = new Color32(192, 255, 0, 255); newColorName = "Green Yellow";} //GreenYellow
			if (hitTag == "Skyline 14")		 {myColor = new Color32(255, 216, 0, 255); newColorName = "Yellow";} //Gold
			if (hitTag == "Skyline 15")		 {myColor = new Color32(255, 144, 0, 255); newColorName = "Orange";} //DarkOrange
			if (hitTag == "Skyline 16")		 {myColor = new Color32(255, 72, 0, 255); newColorName = "Orange Red";} //OrangeRed
			if (hitTag == "Skyline 17")		 {myColor = new Color32(255, 0, 0, 255); newColorName = "Red";} //Red
				if (hitTag == myTag) {
					NameOldColor ();
					if (hit.transform != null && photonView.isMine == true) {
						if (renderer.material.color == Color.white) {
							currentColor = new Vector3(myColor.r, myColor.g, myColor.b);
							photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
						} else {
							currentColor = new Vector3(1f,1f,1f);
							newColorName = "White";
							photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
						}
					} else {
						if (renderer.material.color == Color.white) {
							currentColor = new Vector3(myColor.r, myColor.g, myColor.b);
							photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
						} else {
							currentColor = new Vector3(1f,1f,1f);
							newColorName = "White";
							photonView.RPC ("SyncEmUp", PhotonTargets.All, currentColor, actorID, oldColorName, newColorName);
						}
					}
				}
			}
		}
	void NameOldColor () {
		colorPlaceholder =  this.renderer.material.color;
		if (colorPlaceholder.ToString() == "RGBA(255, 0, 110, 255)") {oldColorName = "Magenta";}
		if (colorPlaceholder.ToString() == "RGBA(192, 0, 130, 255)") {oldColorName = "Violet Red";}
		if (colorPlaceholder.ToString() == "RGBA(128, 0, 170, 255)") {oldColorName = "Violet";}
		if (colorPlaceholder.ToString() == "RGBA(64, 0, 210, 255)") {oldColorName = "Indigo";}
		if (colorPlaceholder.ToString() == "RGBA(0, 38, 255, 255)") {oldColorName = "Blue";}
		if (colorPlaceholder.ToString() == "RGBA(0, 51, 204, 255)") {oldColorName = "Dark Blue";}
		if (colorPlaceholder.ToString() == "RGBA(0, 102, 153, 255)") {oldColorName = "Dark Cyan";}
		if (colorPlaceholder.ToString() == "RGBA(0, 153, 102, 255)") {oldColorName = "Teal";}
		if (colorPlaceholder.ToString() == "RGBA(0, 204, 51, 255)") {oldColorName = "Lime Green";}
		if (colorPlaceholder.ToString() == "RGBA(0, 255, 33, 255)") {oldColorName = "Green";}
		if (colorPlaceholder.ToString() == "RGBA(64, 255, 0, 255)") {oldColorName = "Lawn Green";}
		if (colorPlaceholder.ToString() == "RGBA(128, 255, 0, 255)") {oldColorName = "Chartreuse";}
		if (colorPlaceholder.ToString() == "RGBA(192, 255, 0, 255)") {oldColorName = "Green Yellow";}
		if (colorPlaceholder.ToString() == "RGBA(255, 216, 0, 255)") {oldColorName = "Yellow";}
		if (colorPlaceholder.ToString() == "RGBA(255, 144, 0, 255)") {oldColorName = "Orange";}
		if (colorPlaceholder.ToString() == "RGBA(255, 72, 0, 255)") {oldColorName = "Orange Red";}
		if (colorPlaceholder.ToString() == "RGBA(255, 0, 0, 255)") {oldColorName = "Red";}
		if (colorPlaceholder.ToString() == "RGBA(255, 255, 255, 255)") {oldColorName = "White";}

	}


[RPC] void SyncEmUp(Vector3 color, string ID, string oldColor, string newColor) //Server forcing all clients to update
	{
		Debug.Log (photonView.name + ", " + oldColor + ", " + newColor + ", " + ID + ", " + PhotonNetwork.playerList.Length + ", " + System.DateTime.Now.ToString());
		dataText.text = "Anonymous " + ID + " changed " + photonView.name + " from " + oldColor + " to " + newColor;
		renderer.material.color = new Color(color.x, color.y, color.z, 1f);
	}
[RPC] void SyncEmUpNoLog(Vector3 color) //Server forcing all clients to update when a new player joins. Does not log to console.
	{
		renderer.material.color = new Color(color.x, color.y, color.z, 1f);
	}
[RPC] void ClearEmAll(Vector3 color, string ID) //Server forcing all clients to update when a new player joins. Does not log to console.
	{
		renderer.material.color = new Color(color.x, color.y, color.z, 1f);
	}
	[RPC] void ClearEmAllSpecial(Vector3 color, string ID) //Server forcing all clients to update when a new player joins. Does not log to console.
	{
		renderer.material.color = new Color(color.x, color.y, color.z, 1f);
		Debug.Log ("Clear All, " + ID + ", " + PhotonNetwork.playerList.Length + ", " + System.DateTime.Now.ToString());
		dataText.text = "Anonymous " + ID + " cleared all";
	}
}