using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Source.Content.Audio;

namespace Assets.Source.Core.Managers
{
    public class AudioManager : Patterns.Singleton<AudioManager>
    {
        [SerializeField] private AudioSource musicSource;
        [SerializeField] private AudioSource shipEngineSource;
        //[SerializeField] private AudioSource sfxSource;

        [Space]
        [SerializeField] private AudioObject audioObject;

        [Space]
        [SerializeField]
        private SoundInstance shipEngineSound;
        //[SerializeField]
        //private float minEngineVolume = 0.25f;

        [Space]
        [SerializeField] private SoundInstance shipShootSound;
        [SerializeField] private SoundInstance asteroidExplosionSound;
        [SerializeField] private SoundInstance bulletHitAsteroidSound;
        [SerializeField] private SoundInstance playerHitAsteroidSound;
        [SerializeField] private SoundInstance playerPickedUpHealth;
        [SerializeField] private SoundInstance playerPickedUpAlien;
        [SerializeField] private SoundInstance music;

        protected override void Init()
        {
            base.Init();
            shipEngineSource.clip = shipEngineSound.Clip;
            shipEngineSource.loop = true;
            shipEngineSource.Play();
            musicSource.clip = music.Clip;
            musicSource.Play();
        }

        private void PlaySound(SoundInstance sound)
        {
            AudioObject obj = audioObject.Spawn();
            obj.SetAudioInstance(sound);
        }

        public void PlayShipShoot()
        {
            PlaySound(shipShootSound);
        }

        public void PlayAsteroidExplosion()
        {
            PlaySound(asteroidExplosionSound);
        }

        public void PlayBulletHitAsteroid()
        {
            PlaySound(bulletHitAsteroidSound);
        }

        public void PlayPlayerHitAsteroid()
        {
            PlaySound(playerHitAsteroidSound);
        }

        public void PlayPlayerPickedUpHealth()
        {
            PlaySound(playerPickedUpHealth);
        }

        public void PlayPlayerPickedUpAlien()
        {
            PlaySound(playerPickedUpAlien);
        }

        public void UpdateShipEngineVolume(float target)
        {
            float volume = shipEngineSource.volume;
            volume = Mathf.MoveTowards(volume, target, 1 * Time.deltaTime);
            shipEngineSource.volume = volume;
        }

        //public void Update()
        //{
        //    if (Input.GetKeyDown(KeyCode.Alpha1))
        //    {
        //        PlayShipShoot();
        //    }
        //    if (Input.GetKeyDown(KeyCode.Alpha2))
        //    {
        //        PlayAsteroidExplosion();
        //    }

        //    float input = Input.GetAxis("Horizontal");
        //    UpdateShipEngineVolume(input);
        //}
    }
}
