using Otter;
using PenniesAreGold.ui;
using PenniesAreGold.Utility;

namespace PenniesAreGold.objects
{
    class TroopBuilding : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool _isPlaced = false;
        //private bool _addOne = true;
        private int _timer = 0;
        public int TroopBuildingHealth = 100;

        public TroopBuilding(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(45, 50, (int)Global.Type.TROOPBUILDING);
            Collider.X = -20;
            Collider.Y = -25;
            _spriteMap = new Spritemap<string>(Assets.TROOPBUILDING, 45, 50);
            _spriteMap.Add("spin", new int[] { 0, 1, 2, 3 }, new float[] { 20f, 20f });
            _spriteMap.Add("faster", new int[] { 3, 2, 1, 0 }, new float[] { 10f, 10f });
            _spriteMap.Play("spin");
            Graphic = _spriteMap;
            _spriteMap.CenterOrigin();
            _spriteMap.Color = Color.Green;
        }

        public override void Update()
        {
            base.Update();
            if (!_isPlaced)
            {
                X = Scene.MouseX;
                Y = Scene.MouseY;
                if (AmICollidingWithButtonsOrBuildings(Collider))
                {
                    _spriteMap.Color = Color.Red;
                }
                else
                    _spriteMap.Color = Color.Green;
            }
            if (Global.PlayerSession.Controller.Button("LeftMouseClick").Pressed && !AmICollidingWithButtonsOrBuildings(Collider))
            {
                _isPlaced = true;
                _spriteMap.Color = Color.White;
                if (TroopButton.IsTroopBuildingOnMouse)
                    SoundManager.PlayRequestedSound("_niceClick");
                TroopButton.IsTroopBuildingOnMouse = false;
            }
            if (_isPlaced && _timer <= 0)
            {
                _timer = 400;
                Scene.Add(new Troop(X + 15, Y + 7));
            }
            //if (_addOne && _isPlaced)
            //{
            //    _addOne = false;
            //    Scene.Add(new MiningFacility(0, 0));
            //}
            if (_timer > 0) _timer--;
        }

        public bool AmICollidingWithButtonsOrBuildings(Collider c)
        {
            var collT = c.Collide(X, Y, (int)Global.Type.TROOPBUILDING);
            var collTB = c.Collide(X, Y, (int)Global.Type.TROOPBUTTON);
            var collMF = c.Collide(X, Y, (int)Global.Type.MININGFACILITY);
            var collMB = c.Collide(X, Y, (int)Global.Type.MININGBUTTON);
            if (collT != null || collTB != null || collMF != null || collMB != null)
                return true;
            else
                return false;
        }
        public void Destroy()
        {
            Global.TroopBuildingCost -= 1;
            this.RemoveSelf();
        }
        public override void Render()
        {
            base.Render();
            if (!_isPlaced)
                Collider.Render(Color.Green);
            if (AmICollidingWithButtonsOrBuildings(Collider))
                Collider.Render(Color.Red);
        }
    }
}

