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

public class Audioness : MonoBehaviour {
    public AudioSource grump;
    public AudioSource ingame;
    public AudioSource startMenu;
    public AudioSource titleTheme;
    public AudioSource shot;
    public AudioSource bonus;
    public AudioSource bossApproaches;
    public AudioSource explosions;
    public AudioSource bossTheme;
    public AudioSource grumpShot;
    public AudioSource boo;
    public AudioSource bossHit;
    public AudioSource bossDead;
	
    void Awake()
    {
        Observer.gameTitleExitedHandler += GameTitleExited;
        Observer.grumpTouchedHandler += Grump; 
        Observer.gameInitiatedHandler += StartMenu;
        Observer.gameResetHandler += GameReset;
        Observer.bonusTouchedHandler += Bonus;
        Observer.bossApproachesHandler += BossApproaches;
        Observer.bossReadyHandler += BossReady;
        Observer.bossDeadHandler += BossDead;
        Observer.bossCleanupHandler += BossCleanup;
        Observer.shotHandler += Shot;
        Observer.grumpShotHandler += GrumpShot;
        Observer.bossDamageHandler += BossDamage;
    }

    void BossDamage(int health)
    {
        bossHit.Play();
    }

    void GrumpShot(Vector3 position)
    {
        grumpShot.Play();
        if (Random.Range(0, 100) < 50) boo.Play();
    }

    void Shot(Vector3 position)
    {
        shot.Play();
    }

    public static IEnumerator FadeOut(AudioSource audioSource, float FadeTime)
    {
        float startVolume = audioSource.volume;

        while (audioSource.volume > 0)
        {
            audioSource.volume -= startVolume * Time.deltaTime / FadeTime;

            yield return null;
        }

        audioSource.Stop();
        audioSource.volume = startVolume;
        yield return null;
    }

    public static IEnumerator FadeOut(AudioSource audioSource, float FadeTime, AudioSource audioSourceToPlay, bool bossApproaches)
    {
        float startVolume = audioSource.volume;

        while (audioSource.volume > 0)
        {
            audioSource.volume -= startVolume * Time.deltaTime / FadeTime;

            yield return null;
        }

        audioSource.Stop();
        audioSource.volume = startVolume;
        audioSourceToPlay.Play();
        if (bossApproaches)
        {
            Observer.OnWarningAnimation();
            yield return new WaitForSeconds(13f);
            Observer.OnBossReady();
        }
        yield return null;
    }

    public static IEnumerator DelayStart(AudioSource audioSourceToStart, float DelayTime)
    {
        float delayTimer = 0f;
        while (delayTimer < DelayTime)
        {
            delayTimer += Time.deltaTime;
            yield return null;
        }
        audioSourceToStart.Play();
    }

    void BossReady()
    {
        this.bossTheme.Play();
    }

    void BossDead()
    {
        StartCoroutine(FadeOut(this.bossTheme, 1f));
        explosions.Play();
        bossDead.Play();
    }

    void BossCleanup()
    {
        ingame.Play();
    }

    void BossApproaches()
    {
        StartCoroutine(FadeOut(this.ingame, 1f, this.bossApproaches, bossApproaches));
    }

    void Grump(NPC npc)
    {
        grump.Play();
        StartCoroutine(FadeOut(ingame, 1f));
    }

    void StartMenu()
    {
        ingame.Play();
    }

    void GameTitleExited()
    {
        StartCoroutine(FadeOut(this.titleTheme, 1f));
        startMenu.Play();
    }

    void GameReset()
    {
        startMenu.Play();
    }

    void Bonus(NPC npc)
    {
        bonus.Play();
    }
}
