using GiraffeRoller.entities.bullets;
using GiraffeRoller.Utility;
using Otter;

namespace GiraffeRoller.entities
{
    class WaterTower : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool canHitWater;
        private int timer = Rand.Int(20, 40);
        private bool notTheWater;
        private bool notTheWaterResourvoir;

        public WaterTower(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(100, 200, Global.Type.WATERTOWER);
            _spriteMap = new Spritemap<string>(Assets.WATERTOWER, 100, 200);
            _spriteMap.Add("0", new int[] { 0, 1, 2, 3, 4, 5 }, new float[] { 10f, 10f });
            _spriteMap.Add("1", new int[] { 6, 7, 8, 9, 10, 11 }, new float[] { 10f, 10f });
            _spriteMap.Add("2", new int[] { 12, 13, 14, 15, 16, 17 }, new float[] { 10f, 10f });
            _spriteMap.Add("3", new int[] { 18, 19, 20, 21, 22, 23 }, new float[] { 10f, 10f });
            _spriteMap.Add("4", new int[] { 24, 25, 26, 27, 28, 29 }, new float[] { 10f, 10f });
            _spriteMap.Add("5", new int[] { 30, 31, 32, 33, 34, 35 }, new float[] { 10f, 10f });
            _spriteMap.Add("death", new int[] { 36, 37, 38, 39, 40, 41 }, new float[] { 18f, 18f });
            Graphic = _spriteMap;
            PlayCorrectSpritemap();
            //System.Console.WriteLine("layer: {0}", layer);
            Layer = 1;
        }

        public override void Update()
        {
            base.Update();
            PlayCorrectSpritemap();
            if (_spriteMap.CurrentAnim == "death" && _spriteMap.CurrentFrameIndex == 5)
            {
                Global.Score += 100;
                Global.PlayerHealth += 30;
                Global.EventTwoCanKillPower = true;
                RemoveSelf();
            }

            var chargingPlayer = Collider.Collide(X, Y, (int)Global.Type.PLAYER);
            if (chargingPlayer != null && Player.Charging && canHitWater)
            {
                canHitWater = false;
                Global.WaterHealth--;
                Global.Score += 10;
                Global.StopPlayerCharge = true;
                SoundManager.PlayRequestedSound(RandomHit(), 1.5f);
            }
            else if (chargingPlayer == null && !Player.Charging && !canHitWater)
            {
                canHitWater = true;
            }
            if (Timer > timer)
            {
                timer += Rand.Int(80, 300);
                for (var j = 0; j < 12; j++)
                {
                    Scene.Add(new WaterDrop(X + 50, Y + 50, this));
                }
            }
        }//end of update

        private string RandomHit()
        {
            switch (Rand.Int(1, 10))//one more than the last case number (so counting the default would be an additional one)
            {
                case 1:
                    return "_waterTowerHit";
                case 2:
                    return "_waterTowerHit2";
                case 3:
                    return "_waterTowerHit3";
                case 4:
                    return "_waterTowerHit4";
                case 5:
                    return "_waterTowerHit5";
                case 6:
                    return "_waterTowerHit6";
                case 7:
                    return "_waterTowerHit7";
                case 8:
                    return "_waterTowerHit8";
                default:
                    return "_waterTowerHit9";
            }
        }

        private void PlayCorrectSpritemap()
        {
            if (Global.WaterHealth == 6)
                Shared.ChangeAnimTo("0", _spriteMap);
            else if (Global.WaterHealth == 5)
            {
                Shared.ChangeAnimTo("1", _spriteMap);
                if (!notTheWater)
                {
                    notTheWater = true;
                    SoundManager.PlayRequestedSound("_bossNotTheWater", 2f);
                }
            }
            else if (Global.WaterHealth == 4)
                Shared.ChangeAnimTo("2", _spriteMap);
            else if (Global.WaterHealth == 3)
            {
                if (!notTheWaterResourvoir)
                {
                    notTheWaterResourvoir = true;
                    SoundManager.PlayRequestedSound("_bossNotTheWaterResouvoir", 2f);
                }
                Shared.ChangeAnimTo("3", _spriteMap);
            }
            else if (Global.WaterHealth == 2)
                Shared.ChangeAnimTo("4", _spriteMap);
            else if (Global.WaterHealth == 1)
                Shared.ChangeAnimTo("5", _spriteMap);
            else if (Global.WaterHealth <= 0)
                Shared.ChangeAnimTo("death", _spriteMap);
        }

        public override void Render()
        {
            base.Render();
            //Collider.Render();
            //RemoveSelf();
        }
    }
}