using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SoundManager : MonoBehaviour
{
    public AudioSource musicSource;
    public AudioSource sfxSource;

    public Slider masterSlider;
    public Slider musicSlider;
    public Slider sfxSlider;

    float currentMaster;
    float currentMusic;
    float currentSFX;

	// Use this for initialization
	void Awake ()
    {
	    if (PlayerPrefs.HasKey("MasterVolume") && PlayerPrefs.HasKey("MusicVolume") && PlayerPrefs.HasKey("SFXVolume"))
        {
            currentMaster = PlayerPrefs.GetFloat("MasterVolume");
            currentMusic = PlayerPrefs.GetFloat("MusicVolume");
            currentSFX = PlayerPrefs.GetFloat("SFXVolume");
        }

        else
        {
            PlayerPrefs.SetFloat("MasterVolume", 1);
            PlayerPrefs.SetFloat("MusicVolume", 1);
            PlayerPrefs.SetFloat("SFXVolume", 1);
            PlayerPrefs.Save();
        }

        if (masterSlider != null)
            VolumeSliders();

        SetAudioLevels();
    }

    public void SetVolume()
    {
        currentMaster = masterSlider.value;
        currentMusic = musicSlider.value;
        currentSFX = sfxSlider.value;

        musicSource.volume = currentMusic * currentMaster;
        sfxSource.volume = currentSFX * currentMaster;
    }

    public void Save()
    {
        PlayerPrefs.SetFloat("MasterVolume", currentMaster);
        PlayerPrefs.SetFloat("MusicVolume", currentMusic);
        PlayerPrefs.SetFloat("SFXVolume", currentSFX);
        PlayerPrefs.Save();
    }

    public void VolumeSliders()
    {
        masterSlider.value = currentMaster;
        musicSlider.value = currentMusic;
        sfxSlider.value = currentSFX;
    }

    public void SetAudioLevels()
    {
        musicSource.volume = currentMusic * currentMaster;
        sfxSource.volume = currentSFX * currentMaster;
    }
}
