using GiraffeRoller.Utility;
using Otter;

namespace GiraffeRoller.entities
{
    class Power : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool canHitPower = true;
        private int timer = Rand.Int(20, 40);
        private float closestDistanceItem = 3650f;
        private Entity closestItem;
        private bool getAwayFromPower;

        private bool death;

        public Power(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(100, 100, Global.Type.POWER);
            _spriteMap = new Spritemap<string>(Assets.POWER, 100, 100);
            _spriteMap.Add("0", new int[] { 0, 1 }, new float[] { 10f, 10f });
            _spriteMap.Add("1", new int[] { 2, 3 }, new float[] { 10f, 10f });
            _spriteMap.Add("2", new int[] { 4, 5 }, new float[] { 10f, 10f });
            _spriteMap.Add("3", new int[] { 6, 7 }, new float[] { 10f, 10f });
            _spriteMap.Add("4", new int[] { 8, 9 }, new float[] { 10f, 10f });
            _spriteMap.Add("5", new int[] { 10, 11 }, new float[] { 10f, 10f });
            _spriteMap.Add("6", new int[] { 12, 13 }, new float[] { 7f, 7f });
            _spriteMap.Add("death", new int[] { 12, 13, 14, 15, 16, 17, 18, 19 }, new float[] { 17f, 17f });
            Graphic = _spriteMap;
            PlayCorrectSpritemap();
            //System.Console.WriteLine("layer: {0}", layer);
            Layer = 1;
        }

        public override void Update()
        {
            base.Update();
            PlayCorrectSpritemap();
            if (_spriteMap.CurrentAnim == "death" && _spriteMap.CurrentFrameIndex == 7)
            {
                Global.Score += 200;
                Global.PlayerHealth += 40;
                Global.EventTwoCanKillPower = false;
                Global.EventThreeCanKillWarehouse = true;
                RemoveSelf();
            }
            if (Global.EventTwoCanKillPower)
            {
                var chargingPlayer = Collider.Collide(X, Y, (int)Global.Type.PLAYER);
                if (chargingPlayer != null && Player.Charging && canHitPower)
                {
                    canHitPower = false;
                    Global.PowerHealth--;
                    Global.Score += 20;
                    Global.StopPlayerCharge = true;
                    SoundManager.PlayRequestedSound(RandomHit());
                }
                else if (chargingPlayer == null && !Player.Charging && !canHitPower)
                {
                    canHitPower = true;
                }
            }
            if (Timer > timer)
            {
                timer += Rand.Int(80, 230);
                var anyTeslas = GetEntities<Player>();
                foreach (var item in anyTeslas)
                {
                    var dist = Util.Distance(this, item);
                    if (dist <= closestDistanceItem)
                    {
                        closestDistanceItem = dist;
                        closestItem = item;
                    }
                }
                for (var j = 0; j < 1; j++)
                {
                    //Scene.Add(new PowerDrop(X + 35, Y + 10, closestItem));
                    Scene.Add(new TestPowerShot(X + 35, Y + 10));
                }
            }
        }

        private string RandomHit()
        {
            switch (Rand.Int(1, 4))//one more than the last case number (so counting the default would be an additional one)
            {
                case 1:
                    return "_metalHit";
                case 2:
                    return "_metalHit2";
                case 3:
                    return "_metalHit3";
                default:
                    return "_metalHit4";
            }
        }

        private void PlayCorrectSpritemap()
        {
            if (Global.PowerHealth == 7)
                Shared.ChangeAnimTo("0", _spriteMap);
            else if (Global.PowerHealth == 6)
            {
                if (!getAwayFromPower)
                {
                    getAwayFromPower = true;
                    SoundManager.PlayRequestedSound("_bossGetAwayThePower", 2f);
                }
                Shared.ChangeAnimTo("1", _spriteMap);
            }
            else if (Global.PowerHealth == 5)
                Shared.ChangeAnimTo("2", _spriteMap);
            else if (Global.PowerHealth == 4)
                Shared.ChangeAnimTo("3", _spriteMap);
            else if (Global.PowerHealth == 3)
                Shared.ChangeAnimTo("4", _spriteMap);
            else if (Global.PowerHealth == 2)
                Shared.ChangeAnimTo("5", _spriteMap);
            else if (Global.PowerHealth == 1)
                Shared.ChangeAnimTo("6", _spriteMap);
            else if (Global.PowerHealth <= 0)
            {
                if (!death)
                {
                    death = true;
                    SoundManager.PlayRequestedSound("_powerPlantDeath");
                }
                Shared.ChangeAnimTo("death", _spriteMap);
            }
        }

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