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

public class LobbyMenu : MonoBehaviour
{
    public GameObject m_Player1Panel;
    public GameObject m_Player2Panel;
    public GameObject m_Player3Panel;
    public GameObject m_Player4Panel;
    public GameObject m_TimerPanel;

    private int m_LastPlayerCount = 0;

    private LobbyState m_LobbyState = null;

    void Start ()
    {
        m_Player1Panel.SetActive(false);
        m_Player2Panel.SetActive(false);
        m_Player3Panel.SetActive(false);
        m_Player4Panel.SetActive(false);
        m_TimerPanel.SetActive(false);

        m_LobbyState = (LobbyState)GameManager.Instance.CurrentGameState;
    }

    public void StartGame()
    {
        GameManager.Instance.GotoGameStart();
    }

    public void Update()
    {
        int playerCount = GameManager.GetPersistantGameInfo().ActivePlayers.Count;

        //if (playerCount != m_LastPlayerCount)
        {
            m_LastPlayerCount = playerCount;

            if (playerCount > 0)
            {
                PersistantPlayerInfo p = GameManager.GetPersistantGameInfo().ActivePlayers[0];
                m_Player1Panel.SetActive(true);
                m_Player1Panel.transform.Find("AbilityName").GetComponent<Text>().text = PersistantPlayerInfo.AvailableAbilities[p.SelectedAbility];
            }

            if (playerCount > 1)
            {
                PersistantPlayerInfo p = GameManager.GetPersistantGameInfo().ActivePlayers[1];
                m_Player2Panel.SetActive(true);
                m_Player2Panel.transform.Find("AbilityName").GetComponent<Text>().text = PersistantPlayerInfo.AvailableAbilities[p.SelectedAbility];
            }

            if (playerCount > 2)
            {
                PersistantPlayerInfo p = GameManager.GetPersistantGameInfo().ActivePlayers[2];
                m_Player3Panel.SetActive(true);
                m_Player3Panel.transform.Find("AbilityName").GetComponent<Text>().text = PersistantPlayerInfo.AvailableAbilities[p.SelectedAbility];
            }

            if (playerCount > 3)
            {
                PersistantPlayerInfo p = GameManager.GetPersistantGameInfo().ActivePlayers[3];
                m_Player4Panel.SetActive(true);
                m_Player4Panel.transform.Find("AbilityName").GetComponent<Text>().text = PersistantPlayerInfo.AvailableAbilities[p.SelectedAbility];
            }
        }

        if (m_LobbyState.TimerRunning)
        {
            m_TimerPanel.SetActive(true);
            GameObject.Find("TimerText").GetComponent<Text>().text = m_LobbyState.TimeLeft.ToString("F0");
        }

    }

    /*
    void OnGUI()
    {
        const float h = 22.0f;
        var y = 10.0f;
        int MaxPlayers = GameManager.Instance.m_PersistantGameInfo.MaxPlayers;
        int PlayerCount = GameManager.GetPersistantGameInfo().ActivePlayers.Count;
        GUI.Label(new Rect(10, y, 300, y + h), "Active players: " + PlayerCount + "/" + MaxPlayers);
        y += h;

        if (PlayerCount < MaxPlayers)
        {
            GUI.Label(new Rect(10, y, 300, y + h), "Press a button to join!");
            y += h;
        }

    }
    */
}
