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

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

        public MiningFacility(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(45, 50, (int)Global.Type.MININGFACILITY);
            Collider.X = -20;
            Collider.Y = -25;
            _spriteMap = new Spritemap<string>(Assets.MININGFACILITY, 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 (MineButton.IsMiningBuildingOnMouse)
                    SoundManager.PlayRequestedSound("_niceClick");
                MineButton.IsMiningBuildingOnMouse = false;
            }
            if (_isPlaced && _timer <= 0)
            {
                _timer = 500;
                Scene.Add(new Penny(X+19, Y+5));
            }
            //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()
        {
            var mineButton = GetEntity<MineButton>();//there's only one of these so this will work!
            mineButton.miningTimerStart /= 2;
            this.RemoveSelf();
        }
        public override void Render()
        {
            base.Render();
            if (!_isPlaced)
                Collider.Render(Color.Green);
            if (AmICollidingWithButtonsOrBuildings(Collider))
                Collider.Render(Color.Red);
        }
    }
}
