using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    [Header("Machine Sounds")]
    [SerializeField] AudioClip machinePlace;
    [SerializeField, Range(0.1f, 1.0f)] float machinePlaceVolume = 1f;
    [SerializeField] AudioClip machineShoot;
    [SerializeField, Range(0.1f, 1.0f)] float machineShootVolume = 1f;
    [SerializeField] AudioClip machineDestroyed;
    [SerializeField, Range(0.1f, 1.0f)] float machineDestroyVolume = 1f;
    [SerializeField] AudioClip switchSelection;
    [SerializeField, Range(0.1f, 1.0f)] float switchSelectionVolume = 1f;

    [Header("Enemy sounds")]
    [SerializeField] AudioClip enemyHit;
    [SerializeField, Range(0.1f, 1.0f)] float enemyHitVolume = 1f;
    [SerializeField] AudioClip enemyDeath;
    [SerializeField, Range(0.1f, 1.0f)] float enemyDeathVolume = 1f;

    [Header("Other sounds")]
    [SerializeField] AudioClip buttonPress;
    [SerializeField, Range(0.1f, 1.0f)] float buttonPressVolume = 1f;
    [SerializeField] AudioClip gameBegin;
    [SerializeField, Range(0.1f, 1.0f)] float gameBeginVolume = 1f;
    [SerializeField] AudioClip menuTheme;
    [SerializeField, Range(0.1f, 1.0f)] float menuThemeVolume = 1f;
    [SerializeField] AudioClip gameTheme;
    [SerializeField, Range(0.1f, 1.0f)] float gameThemeVolume = 1f;


    private AudioSource audioSource;
    public AudioSource musicSource;

    public enum SoundType
    {
        MACHINEPLACE,
        MACHINESHOOT,
        MACHINEDESTROYED,
        SWITCHSELECTION,
        ENEMYHIT,
        ENEMYDEATH,
        BUTTONPRESS,
        GAMEBEGIN,
        MENUTHEME,
        GAMETHEME,
    }

    private void Awake()
    {
        musicSource = Camera.main.GetComponent<AudioSource>();
    }

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }
    public void PlaySound(SoundType soundType)
    {
        AudioClip sound = null;
        float volume = 1f;

        switch (soundType)
        {
            case SoundType.MACHINEPLACE:
                sound = machinePlace;
                volume = machinePlaceVolume;
                break;
            case SoundType.MACHINESHOOT:
                sound = machineShoot;
                volume = machineShootVolume;
                break;
            case SoundType.MACHINEDESTROYED:
                sound = machineDestroyed;
                volume = machineDestroyVolume;
                break;
            case SoundType.SWITCHSELECTION:
                sound = switchSelection;
                volume = switchSelectionVolume;
                break;
            case SoundType.ENEMYHIT:
                sound = enemyHit;
                volume = enemyHitVolume;
                break;
            case SoundType.ENEMYDEATH:
                sound = enemyDeath;
                volume = enemyDeathVolume;
                break;
            case SoundType.BUTTONPRESS:
                sound = buttonPress;
                volume = buttonPressVolume;
                break;
            case SoundType.GAMEBEGIN:
                sound = gameBegin;
                volume = gameBeginVolume;
                break;
        }

        audioSource.PlayOneShot(sound, volume);

    }

    public void PlayMusic(SoundType musicType)
    {
        AudioClip music = null;
        float volume = 1f;

        switch (musicType)
        {
            case SoundType.MENUTHEME:
                music = menuTheme;
                volume = menuThemeVolume;
                break;
            case SoundType.GAMETHEME:
                music = gameTheme;
                volume = gameThemeVolume;
                break;
        }
        musicSource.Stop();
        musicSource.clip = music;
        musicSource.volume = volume;
        musicSource.Play();
    }
    public void PauseMusic()
    {
        musicSource.Pause();
    }
    public void UnPauseMusic()
    {
        musicSource.UnPause();
    }
}