using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using Photon.Pun;
public class CanvasManagerScript :  MonoBehaviourPunCallbacks{

    [Header("Menus")]
    public GameObject startJoinMenu;
    public GameObject quitMenu;

    [Header("Displays")]
    public GameObject lobbyDisplay;
    public GameObject countdownDisplay;
    public GameObject pausedDisplay;
    
    [Header("Gameplay UI")]
    public GameObject timeDisplay;
    public GameObject spectatorDisplay;

    [Header("Interactive UI")]
    public GameObject backToLobbyButton;
    public GameObject exitGameButton;
	public PlayerStats playerStats;
	public bool editMode = false;

	public TrackManagerScript manager;

    // Start is called before the first frame update
    void Start () {
		if (!manager) {
			manager = FindObjectOfType<TrackManagerScript> ();
		}

		// Test to reset time at the start in hopes of stopping the freezing character bug
		if (!PhotonNetwork.IsConnected)
		{
			Time.timeScale = 0f;
			Time.timeScale = 1f;
		}
    }

    // Update is called once per frame
    void Update () {

		if (!manager) {
			manager = FindObjectOfType<TrackManagerScript> ();
		}

		if (PhotonNetwork.IsConnected && Time.timeScale != 1f) {
			Time.timeScale = 1f;
		}

		if (SceneManager.GetActiveScene ().name == "RKR - Esports Lobby") {
			if (PhotonNetwork.IsConnected) {
				backToLobbyButton.SetActive (true);
                lobbyDisplay.SetActive(true);
			} else {
                lobbyDisplay.SetActive(false);
				backToLobbyButton.SetActive (false);

			}
		}

		/*
		#if UNITY_EDITOR
		if(editMode){
			backToLobby.SetActive(true);
			exitGame.SetActive(true);
			pauseRace.SetActive(false);
		} else {
			backToLobby.SetActive(false);
			exitGame.SetActive(false);
			pauseRace.SetActive(true);
		}
		#endif
		*/

		if (Input.GetKeyDown (KeyCode.Escape) && quitMenu != null) {

			if (!quitMenu.activeInHierarchy) {
                quitMenu.SetActive (true);
				if (!PhotonNetwork.IsConnected) {
					Time.timeScale = 0f;
				}
			} else {
                quitMenu.SetActive (false);
				if (!PhotonNetwork.IsConnected) {
					Time.timeScale = 1f;
				}
			}
		}
    }

	public void RaceSelect () {
		if (manager) {
			manager.VotingGUIActive = true;
			startJoinMenu.SetActive (false);
		}
	}

	public void SetInLobbyCanvasActive(bool _spectator)
	{
		startJoinMenu.SetActive(false);
		lobbyDisplay.SetActive(true);
		spectatorDisplay.SetActive(_spectator);
	}

	public void RaceOrSpectateCanvas(bool isSpectator)
	{
		spectatorDisplay.SetActive(isSpectator);
        timeDisplay.SetActive(!isSpectator);
	}


	public void ReturnToLobby () {
		StartCoroutine(TotalDisconnect());
	}

	public void ExitGame () {
		Application.Quit ();
	}

	IEnumerator TotalDisconnect() {

		if (PhotonNetwork.IsConnected) {
			
			if (PhotonNetwork.InRoom) {
				PhotonNetwork.LeaveRoom();
			}

			PhotonNetwork.Disconnect ();

			while (PhotonNetwork.IsConnected) {
				//Debug.Log ("waiting");
				yield return null;
			}
		}

		//SceneManager.LoadScene ("RKR-Lobby");
		SceneManager.LoadScene ("RKR - Esports Lobby");
	}
}