using Otter;
using System;

namespace SuperRefluxPowergridDefense_.entities
{
    class Tesla : Entity
    {
        private Spritemap<string> _spriteMap;
        private Rectangle rect;
        private bool isOn = false;
        private bool timeToFire = true;
        private int firingTimer = Global.teslaFiringSpeed;//was 300
        public int TeslaHealth = 100;


        public Tesla(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(15, 50, (int)Global.Type.TESLA);
            Collider.X = -6;
            Collider.Y = -26;
            rect = new Rectangle((int)X - 6, (int)Y - 26, 15, 50);
            _spriteMap = new Spritemap<string>(Assets.TESLA, 46, 56);
            _spriteMap.Add("off", new int[] { 0 }, new float[] { 10f, 10f });
            _spriteMap.Add("on", new int[] { 1, 2 }, new float[] { 10f, 10f });
            _spriteMap.Play("off");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
        }

        public override void Update()
        {
            base.Update();
            //it is assumed that this unit is ON.
            //when i want to use power, I need to have it request to consume power from the battery.
            var mouseLocation = new Vector2((int)Scene.MouseX, (int)Scene.MouseY);
            if (Util.InRect(mouseLocation, rect))
            {
                if (Global.PlayerSession.Controller.Button("LeftMouseClick").Pressed)
                {
                    SoundManager.PlayRequestedSound("_realClickyClick");
                    if (isOn)
                        isOn = false;
                    else
                        isOn = true;
                }
            }
            if (Global.Battery >= 1 && isOn)
            {
                if (_spriteMap.CurrentAnim != "on")
                {
                    Global.Battery--;
                    _spriteMap.Play("on");
                }
                if (Global.Battery >= 100 && timeToFire)
                {
                    Global.Battery -= 100;
                    timeToFire = false;
                    firingTimer = Global.teslaFiringSpeed;
                    Scene.Add(new TeslaBeam(X, Y-28));
                }
            }
            else
                _spriteMap.Play("off");

            if (firingTimer > 0 && isOn)
                firingTimer--;
            if (firingTimer <= 0 && isOn)
                timeToFire = true;

        }
        public override void Render()//mainly for testing colliders :D
        {
            base.Render();
            //Collider.Render();
        }

        public void Destroy()
        {
            RemoveSelf();
            Scene.Add(new PlayerDeath(X,Y));
        }

    }
}
