using Otter;
using System;
using System.Collections.Generic;

namespace KellysMarvelousAdventure.Utilities
{
    public class SoundManager
    {
        #region Public Static
        public static Dictionary<string, Sound> sounds = new Dictionary<string, Sound>();
        public static bool isSoundEnabled = true;
        #endregion

        #region Private Variables
        private Sound _punchTree = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _wurgely = new Sound(Assets.SFX_WURGELY);
        private Sound _wurgely2 = new Sound(Assets.SFX_WURGELY2);
        private Sound _wurgely3 = new Sound(Assets.SFX_WURGELY3);
        private Sound _wurgely4 = new Sound(Assets.SFX_WURGELY4);
        private Sound _wagonHit = new Sound(Assets.SFX_WAGONHIT);
        private Sound _wagonHit2 = new Sound(Assets.SFX_WAGONHIT2);
        private Sound _wagonHit3 = new Sound(Assets.SFX_WAGONHIT3);
        private Sound _wagonHoldUp = new Sound(Assets.SFX_WAGONHOLDUP);
        private Sound _catDeath = new Sound(Assets.SFX_CATDEATH);
        private Sound _doorOpen = new Sound(Assets.SFX_DOOROPEN);
        private Sound _sappExplosion = new Sound(Assets.SFX_SAPPEXPLOSION);
        private Sound _beamDown = new Sound(Assets.SFX_BEAMDOWN);
        private Sound _girlILoveMyFarm = new Sound(Assets.SFX_GIRLILOVEMYFARM);
        private Sound _girlHowIsThisGoinaHelpMe = new Sound(Assets.SFX_GIRLHOWISTHISGOINAHELPME);
        private Sound _girlGadZooks = new Sound(Assets.SFX_GIRLGADZOOKS);
        private Sound _girlHaHaa = new Sound(Assets.SFX_GIRLHAHAA);
        private Sound _girlHaHaHaa = new Sound(Assets.SFX_GIRLHAHAHAA);
        private Sound _girlHaHaHaHaa = new Sound(Assets.SFX_GIRLHAHAHAHAA);
        private Sound _girlHaHaHaHaHaHaHaHaHa = new Sound(Assets.SFX_GIRLHAFOREVER);
        private Sound _girlTakeThat = new Sound(Assets.SFX_GIRLTAKETHAT);
        private Sound _girlDeath = new Sound(Assets.SFX_GIRLDEATH);
        private Sound _girlOw = new Sound(Assets.SFX_GIRLOW);
        private Sound _girlWhatDidYouKnew = new Sound(Assets.SFX_GIRLWHATDIDYOUKNEW);
        private Sound _oldManPleaseTakeMyWagon = new Sound(Assets.SFX_OLDMANPLEASETAKEMYWAGON);
        private Sound _oldManSlowLaugh = new Sound(Assets.SFX_OLDMANSLOWLAUGH);

        //#region SoundInstances
        private static Sound locatedSound;
        #region PunchTree
        private Sound _punchTree1 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree2 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree3 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree4 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree5 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree6 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree7 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree8 = new Sound(Assets.SFX_PUNCHTREE);
        private Sound _punchTree9 = new Sound(Assets.SFX_PUNCHTREE);
        #endregion
        #endregion

        public SoundManager()
        {
            LoadSounds();
        }
        #region Public Properties
        public bool SoundIsEnabled
        {
            get
            {
                return isSoundEnabled;
            }

            set
            {
                isSoundEnabled = value;
            }
        }
        #endregion

