using KellysMarvelousAdventure.entities;
using Otter;

namespace KellysMarvelousAdventure.UI
{
    class SapBombs : Entity
    {
        private Spritemap<string> _spriteMap;
        private float startingPositionX = 16f;
        private float startingPositionY = 2f;
        private Rectangle rect;

        public SapBombs(float x, float y)
        {
            X = x; Y = y;
            startingPositionX = X;
            startingPositionY = Y;
            rect = new Rectangle((int)X, (int)Y, 50, 50);
            _spriteMap = new Spritemap<string>(Assets.SAPPBOMBSUI, 50, 50);
            _spriteMap.Add("off", new int[] { 0 });
            _spriteMap.Add("on", new int[] { 1 });
            _spriteMap.Play("off");
            Graphic = _spriteMap;
            Layer = -2;
        }
        public override void Update()
        {
            base.Update();
            X = Scene.CameraX + startingPositionX;
            Y = Scene.CameraY + startingPositionY;
            rect.X = (int)X;
            rect.Y = (int)Y;
            if (Global.SapBombs >= 1)
                ChangeAnimTo("on");
            else
                ChangeAnimTo("off");

            var mouseLocation = new Vector2((int)Scene.MouseX, (int)Scene.MouseY);
            if (Util.InRect(mouseLocation, rect))
            {
                //okay, if you are hovering over the tree to click it, not sure what will happen, but that's what this is supposed to handle
                //_spriteMap.Play("hover");
                //_spriteMap.Color = Color.Gold;
                if (Global.PlayerSession.Controller.Button("LeftMouseClick").Pressed)
                {
                    var anySapbombs = GetEntities<ActualSapBomb>();
                    if (Global.SapBombs >= 1 && anySapbombs != null)//meaning there isn't any
                    {
                        Global.SapBombs--;
                        if (Global.SapBombs < 8)
                        {
                            Global.SapBar -= Global.SapBar;//reverses the length of time.
                            Global.Increaser *= 2;//reverses the length of time.
                        }
                        var catships = GetEntities<CatShipSpawner>();//pick up a bomb, reset all ships so they can be hit again.
                        foreach (var item in catships)
                        {
                            item.once = true;
                        }
                        Scene.Add(new ActualSapBomb(X, Y));
                    }
                }
            }
        }//end of update
        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }
    }
}
