﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine.Analytics;

public class MenuNavigationController : MonoBehaviour {

    [Header("Canvas Panels")]
    public GameObject mainPanel;
    public GameObject playPanel;
    public GameObject creditsPanel;
    public GameObject optionsPanel;
    public GameObject spectatePanel;
    public GameObject statsPanel;
    public GameObject leaderboardPanel;
    public GameObject loginPanel;
    public GameObject signupPanel;
    public GameObject loadingPanel;
    public GameObject controlsPanel;
    public GameObject playerControlsPanel;
    public GameObject spectatorControlsPanel;
    public GameObject swapControlsButton;
    public GameObject customizePanel;

    [Header("Player Stats")]
    public PlayerStats playerStats;
    
    public List<Color> controlButtonColors;


    void Awake() {
		Cursor.visible = true;
		Cursor.lockState = CursorLockMode.None;
        if(PlayFabClientAPI.IsClientLoggedIn()) {
            GoToTeacherMainMenu();
            playerStats.GetPlayerStats();
            customizePanel.SetActive(true);
        }
    }
    
    public void PlayButton() {
        SetActivePanel(playPanel);
    }

    public void CreditsButton() {
        SetActivePanel(creditsPanel);
    }

    public void OptionsButton() {
        SetActivePanel(optionsPanel);
    }

	public void SpectateButton() {
		SetActivePanel(spectatePanel);
	}

    public void CreateAccountButton() {
        SetActivePanel(signupPanel);
    }

    public void StatsButton() {
        SetActivePanel(statsPanel);
    }

    public void LoginToMenu() {
        SetActivePanel(mainPanel);
        customizePanel.SetActive(true);
    }

    public void LeaderboardButton() {
        SetActivePanel(leaderboardPanel);
    }

    public void BackToLogin() {
        SetActivePanel(loginPanel);
    }

    // bring you back to root main menu
    public void BackButton() {
        SetActivePanel(mainPanel);
    }

	public void ControlsButton() {
        SetActivePanel(controlsPanel);
        playerControlsPanel.SetActive(true);
        spectatorControlsPanel.SetActive(false);
        swapControlsButton.GetComponent<Button>().image.color = new Color(255f, 135f, 0f);
    }

    public void SwapControlsButton()
    {
        playerControlsPanel.SetActive(!playerControlsPanel.activeInHierarchy);
        spectatorControlsPanel.SetActive(!spectatorControlsPanel.activeInHierarchy);

        TextMeshProUGUI text = swapControlsButton.transform.GetComponentInChildren<TextMeshProUGUI>();
        if (!text) { Debug.LogError("Text not found!"); return; }
        else { text.text = playerControlsPanel.activeInHierarchy ? "Spectator Controls" : "Player Controls"; }

    }

    void SetActivePanel(GameObject panel) {
        if(mainPanel) mainPanel.SetActive(panel == mainPanel);
        if(playPanel) playPanel.SetActive(panel == playPanel);
        if(creditsPanel) creditsPanel.SetActive(panel == creditsPanel);
        if(optionsPanel) optionsPanel.SetActive(panel == optionsPanel);
        if(statsPanel) statsPanel.SetActive(panel == statsPanel);
        if(spectatePanel) spectatePanel.SetActive(panel == spectatePanel);
        if(controlsPanel) controlsPanel.SetActive(panel == controlsPanel);
		if(loginPanel) loginPanel.SetActive(panel == loginPanel);
		if(signupPanel) signupPanel.SetActive(panel == signupPanel);
		if(leaderboardPanel) leaderboardPanel.SetActive(panel == leaderboardPanel);
        if(loadingPanel) loadingPanel.SetActive(panel == loadingPanel);
    }

    public void GoToTeacherMainMenu() {
		SetActivePanel (mainPanel);
    }
}