        #region Public Methods and Operators
        public static void PlayRequestedSound(string sound, float volume = 1.0f)
        {
            //get sound.
            //if sound is playing, get the sound again and set it to a new instance,
            //if new sound is playing, get the sound again and set it to a new instance,
            //if new sound is playing, get the sound again and...
            try
            {
                for (var i = 1; i < 20; i++)//max sounds is the biggest number in there
                {
                    sounds.TryGetValue(sound, out locatedSound);
                    if (locatedSound.IsPlaying)
                    {
                        //Console.WriteLine("sound " + i + " is playing.");
                        if (i != 1)
                        {
                            if (i.ToString().Length > 1)
                                sound = sound.TrimEnd(sound[sound.Length - 2]);
                            else
                                sound = sound.TrimEnd(sound[sound.Length - 1]);
                        }
                        sound = sound + i.ToString();
                        //Console.WriteLine("sound?: " + sound);
                        sounds.TryGetValue(sound, out locatedSound);
                    }
                    else if (isSoundEnabled)
                    {
                        //i need the volume of the sound to decrease the more you have playing at the same time.
                        //so if 1 full sound. 2 a little less, 3 etc.
                        #region SetSoundVolumeBasedOnNumberOfSoundsPlayingAtOnce
                        if (i == 1)
                            volume = 1f;
                        else if (i == 2)
                            volume = .5f;
                        else if (i == 3)
                            volume = .3f;
                        else if (i == 4)
                            volume = .1f;
                        else if (i == 5)
                            volume = .05f;
                        else if (i == 6)
                            volume = .04f;
                        else if (i >= 7)
                            volume = .03f;
                        #endregion
                        locatedSound.Volume = volume;
                        locatedSound.Play();
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("playing the sound Failed");
            }
        }

        public void LoadSounds()
        {
            sounds.Add("_wurgely", _wurgely);
            sounds.Add("_wurgely2", _wurgely2);
            sounds.Add("_wurgely3", _wurgely3);
            sounds.Add("_wurgely4", _wurgely4);
            sounds.Add("_wagonHit", _wagonHit);
            sounds.Add("_wagonHit2", _wagonHit2);
            sounds.Add("_wagonHit3", _wagonHit3);
            sounds.Add("_wagonHoldUp", _wagonHoldUp);
            sounds.Add("_catDeath", _catDeath);
            sounds.Add("_doorOpen", _doorOpen);
            sounds.Add("_girlILoveMyFarm", _girlILoveMyFarm);
            sounds.Add("_girlGadZooks", _girlGadZooks);
            sounds.Add("_girlHaHaa", _girlHaHaa);
            sounds.Add("_girlHaHaHaa", _girlHaHaHaa);
            sounds.Add("_girlHaHaHaHaa", _girlHaHaHaHaa);
            sounds.Add("_girlHaHaHaHaHaHaHaHaHa", _girlHaHaHaHaHaHaHaHaHa);
            sounds.Add("_girlTakeThat", _girlTakeThat);
            sounds.Add("_girlHowIsThisGoinaHelpMe", _girlHowIsThisGoinaHelpMe);
            sounds.Add("_oldManPleaseTakeMyWagon", _oldManPleaseTakeMyWagon);
            sounds.Add("_sappExplosion", _sappExplosion);
            sounds.Add("_beamDown", _beamDown);
            sounds.Add("_girlDeath", _girlDeath);
            sounds.Add("_girlOw", _girlOw);
            sounds.Add("_girlWhatDidYouKnew", _girlWhatDidYouKnew);
            sounds.Add("_oldManSlowLaugh", _oldManSlowLaugh);
        /*sounds.Add("_click", _click);
        /*sounds.Add("_click", _click);
        sounds.Add("_buzzReady", _buzzReady);
        sounds.Add("_death1", _death1);
        sounds.Add("_death2", _death2);
        sounds.Add("_death3", _death3);
        sounds.Add("_death4", _death4);
        sounds.Add("_death5", _death5);
        sounds.Add("_death6", _death6);
        sounds.Add("_death7", _death7);
        sounds.Add("_niceClick", _niceClick);
        sounds.Add("_pulse", _pulse);
        sounds.Add("_shot", _shot);*/
        //sounds.Add("_pickUpAssaultRifle", _pickUpAssaultRifle);
        #region PunchTree
        sounds.Add("_punchTree", _punchTree);
            sounds.Add("_punchTree1", _punchTree1);
            sounds.Add("_punchTree2", _punchTree2);
            sounds.Add("_punchTree3", _punchTree3);
            sounds.Add("_punchTree4", _punchTree4);
            sounds.Add("_punchTree5", _punchTree5);
            sounds.Add("_punchTree6", _punchTree6);
            sounds.Add("_punchTree7", _punchTree7);
            sounds.Add("_punchTree8", _punchTree8);
            sounds.Add("_punchTree9", _punchTree9);
            #endregion
        }
        #endregion
    }

}

