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

public class DeadEngine : MonoBehaviour
{
    public GameObject hudGO;
    public GameObject cockpitGO;
    public TMPro.TextMeshProUGUI scoreText;

    public float cockpitRotationSpeed;

    public BackgroundMusicManager currentBGMManager;
    public MenuAudioManager currentMenuAudioManager;

    public float flashFrequencyTime;
    private TimerHelper _flashTimer;

    private MeshRenderer _meshRenderer;
    private Color _defaultEmissionColor;

    private GameObject _lastControlSelected;

    private void Awake()
    {
        GameManager.Instance.currentGameState = GameState.DEATHMENU;
    }

    // Start is called before the first frame update
    void Start()
    {
        // If the BGM manager is present, queue up and play the given track index
        if (currentBGMManager)
        {
            currentBGMManager.StartAudioQueueAndPlay(0);
        }

        _flashTimer = new TimerHelper(flashFrequencyTime);
        _meshRenderer = hudGO.GetComponent<MeshRenderer>();
        _defaultEmissionColor = _meshRenderer.materials[0].GetColor("_EmissionColor");
        _lastControlSelected = EventSystem.current.firstSelectedGameObject;

        scoreText.text = scoreText.text.Replace("{SCORE}", GameManager.Instance.CurrentScore.ToString().PadLeft(9, '0'));
    }

    public void PlayMenuClick()
    {
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }
    }

    public void MainMenuNav()
    {
        // Play the menu click sound if the audio manager is present
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }
        SceneManager.LoadSceneAsync("MainMenu");
    }

    public void GameMenuNav()
    {
        // Play the menu click sound if the audio manager is present
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }
        GameManager.Instance.ResetStats();
        SceneManager.LoadSceneAsync("Game");
    }

    public void Quit()
    {
        // Play the menu click sound if the audio manager is present
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }

#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
             Application.Quit();
#endif
    }

    // Update is called once per frame
    void Update()
    {
        if (cockpitGO != null)
        {
            cockpitGO.transform.Rotate(Vector3.up, cockpitRotationSpeed * Time.deltaTime);
        }

        if (_flashTimer.HasTicked(Time.deltaTime))
        {
            float randomNo = Random.Range(0.0f, 1.0f);
            _meshRenderer.materials[0].SetColor("_EmissionColor", Color.Lerp(Color.black, _defaultEmissionColor, randomNo));
        }

        float horzIn = Input.GetAxis("Horizontal");
        float vertIn = Input.GetAxis("Vertical");

        if (horzIn != 0 || vertIn != 0)
        {
            if (EventSystem.current.currentSelectedGameObject == null)
            {
                EventSystem.current.SetSelectedGameObject(_lastControlSelected);
            }
            else
            {
                _lastControlSelected = EventSystem.current.currentSelectedGameObject;
            }
        }
    }
}
