using KellysMarvelousAdventure.UI;
using KellysMarvelousAdventure.Utilities;
using Otter;

namespace KellysMarvelousAdventure.entities
{
    class CatShipSpawner : Entity
    {
        private Spritemap<string> _spriteMap;
        private int _spawnCatTimer;
        public bool once = true;
        private int health = 4;
        private Text text;
        private Text backgroundText;

        public CatShipSpawner(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(50, 50, (int)Global.Type.CATSHIPSPAWNER);
            _spriteMap = new Spritemap<string>(Assets.CATSHIPSPAWNER, 50, 50);
            _spriteMap.Add("idle", new int[] { 0, 1, 0, 2 }, new float[] { 7, 7 });
            _spriteMap.Add("spawnCat", new int[] { 6, 7, 8, 9, 10, 11 }, new float[] { 5, 5 });
            _spriteMap.Add("death", new int[] { 0, 6, 1, 11, 2, 1, 0, 6, 11 }, new float[] { 1, 1 });
            _spriteMap.Play("idle");
            Graphic = _spriteMap;
            _spawnCatTimer = Global.SpawnCatTimer;
            backgroundText = new Text(health.ToString(), 12);
            text = new Text(health.ToString(), 12);
            AddGraphic(backgroundText, -20, 0);
            Layer = 1;
        }
        public override void Update()
        {
            base.Update();
            if (_spawnCatTimer <= 0)
                ChangeAnimTo("spawnCat");
            if (_spawnCatTimer > 0)
                _spawnCatTimer--;
            if (_spriteMap.CurrentAnim == "spawnCat" && _spriteMap.CurrentFrameIndex == 5)
            {
                SoundManager.PlayRequestedSound("_beamDown");
                var rand = Rand.Int(1, 3);//has to be one greater at the end to get it. 0-1. It's always one LESS than the greatest number. Must count 0, 1.(which counts as 2?)
                //System.Console.WriteLine("rand: {0}", rand);
                if (rand == 1)
                    Scene.Add(new BlueCat(X+20, Y+20));
                _spawnCatTimer = Global.SpawnCatTimer;
                ChangeAnimTo("idle");
            }
            //collisions!
            var stair = Collider.Collide((int)X, (int)Y, (int)Global.Type.SAPPEXPLOSION);
            if (stair != null && once)
            {
                //okayt take damage, but only once per bomb drop.
                once = false;
                health--;
                if (health <= 0)
                    _spriteMap.Play("death");
                else
                {
                    var rand = Rand.Int(1, 5);
                    if (rand == 1)
                        SoundManager.PlayRequestedSound("_girlHaHaa");
                    if (rand == 2)
                        SoundManager.PlayRequestedSound("_girlHaHaHaa");
                    if (rand == 3)
                        SoundManager.PlayRequestedSound("_girlHaHaHaHaa");
                    if (rand == 4)
                        SoundManager.PlayRequestedSound("_girlTakeThat");
                    RemoveGraphic(backgroundText);
                    backgroundText = new Text(health.ToString(), 12);
                    AddGraphic(backgroundText, -20, 0);
                }

            }
            if (_spriteMap.CurrentAnim == "death" && _spriteMap.CurrentFrameIndex == 8)
            {
                SoundManager.PlayRequestedSound("_girlHaHaHaHaHaHaHaHaHa");
                Global.SpawnCatTimerMin -= 40;
                Global.SpawnCatTimerMax -= 40;
                Global.score += 50;
                Global.NumberOfShips--;
                RemoveSelf();
            }
        }
        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }
    }
}
