using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using InControl;

public class LobbyState : GameState
{
    private PersistantGameInfo m_GameInfo = null;

    const float CONST_Timer = 5.0f;
    private float m_TimeLeft = CONST_Timer;
    private bool m_TimerRunning = false;

    public bool TimerRunning { get { return m_TimerRunning; } }
    public float TimeLeft { get { return m_TimeLeft; } }

    public LobbyState()
        :base(GameStateType.Lobby)
    {
    }

    public override void OnEnter()
    {
        base.OnEnter();
        // Called on Start
        SceneManager.LoadScene("Lobby");

        m_GameInfo = GameManager.Instance.m_PersistantGameInfo;
        m_GameInfo.Reset();
    }

    public override void OnSceneLoaded(string _sceneName)
    {
        base.OnSceneLoaded(_sceneName);
    }

    public void ResetTimer()
    {
        m_TimeLeft = CONST_Timer;
        m_TimerRunning = true;
    }

    public override void OnUpdate()
    {
        base.OnUpdate();

        /*
        if (m_Timer == null)
        {
            m_Timer = GameObject.FindObjectOfType<Timer>();
            m_Timer.ResetTimer();
        }
        */

        var inputDevice = InputManager.ActiveDevice;

        if (JoinButtonWasPressedOnDevice(inputDevice))
        {
            if (!IsAPlayerUsingDevice(inputDevice))
            {
                m_GameInfo.AddPlayer(inputDevice);

                if (m_GameInfo.IsSessionFull())
                {
                    GameManager.Instance.GotoGameStart();
                }
                else
                {
                    ResetTimer();
                }
            }
        }

        if (inputDevice.LeftStickLeft.WasPressed)
        {
            PersistantPlayerInfo p = m_GameInfo.GetPlayerUsingDevice(inputDevice);

            if (p != null)
            {
                p.SelectedAbility = p.SelectedAbility - 1;
                if (p.SelectedAbility < 0)
                    p.SelectedAbility = 0;
            }
        }

        if (inputDevice.LeftStickRight.WasPressed)
        {
            PersistantPlayerInfo p = m_GameInfo.GetPlayerUsingDevice(inputDevice);

            if (p != null)
            {
                p.SelectedAbility = p.SelectedAbility + 1;
                if (p.SelectedAbility >= PersistantPlayerInfo.AvailableAbilities.Count)
                    p.SelectedAbility = PersistantPlayerInfo.AvailableAbilities.Count - 1;
            }
        }

        if (m_TimeLeft<=0.0f)
        {
            m_TimeLeft = 0.0f;
            GameManager.Instance.GotoGameStart();
        }

        if (m_TimerRunning)
            m_TimeLeft -= Time.deltaTime;
    }

    bool IsAPlayerUsingDevice(InputDevice _inputDevice)
    {
        return (m_GameInfo.GetPlayerUsingDevice(_inputDevice) != null);
    }

    bool JoinButtonWasPressedOnDevice(InputDevice inputDevice)
    {
        return inputDevice.Action1.WasPressed || inputDevice.Action2.WasPressed || inputDevice.Action3.WasPressed || inputDevice.Action4.WasPressed;
    }
}
