using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Rewired;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace GGJ2021
{
    public class MusicManager : MonoBehaviour
    {
        [FMODUnity.EventRef]
        [SerializeField]
        private string musicRef = null;

        [SerializeField]
        private FMODUnity.StudioEventEmitter hideStart = null;

        [SerializeField]
        private FMODUnity.StudioEventEmitter hideEndByForce = null;

        [SerializeField]
        private FMODUnity.StudioEventEmitter gameWinSfx = null;

        [SerializeField]
        private FMODUnity.StudioEventEmitter gameLoseSfx = null;

        [SerializeField]
        private Image gameEndPanel = null;

        [SerializeField]
        private GameObject winImage = null;

        [SerializeField]
        private GameObject loseImage = null;

        [SerializeField]
        private GameObject restartText = null;

        private FMOD.Studio.EventInstance _musicState;
        private bool _canRestart;
        private Player _player;

        private void Awake()
        {
            FMODUnity.RuntimeManager.CoreSystem.setDSPBufferSize(512, 4); // default is 1024/4

            _player = ReInput.players.GetPlayer(0);
            _canRestart = false;
        }

        private void Start()
        {
            _musicState = FMODUnity.RuntimeManager.CreateInstance(musicRef);
            _musicState.setParameterByName("Hiding", 0f);
            _musicState.start();
        }

        private void OnEnable()
        {
            Messenger.AddListener(Events.OnHideStart, OnHideStart);
            Messenger.AddListener(Events.OnHideEnd, OnHideEnd);
            Messenger.AddListener(Events.OnGameWin, OnGameWin);
            Messenger.AddListener(Events.OnGameLose, OnGameLose);
            Messenger.AddListener(Events.OnHideEndByForce, OnHideEndByForce);
        }

        private void OnDisable()
        {
            Messenger.RemoveListener(Events.OnHideStart, OnHideStart);
            Messenger.RemoveListener(Events.OnHideEnd, OnHideEnd);
            Messenger.RemoveListener(Events.OnGameWin, OnGameWin);
            Messenger.RemoveListener(Events.OnGameLose, OnGameLose);
            Messenger.RemoveListener(Events.OnHideEndByForce, OnHideEndByForce);
        }

        private void OnDestroy()
        {
            _musicState.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
            _musicState.release();
        }

        private void Update()
        {
            if (_canRestart == true && _player.GetAnyButtonDown())
            {
                SceneManager.LoadScene(0);
                _canRestart = false;
            }
        }

        private void OnHideStart()
        {
            Debug.Log("start");
            _musicState.setParameterByName("Hiding", 1f);
            hideStart.Play();
        }

        private void OnHideEnd()
        {
            _musicState.setParameterByName("Hiding", 0f);
        }

        private void OnHideEndByForce()
        {
            Debug.Log("hide end by force");
            hideEndByForce.Play();
        }

        private void OnGameWin()
        {
            _musicState.setParameterByName("Hiding", 0f);
            _musicState.setParameterByName("win", 1f);
            gameWinSfx.Play();

            gameEndPanel.enabled = true;
            gameEndPanel.DOFade(0.6f, 1f).SetDelay(1f).OnComplete(() =>
            {
                winImage.SetActive(true);
                restartText.SetActive(true);
                _canRestart = true;
            });
        }

        private void OnGameLose()
        {
            _musicState.setParameterByName("Hiding", 0f);
            _musicState.setParameterByName("caught", 1f);
            gameLoseSfx.Play();

            gameEndPanel.enabled = true;
            gameEndPanel.DOFade(0.6f, 1f).SetDelay(1f).OnComplete(() =>
            {
                loseImage.SetActive(true);
                restartText.SetActive(true);
                _canRestart = true;
            });
        }
    }

}
