using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class FinalPlacementDisplay : MonoBehaviour
{
    [Header("Local Player")]
    [SerializeField] RacerCore m_localPlayer;

    [Header("Text Displays")]
    [SerializeField] GameObject m_youFinishedInText;
    [SerializeField] GameObject m_finalPlacementHolder;
    [SerializeField] TextMeshProUGUI m_finalPlacementNumber;
    [SerializeField] TextMeshProUGUI m_finalPlacementSupertext;

    [Header("Placement Colors")]
    [SerializeField] Color[] m_placementColors = new Color[8];

    [Header("Debug")]
    [SerializeField] DebugChannelSO m_debugChannel;

    // DEFAULT METHODS //
    #region
    private void OnEnable()
    {
        if (!m_localPlayer)
            return;

        if (m_debugChannel)
            m_debugChannel.Raise(this, "Subscribed to local player! Awaiting final placement.");
        
        m_localPlayer.OnPlacementFinalized += SetFinalPlacementDisplay;
    }
    private void OnDisable()
    {
        if (!m_localPlayer)
            return;

        if (m_debugChannel)
            m_debugChannel.Raise(this, "Unsubscribed from local player.");

        m_localPlayer.OnPlacementFinalized -= SetFinalPlacementDisplay;
    }
    void Start()
    {
        if (m_youFinishedInText)
            m_youFinishedInText.SetActive(false);

        if (m_finalPlacementHolder)
            m_finalPlacementHolder.SetActive(false);

        StartCoroutine(TryGetPlayer());
    }
    #endregion

    // EXECUTABLE METHODS //
    #region
    void SetFinalPlacementDisplay(int _place)
    {
        if (_place == -1 && m_finalPlacementNumber)
        {
            TimeDisplayHandler _tdm = FindObjectOfType<TimeDisplayHandler>();
            m_finalPlacementNumber.SetText(!_tdm ? "Finish!" : ((float)Mathf.FloorToInt(_tdm.RaceTimeElapsed * 100) / 100).ToString() + " s");
            m_youFinishedInText.SetActive(true);
            m_finalPlacementHolder.SetActive(true);
            m_finalPlacementSupertext.gameObject.SetActive(false);
        }

        if (_place < 1 || _place > 8)
            return;

        if (m_youFinishedInText)
            m_youFinishedInText.SetActive(true);

        if (m_finalPlacementNumber)
        {
            m_finalPlacementNumber.SetText(_place.ToString());

            if (m_placementColors != null && m_placementColors.Length > _place - 1)
                m_finalPlacementNumber.color = m_placementColors[_place - 1];
            else
                m_finalPlacementNumber.color = Color.white;
        }

        if (m_finalPlacementSupertext)
        {
            switch (_place)
            {
                case (1):
                    m_finalPlacementSupertext.SetText("st");
                    break;
                case (2):
                    m_finalPlacementSupertext.SetText("nd");
                    break;
                case (3):
                    m_finalPlacementSupertext.SetText("rd");
                    break;
                default:
                    m_finalPlacementSupertext.SetText("th");
                    break;
            }

            if (m_placementColors != null && m_placementColors.Length > _place - 1)
                m_finalPlacementSupertext.color = m_placementColors[_place - 1];
            else
                m_finalPlacementSupertext.color = Color.white;
        }
        
        if (m_finalPlacementHolder)
            m_finalPlacementHolder.SetActive(true);

        if (m_finalPlacementNumber)
            m_finalPlacementNumber.gameObject.SetActive(true);

        if (m_finalPlacementSupertext)
            m_finalPlacementSupertext.gameObject.SetActive(true);
    }
    #endregion

    // COROUTINES //
    #region
    IEnumerator TryGetPlayer()
    {
        while (!m_localPlayer)
        {
            m_localPlayer = RacerCore.LocalRacer;

            yield return null;
        }

        if (m_debugChannel)
            m_debugChannel.Raise(this, "Subscribed to local player! Awaiting final placement.");

        m_localPlayer.OnPlacementFinalized += SetFinalPlacementDisplay;
    }
    #endregion
}
