using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;

public class AnimationWheel : MonoBehaviour {

	PlayerBase myPlayer;
	public static bool toggleWheel = false;
	public static bool emoteActive = false;
	bool lastEmoteValue = false;
	public List<GameObject> emoteTexts;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (!myPlayer) {
			GetLocalPlayer ();
		}

		if (Input.GetKeyDown (KeyCode.B)) {
			if (toggleWheel) {
				ResetWheel (false);
			} else {
				ResetWheel (true);
			}
		}

		if (emoteActive != lastEmoteValue && emoteActive == false) {
			StopEmote ();
		}
	}

	public void DisplayName (Transform obj) {
		obj.GetChild (0).gameObject.SetActive (true);
	}

	public void HideName (Transform obj) {
		obj.GetChild (0).gameObject.SetActive (false);
	}

	public void ActivateEmote (int emoteNum){
		myPlayer.anim.SetInteger ("Dance Number", emoteNum);
		myPlayer.anim.SetTrigger ("Start Dancing");
		myPlayer.anim.applyRootMotion = true;
		ResetWheel (false);
		myPlayer.anim.SetLayerWeight (4, 1f);
		emoteActive = true;
		lastEmoteValue = true;
		Cursor.visible = false;
		Cursor.lockState = CursorLockMode.Locked;
	}

	void GetLocalPlayer() {
		GameObject[] obj = GameObject.FindGameObjectsWithTag("Player");
		for (int i = 0; i < obj.Length; i++) {
			PlayerBase playerBase = obj[i].GetComponent<PlayerBase>();

			if(playerBase.photonView && playerBase.photonView.IsMine && PhotonNetwork.IsConnected) {
				myPlayer = playerBase;
			}
			else if(!playerBase.photonView || !PhotonNetwork.IsConnected) {
				myPlayer = playerBase;
			}
		}
	}

	void StopEmote () {
		myPlayer.anim.SetTrigger ("Stop Dancing");
		myPlayer.anim.applyRootMotion = false;
		myPlayer.anim.SetLayerWeight (4, 0f);
		lastEmoteValue = false;
	}

	public void ResetWheel (bool toggle) {
		transform.GetChild (0).gameObject.SetActive (transform.GetChild (0).gameObject.activeInHierarchy ? false : true);
		if (emoteTexts.Count > 0) {
			for (int i = 0; i < emoteTexts.Count; i++) {
				emoteTexts [i].SetActive (false);
			}
		}
		toggleWheel = toggle;
	}
}
