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

public class AudioManager : MonoBehaviour {

    public enum AudioChannel { Music, SoundEffects };
    public AudioSource musicSource, lapSoundsSource, engineSource;

    float musicVolume = .2f;
    float soundEffectsVolume = 1f;

    public Transform audioListenerT;
    public Transform playerT;

    private void Awake() {

        musicVolume = PlayerPrefs.GetFloat("MusicVolume", musicVolume);
        soundEffectsVolume = PlayerPrefs.GetFloat("SoundEffectsVolume", soundEffectsVolume);
    }

    public void SetVolume(float volume, AudioChannel channel) {
        switch (channel) {
            case AudioChannel.Music:
                musicVolume = volume;
                break;
            case AudioChannel.SoundEffects:
                soundEffectsVolume = volume;
                break;
        }

        musicSource.volume = musicVolume;
        lapSoundsSource.volume = soundEffectsVolume;
        if (engineSource)
        {
            engineSource.volume = soundEffectsVolume/2f;
        }

        PlayerPrefs.SetFloat("MusicVolume", musicVolume);
        PlayerPrefs.SetFloat("SoundEffectsVolume", soundEffectsVolume);
    }

    public float GetVolume(AudioChannel channel) {
        switch (channel) {
            case AudioChannel.Music:
                return musicVolume;
            case AudioChannel.SoundEffects:
                return soundEffectsVolume;
        }
        return 0;
    }

    public void PlayMusic() {
        musicSource.loop = true;
        musicSource.Play();

    }

    public void StopMusic() {
        musicSource.loop = false;
        musicSource.Stop();
    }

    public void PlaySound(AudioClip clip, Vector3 position) {
        if (!clip)
            return;
        AudioSource.PlayClipAtPoint(clip, position, soundEffectsVolume);
    }
}
