using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Assets.Scripts.Events;
using UnityEngine;

namespace Assets.Scripts.Core
{
    class MusicController : MonoBehaviour
    {
        [SerializeField] private AudioClip _act1;
        [SerializeField] private AudioClip _act2;
        [SerializeField] private AudioClip _act3;
        [SerializeField] private AudioClip _battle;
        [SerializeField] private AudioClip _finalBattle;
        private AudioSource _source;
        private AudioClip _playing;
        private AudioClip _previous;

        public static MusicController Instance { get; private set; }

        protected void Awake() {
            if (Instance != null) {
                DestroyObject(gameObject);
            } else {
                Instance = this;
                DontDestroyOnLoad(gameObject);
                _source = GetComponent<AudioSource>();
            }
        }

        public void SetAct(Act act) {
            switch (act) {
                case Act.Act_1:
                    Play(_act1);
                    break;
                case Act.Act_2:
                    Play(_act2);
                    break;
                case Act.Act_3:
                case Act.Epilogue:
                    Play(_act3);
                    break;
            }
        }

        public void Play(AudioClip clip) {
            if (_playing != clip) {
                _playing = clip;
                _source.clip = clip;
                _source.Play();
            }
        }

        public void PlayTitle() {
            Play(_act3);
        }

        public void PlayCredits () {
            Play(_act1);
        }

        public void StartGame () {
            Play(_act1);
        }

        public void StartBattle(bool final) {
            _previous = _playing;
            Play(final ? _finalBattle : _battle);
        }

        public void EndBattle() {
            Play(_previous);
        }
    }
}
