using Otter;
using System;

namespace SuperRefluxPowergridDefense_.entities
{
    class Generator : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool isOn = false;
        private Rectangle rect;
        public int _fuel = 0;
        private float _fuelTimer = 0;
        public int GeneratorHealth = 100;

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

        public override void Update()
        {
            base.Update();
            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 (isOn && _fuelTimer > 0)
            {
                if (_spriteMap.CurrentAnim != "on")
                    _spriteMap.Play("on");
                Global.TotalGlobalPower += 1;//was 1
            }
            else
                _spriteMap.Play("off");

            if (_fuel >= 0 && isOn)//we have fuel. decrease fuel timer.
                _fuelTimer--;

            if (isOn && _fuel > 0 && _fuelTimer <= 0)//we have fuel, but timer is zero.
            {
                _fuelTimer = 1000;//refil
                _fuel--;//reduce by one fuel.
            }
            else if (_fuel <= 0 && _fuelTimer <= 0)//we have no fuel, and the timer just ran out.
            {
                _fuelTimer = 0;
                isOn = false;
            }
            //Console.WriteLine("_fuel: {0}", _fuel);
            //Console.WriteLine("_fuelTimer: {0}", _fuelTimer);

        }

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

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