using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Photon.Pun;

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

    [Header("Displays")]
    [SerializeField] GameObject m_readyDisplay;
    [SerializeField] GameObject m_threeDisplay;
    [SerializeField] GameObject m_twoDisplay;
    [SerializeField] GameObject m_oneDisplay;
    [SerializeField] GameObject m_goDisplay;
    [SerializeField] GameObject m_finalLapDisplay;

    [Header("Countdown Sounds")]
    [SerializeField] AudioSource m_cameraAudioSource;
    [SerializeField] AudioClip m_countDownSound;
    [SerializeField] AudioClip m_countDownCompleteSound;
    [SerializeField] AudioClip m_finalLapSound;

    TimeDisplayHandler m_timeDisplayHandler;
    GameLauncher m_gameLauncher;

    public delegate void OnCountdownEndAction();
    public OnCountdownEndAction OnCountdownEnd;

    private void OnEnable()
    {
        if (!m_localPlayer)
            return;

        m_localPlayer.OnFinalLapStarted += FlashFinalLap;
    }

    private void OnDisable()
    {
        if (!m_localPlayer)
            return;

        m_localPlayer.OnFinalLapStarted -= FlashFinalLap;
    }

    private void Start()
    {
        if (m_readyDisplay)
            m_readyDisplay.SetActive(false);

        if (m_threeDisplay)
            m_threeDisplay.SetActive(false);

        if (m_twoDisplay)
            m_twoDisplay.SetActive(false);

        if (m_oneDisplay)
            m_oneDisplay.SetActive(false);

        if (m_goDisplay)
            m_goDisplay.SetActive(false);

        if (m_finalLapDisplay)
            m_finalLapDisplay.SetActive(false);

        m_timeDisplayHandler = FindObjectOfType<TimeDisplayHandler>();
        m_gameLauncher = FindObjectOfType<GameLauncher>();

        string _sceneName = SceneManager.GetActiveScene().name;

        if (!m_localPlayer)
            StartCoroutine(TryGetPlayer());

        if (!_sceneName.Contains("Testing") && !_sceneName.Contains("Lobby") && !_sceneName.Contains("Custom"))
            StartCoroutine(WaitToStart());
    }

    IEnumerator TryGetPlayer()
    {
        while (!m_localPlayer)
        {
            m_localPlayer = RacerCore.LocalRacer;

            yield return null;
        }

        m_localPlayer.OnFinalLapStarted += FlashFinalLap;
    }

    IEnumerator WaitToStart()
    {
        if (!m_timeDisplayHandler)
        {
            if (m_readyDisplay)
                m_readyDisplay.SetActive(false);

            if (m_goDisplay)
                m_goDisplay.SetActive(true);

            yield break;
        }
        
        StartCoroutine(CountDownToStart());
    }


    public void StartCountdown()
    {
        StartCoroutine(CountDownToStart());
    }
    IEnumerator CountDownToStart()
    {
        if (m_readyDisplay)
            m_readyDisplay.SetActive(true);

        yield return new WaitForSeconds(3);

        if (m_readyDisplay)
            m_readyDisplay.SetActive(false);
        if (m_threeDisplay)
            m_threeDisplay.SetActive(true);

        PlayLapAudio(m_countDownSound);

        yield return new WaitForSeconds(1);
        
        if (m_threeDisplay)
            m_threeDisplay.SetActive(false);
        if (m_twoDisplay)
            m_twoDisplay.SetActive(true);
        
        PlayLapAudio(m_countDownSound);

        yield return new WaitForSeconds(1);
        
        if (m_twoDisplay)
            m_twoDisplay.SetActive(false);
        if (m_oneDisplay)
            m_oneDisplay.SetActive(true);

        PlayLapAudio(m_countDownSound);

        yield return new WaitForSeconds(1);
        
        if (m_oneDisplay)
            m_oneDisplay.SetActive(false);
        if (m_goDisplay)
            m_goDisplay.SetActive(true);

        PlayLapAudio(m_countDownCompleteSound);

        if (OnCountdownEnd != null)
            OnCountdownEnd.Invoke();

        yield return new WaitForSeconds(1);
        
        if (m_goDisplay)
            m_goDisplay.SetActive(false);
    }

    void PlayLapAudio(AudioClip _clip, float _volume = 1)
    {
        if (m_cameraAudioSource && _clip)
        {
            m_cameraAudioSource.volume = _volume;
            m_cameraAudioSource.clip = _clip;
            m_cameraAudioSource.Play();
        }
    }

    void FlashFinalLap()
    {
        PlayLapAudio(m_finalLapSound, 0.5f);

        if (!m_finalLapDisplay)
            return;

        StartCoroutine(FinalLap());
    }
    IEnumerator FinalLap()
    {
        m_finalLapDisplay.SetActive(true);

        yield return new WaitForSeconds(3);
        
        m_finalLapDisplay.SetActive(false);

        yield break;
    }
}
