using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class AudioHandler : MonoBehaviour
{
    public AudioClip buttonClick;
    public AudioClip damage;
    public AudioClip explosion;
    public AudioClip hit;
    public AudioClip hitDagger;
    public AudioClip IceHit;
    public AudioClip LevelUp;
    public AudioClip jump;

    AudioSource cameraAudio;
    AudioSource playerAudio;

    string filepath = "./audiosettings.txt";

    // Start is called before the first frame update
    void Start()
    {
        cameraAudio = GameObject.Find("Main Camera").GetComponent<AudioSource>();
        playerAudio = GameObject.Find("Player").GetComponent<AudioSource>();
        loadAudioSettings();
    }

    public void playButtonClick()
    {
        cameraAudio.mute = true;
        playerAudio.clip = buttonClick;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playDamage()
    {
        cameraAudio.mute = true;
        playerAudio.clip = damage;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playExplosion()
    {
        cameraAudio.mute = true;
        playerAudio.clip = explosion;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playHit()
    {
        cameraAudio.mute = true;
        playerAudio.clip = hit;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playDaggerHit()
    {
        cameraAudio.mute = true;
        playerAudio.clip = hitDagger;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playIceHit()
    {
        cameraAudio.mute = true;
        playerAudio.clip = IceHit;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playJump()
    {
        cameraAudio.mute = true;
        playerAudio.clip = jump;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void playLevelUp()
    {
        cameraAudio.mute = true;
        playerAudio.clip = LevelUp;
        playerAudio.Play();
        cameraAudio.mute = false;
    }

    public void changeVolumeMusic()
    {
        cameraAudio.volume = GameObject.Find("slideMusic").GetComponent<Slider>().value;
    }

    public void changeVolumeEffects()
    {
        playerAudio.volume = GameObject.Find("slideEffects").GetComponent<Slider>().value;
    }

    public void setSlider()
    {
        GameObject.Find("slideEffects").GetComponent<Slider>().value = playerAudio.volume;
        GameObject.Find("slideMusic").GetComponent<Slider>().value = cameraAudio.volume;
    }

    public void loadAudioSettings()
    {
        if (File.Exists(filepath))
        {
            string[] lines = File.ReadAllLines(filepath);
            cameraAudio.volume = float.Parse(lines[0].Split(':')[1]);
            playerAudio.volume = float.Parse(lines[1].Split(':')[1]);   
        }
        else
        {
            StreamWriter sw = File.CreateText(filepath);
            sw.WriteLine("Music:0.5");
            sw.WriteLine("Effects:0.5");
            sw.Flush();
        }
    }

    public void saveAudioSettings()
    {
        float music = GameObject.Find("slideMusic").GetComponent<Slider>().value;
        float effects = GameObject.Find("slideEffects").GetComponent<Slider>().value;
        StreamWriter sw;
        if (!File.Exists(filepath))
        {
            sw = File.CreateText(filepath);
        }
        else
        {
            sw = new StreamWriter(filepath);
        }
        sw.WriteLine("Music:"+ music);
        sw.WriteLine("Effects:"+ effects);
        sw.Flush();
    }
}
