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

namespace GiraffeRoller.entities
{
    class Warehouse : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool canHitWarehouse = true;
        private float timer = 0;
        private float closestDistanceItem = 3650f;
        private Entity closestItem;
        private bool death;

        public Warehouse(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(300, 300, Global.Type.WAREHOUSE);
            _spriteMap = new Spritemap<string>(Assets.WAREHOUSE, 300, 300);
            _spriteMap.Add("0", new int[] { 0 }, new float[] { 10f, 10f });
            _spriteMap.Add("1", new int[] { 1 }, new float[] { 10f, 10f });
            _spriteMap.Add("2", new int[] { 2 }, new float[] { 10f, 10f });
            _spriteMap.Add("3", new int[] { 3 }, new float[] { 10f, 10f });
            _spriteMap.Add("4", new int[] { 4 }, new float[] { 10f, 10f });
            _spriteMap.Add("5", new int[] { 5 }, new float[] { 10f, 10f });
            _spriteMap.Add("6", new int[] { 6 }, new float[] { 7f, 7f });
            _spriteMap.Add("7", new int[] { 7 }, new float[] { 7f, 7f });
            _spriteMap.Add("8", new int[] { 8 }, new float[] { 7f, 7f });
            _spriteMap.Add("death", new int[] { 9, 10, 11, 12, 13, 14, 15 }, 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 == 6)
            {
                Global.Score += 300;
                Global.PlayerHealth += 50;
                Global.EventThreeCanKillWarehouse = false;
                Global.EventFourCanKillBoss = true;
                RemoveSelf();
            }
            if (Global.EventThreeCanKillWarehouse)
            {
                var chargingPlayer = Collider.Collide(X, Y, (int)Global.Type.PLAYER);
                if (chargingPlayer != null && Player.Charging && canHitWarehouse)
                {
                    canHitWarehouse = false;
                    Global.WarehouseHealth--;
                    Global.Score += 10;
                    Global.StopPlayerCharge = true;
                    SoundManager.PlayRequestedSound(RandomHit());
                }
                else if (chargingPlayer == null && !Player.Charging && !canHitWarehouse)
                {
                    canHitWarehouse = true;
                }
            }
            if (Timer > timer)
            {
                timer += Rand.Int(1, 80);
                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 ConcreteBlock(X + Rand.Int(0, 300), Y + Rand.Int(0, 300), closestItem));
                }
            }
        }//end of update

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

        private void PlayCorrectSpritemap()
        {
            if (Global.WarehouseHealth == 9)
                Shared.ChangeAnimTo("0", _spriteMap);
            else if (Global.WarehouseHealth == 8)
                Shared.ChangeAnimTo("1", _spriteMap);
            else if (Global.WarehouseHealth == 7)
                Shared.ChangeAnimTo("2", _spriteMap);
            else if (Global.WarehouseHealth == 6)
                Shared.ChangeAnimTo("3", _spriteMap);
            else if (Global.WarehouseHealth == 5)
                Shared.ChangeAnimTo("4", _spriteMap);
            else if (Global.WarehouseHealth == 4)
                Shared.ChangeAnimTo("5", _spriteMap);
            else if (Global.WarehouseHealth == 3)
                Shared.ChangeAnimTo("6", _spriteMap);
            else if (Global.WarehouseHealth == 2)
                Shared.ChangeAnimTo("7", _spriteMap);
            else if (Global.WarehouseHealth == 1)
                Shared.ChangeAnimTo("8", _spriteMap);
            else if (Global.WarehouseHealth <= 0)
            {
                if (!death)
                {
                    death = true;
                    SoundManager.PlayRequestedSound("_warehouseDeath");
                }
                Shared.ChangeAnimTo("death", _spriteMap);
            }
        }

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