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

public class MainMenuEngine : MonoBehaviour
{
    public GameObject hudGO;
    public GameObject joystickGO;
    public GameObject yolkAGO;
    public GameObject yolkBGO;
    public TMPro.TextMeshProUGUI versionText;

    public BackgroundMusicManager currentBGMManager;
    public MenuAudioManager currentMenuAudioManager;

    public float joystickJiggleSpeed;

    public float flashFrequencyTime;
    private TimerHelper _flashTimer;

    private MeshRenderer _meshRenderer;
    private Color _defaultEmissionColor;

    private GameObject _lastControlSelected;

    // 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");

        versionText.text = versionText.text.Replace("{versionNo}", GameManager.Instance.CurrentVersion);
    }

    public void SetupVolumeOptions()
    {
        // Gets the volume settings from the player profile and sets it in the game
        float currentMasterVol = PlayerPrefs.GetFloat("currentMasterVol");
        GameObject masterVolSliderGo = GameObject.Find("MainVolSlider");

        float currentMusicVol = PlayerPrefs.GetFloat("currentMusicVol");
        GameObject musicVolSliderGo = GameObject.Find("MusicVolSlider");

        float currentSFXVol = PlayerPrefs.GetFloat("currentSFXVol");
        GameObject SFXVolSliderGo = GameObject.Find("SfxVolSlider");

        masterVolSliderGo.GetComponent<Slider>().value = currentMasterVol;
        musicVolSliderGo.GetComponent<Slider>().value = currentMusicVol;
        SFXVolSliderGo.GetComponent<Slider>().value = currentSFXVol;
    }

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

    public void StartGame()
    {
        // Play the menu click sound if the audio manager is present
        if (currentMenuAudioManager)
        {
            currentMenuAudioManager.PlayMenuClick();
        }
        SceneManager.LoadScene("Rules");
    }

    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
    }

    public void SetMasterVolume(float value)
    {
        // Converts the master volume into decibels and stores it in the player profile
        GameManager.Instance.currentAudioMixer.SetFloat("masterVol", SharedMethods.ConvertToDecibels(value));
        PlayerPrefs.SetFloat("currentMasterVol", value);
    }

    public void SetMusicVolume(float value)
    {
        // Converts the music volume into decibels and stores it in the player profile
        GameManager.Instance.currentAudioMixer.SetFloat("musicVol", SharedMethods.ConvertToDecibels(value));
        PlayerPrefs.SetFloat("currentMusicVol", value);
    }

    public void SetSFXVolume(float value)
    {
        // Converts the sfx volume into decibels and stores it in the player profile
        GameManager.Instance.currentAudioMixer.SetFloat("sfxVol", SharedMethods.ConvertToDecibels(value));
        PlayerPrefs.SetFloat("currentSFXVol", value);
    }

    // Update is called once per frame
    void Update()
    {
        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 horzLRot = -Input.GetAxis("Horizontal") * joystickJiggleSpeed;
        float vertLRot = -45.0f + -Input.GetAxis("Vertical") * joystickJiggleSpeed;
        joystickGO.transform.eulerAngles = new Vector3(vertLRot, 0.0f, horzLRot);

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

        float horzRRot = -25 + -Input.GetAxis("Mouse X") * joystickJiggleSpeed;
        float vertRRot = -25 + -Input.GetAxis("Mouse Y") * joystickJiggleSpeed;
        yolkAGO.transform.eulerAngles = new Vector3(vertRRot, 0.0f, 0.0f);
        yolkBGO.transform.eulerAngles = new Vector3(horzRRot, 0.0f, 0.0f);
    }
}
