using Otter;
using System;

namespace SuperRefluxPowergridDefense_.entities
{
    class Fuel : Entity
    {
        private Spritemap<string> _spriteMap;
        private Rectangle rect;

        private double angleInRadians = 1;
        private double reverseAngleInRadians = 1;
        private int firstTimer = 100;
        public int FuelHealth = 100;

        public Fuel(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(10, 10, (int)Global.Type.FUEL);
            Collider.X = -5;
            Collider.Y = -5;
            rect = new Rectangle((int)X, (int)Y, 20, 20);
            _spriteMap = new Spritemap<string>(Assets.FUEL, 20, 20);
            _spriteMap.Add("stop", new int[] { 0 }, new float[] { 1f, 1f });
            _spriteMap.Add("run", new int[] { 0, 1 }, new float[] { 10f, 10f });
            _spriteMap.Add("die", new int[] { 2 }, new float[] { 1f, 1f });
            _spriteMap.Play("run");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
            angleInRadians = GetAngle(Otter.Rand.Float(0f, 359f));
        }

        public override void Update()
        {
            base.Update();
            var fuell = Collider.Collide(X, Y, (int)Global.Type.FUEL);
            if (fuell != null)
            {
                var speed = 1f;
                X -= speed * (float)Math.Cos(angleInRadians);
                Y -= speed * (float)Math.Sin(angleInRadians);
                ChangeAnimTo("run");
            }
            else
                ChangeAnimTo("stop");//has a built in check for if it's already playing.
            //collide with anything else, reverse direction.
            var colBullet = Collider.Collide(X, Y, (int)Global.Type.BULLET);
            var colSolid = Collider.Collide(X, Y, (int)Global.Type.SOLID);
            var colGenerator = Collider.Collide(X, Y, (int)Global.Type.GENERATOR);
            var colTesla = Collider.Collide(X, Y, (int)Global.Type.TESLA);
            var colGrid = Collider.Collide(X, Y, (int)Global.Type.GRID);
            var colDown = Collider.Collide(X, Y, (int)Global.Type.WALLDOWN);
            var colUp = Collider.Collide(X, Y, (int)Global.Type.WALLLEFT);
            var colRight = Collider.Collide(X, Y, (int)Global.Type.WALLRIGHT);
            var colLeft = Collider.Collide(X, Y, (int)Global.Type.WALLLEFT);

            if (colBullet != null || colSolid != null || colGenerator != null
             || colTesla != null || colGrid != null || colDown != null || colUp != null
              || colRight != null || colLeft != null)
            {
                var speed = 1f;
                X -= speed * (float)Math.Cos(reverseAngleInRadians);
                Y -= speed * (float)Math.Sin(reverseAngleInRadians);
                ChangeAnimTo("run");
            }
            else
                ChangeAnimTo("stop");
            if (firstTimer > 0)
                firstTimer--;
            if (firstTimer <= 0)
            {
                if (fuell != null)//you are still colliding with fuel. get a new angle.
                {
                    angleInRadians = GetAngle(Otter.Rand.Float(0f, 359f));
                    firstTimer = 100;
                    ChangeAnimTo("run");
                }
            }
        }

        public double GetAngle(float angle)
        {
            var tempAngle = MathHelper.ToRadians(angle);
            reverseAngleInRadians = MathHelper.ToRadians(angle + 180);
            return tempAngle;
        }

        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }

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

        public override void Render()
        {
            base.Render();
            //Collider.Render(Otter.Color.Black);
        }
    }
}
