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

namespace PenniesAreGold.Utility
{
    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 _click = new Sound(Assets.SFX_CLICK);
        private Sound _buzzReady = new Sound(Assets.SFX_BUZZREADY);
        private Sound _click2 = new Sound(Assets.SFX_CLICK2);
        private Sound _death1 = new Sound(Assets.SFX_DEATH1);
        private Sound _death2 = new Sound(Assets.SFX_DEATH2);
        private Sound _death3 = new Sound(Assets.SFX_DEATH3);
        private Sound _death4 = new Sound(Assets.SFX_DEATH4);
        private Sound _death5 = new Sound(Assets.SFX_DEATH5);
        private Sound _death6 = new Sound(Assets.SFX_DEATH6);
        private Sound _death7 = new Sound(Assets.SFX_DEATH7);
        private Sound _niceClick = new Sound(Assets.SFX_NICECLICK);
        private Sound _pulse = new Sound(Assets.SFX_PULSE);
        private Sound _shot = new Sound(Assets.SFX_SHOT);
        //private Sound _leadBulletShot = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletDeath = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _pickUpAssaultRifle = new Sound(Assets.SFX_PICKUP_ASSAULTRIFLE);
        //private Sound _dropWeapon = new Sound(Assets.SFX_DROP_WEAPON);
        //private Sound _bigRockShot = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockDeath = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _ballRolling = new Sound(Assets.SFX_BALLROLLING);
        //private Sound _pickUpBallCannon = new Sound(Assets.SFX_PICKUP_BALLCANNON);
        //private Sound _enemyStartRoar = new Sound(Assets.SFX_ENEMY_START_ROAR);
        //private Sound _moiPowerUp = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _catStatuePowerUp = new Sound(Assets.SFX_CAT_STATUE_POWER_UP);
        //private Sound _moiDeath = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _playerDeath = new Sound(Assets.SFX_PLAYER_DEATH);
        //private Sound _playerHurt = new Sound(Assets.SFX_PLAYER_HURT);
        //private Sound _catDeath = new Sound(Assets.SFX_CAT_DEATH);
        //private Sound _ratNoise = new Sound(Assets.SFX_RATNOISE);
        //private Sound _musicTitle = new Sound(Assets.MUSIC_TITLE);
        //private Sound _badPassword = new Sound(Assets.SFX_BADPASSWORD);
        //private Sound _goodPassword = new Sound(Assets.SFX_PASSWORD_ENTERED);
        //private Sound _getALife = new Sound(Assets.MUSIC_GAIN_LIFE);
        //private Sound _introMusic = new Sound(Assets.MUSIC_GAME_START);
        //#region SoundInstances
        private static Sound locatedSound;
        //#region MoiDeath
        //private Sound _moiDeath1 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath2 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath3 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath4 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath5 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath6 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath7 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath8 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath9 = new Sound(Assets.SFX_MOI_DEATH);
        //private Sound _moiDeath10 = new Sound(Assets.SFX_MOI_DEATH);
        //#endregion
        //#region moiPowerUp
        //private Sound _moiPowerUp1 = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _moiPowerUp2 = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _moiPowerUp3 = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _moiPowerUp4 = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _moiPowerUp5 = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _moiPowerUp6 = new Sound(Assets.SFX_MOI_POWER_UP);
        //private Sound _moiPowerUp7 = new Sound(Assets.SFX_MOI_POWER_UP);
        //#endregion
        //#region LeadBulletShot
        //private Sound _leadBulletShot1 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot2 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot3 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot4 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot5 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot6 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot7 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot8 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot9 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot10 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot11 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot12 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot13 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot14 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot15 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot16 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot17 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot18 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot19 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //private Sound _leadBulletShot20 = new Sound(Assets.SFX_LEAD_BULLET_SHOT);
        //#endregion
        //#region LeadBulletDeath
        //private Sound _leadBulletDeath1 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath2 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath3 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath4 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath5 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath6 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath7 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath8 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath9 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //private Sound _leadBulletDeath10 = new Sound(Assets.SFX_LEAD_BULLET_DEATH);
        //#endregion
        //#region BigRockShot
        //private Sound _bigRockShot1 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot2 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot3 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot4 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot5 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot6 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot7 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot8 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot9 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //private Sound _bigRockShot10 = new Sound(Assets.SFX_BIG_ROCK_SHOT);
        //#endregion
        //#region BigRockDeath
        //private Sound _bigRockDeath1 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath2 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath3 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath4 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath5 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath6 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath7 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath8 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath9 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //private Sound _bigRockDeath10 = new Sound(Assets.SFX_BIG_ROCK_DEATH);
        //#endregion
        //#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)
                    {
                        if (i > 5)
                            volume = .5f;
                        locatedSound.Volume = volume;
                        locatedSound.Play();
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("playing the sound Failed");
            }
        }

        public void LoadSounds()
        {
            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 Moi Death
            //sounds.Add("_moiDeath", _moiDeath);
            //sounds.Add("_moiDeath1", _moiDeath1);
            //sounds.Add("_moiDeath2", _moiDeath2);
            //sounds.Add("_moiDeath3", _moiDeath3);
            //sounds.Add("_moiDeath4", _moiDeath4);
            //sounds.Add("_moiDeath5", _moiDeath5);
            //sounds.Add("_moiDeath6", _moiDeath6);
            //sounds.Add("_moiDeath7", _moiDeath7);
            //sounds.Add("_moiDeath8", _moiDeath8);
            //sounds.Add("_moiDeath9", _moiDeath9);
            //sounds.Add("_moiDeath10", _moiDeath10);
            //#endregion
        }
        #endregion
    }

}

