using Otter;
using System;

namespace SuperRefluxPowergridDefense_.entities
{
    class TeslaBeam : Entity
    {
        private Spritemap<string> _spriteMap;
        public int GeneratorHealth = 100;
        private bool isSeekingTarget = true;
        private float closestDistanceItem = 1650f;
        private Entity closestItem;
        private double angleInRadians = 1;//this is about 57 degrees. ;-)
        private float thisX = 0;
        private float thisY = 0;
        private float startingX = 0;
        private float startingY = 0;
        public int deathTimer = 30;
        private double angle = Otter.Rand.Angle;
        private int particleTimer = 10;
        private bool one = true;
        private bool two = true;

        public TeslaBeam(float x, float y)
        {
            X = x; Y = y;
            startingX = x; startingY = y;
            SetHitbox(10, 10, (int)Global.Type.TESLABEAM);
            Collider.X = -5;
            Collider.Y = -5;
            _spriteMap = new Spritemap<string>(Assets.SPARKLE, 20, 20);
            _spriteMap.Add("main", new int[] { 0, 1 , 2, 3, 4 }, new float[] { 10f, 10f });
            _spriteMap.Play("main");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
        }

        public override void Update()
        {
            base.Update();
            thisX = X; thisY = Y;

            if (isSeekingTarget)
            {
                ScanAllTargets();
            }
            else if (!AmICollidingWithScaryGirl())//if colliding with scary girl, murder her.
            {
                if (two)
                {
                    two = false;
                    SoundManager.PlayRequestedSound("_teslaFireBeam");
                }
                if (particleTimer <= 0)
                        Scene.Add(new BlueParticle(X, Y));
                if (particleTimer > 0)
                    particleTimer--;

                var speed = 9f;
                X -= speed * (float)Math.Cos(angleInRadians);
                Y -= speed * (float)Math.Sin(angleInRadians);
            }
            if (deathTimer > 0 && closestItem != null)
                deathTimer--;
            if (deathTimer <= 0)
                Destroy();
        }

        public void ScanAllTargets()
        {
            var anyScaryGirls = GetEntities<ScaryGirl>();
            foreach (var item in anyScaryGirls)
            {
                var dist = Util.Distance(this, item);
                if (dist <= closestDistanceItem)
                {
                    closestDistanceItem = dist;
                    closestItem = item;
                }
            }
            if (closestItem != null)
            {
                isSeekingTarget = false;
                GetDirection();
            }
            else
            {//put them in a circle around you.
                if (one)
                {
                    one = false;
                    SoundManager.PlayRequestedSound("_teslaBeamReady");
                }
                X = startingX - 10 * (float)Math.Cos(angle);//test angles (1)
                Y = startingY - 10 * (float)Math.Sin(angle);//test angles (1)
                angle+=.1f;
                if (angle >= 360)
                    angle = 1;
            }
        }

        public void GetDirection()
        {
            var vector1 = new Point((int)closestItem.X, (int)closestItem.Y);
            var vector2 = new Point((int)X, (int)Y);
            angleInRadians = Math.Atan2(vector2.Y - vector1.Y, vector2.X - vector1.X);
            var angleInFrisbees = MathHelper.ToDegrees((float)angleInRadians);
            if (angleInFrisbees < 0)
            {
                angleInFrisbees += 360;
                angleInRadians = MathHelper.ToRadians(angleInFrisbees);
            }
        }

        public double GetAngle(float angle)
        {
            return MathHelper.ToRadians(angle);
        }

        public bool AmICollidingWithScaryGirl()
        {
            var colGirl = Collider.Collide(X, Y, (int)Global.Type.SCARYGIRL);
            if (colGirl != null)
            {
                ScaryGirl scaryGirl = (ScaryGirl)colGirl.Entity;
                scaryGirl.Destroy();
                Destroy();
                return true;
            }
            else
                return false;
        }

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

        public void Destroy()
        {
            RemoveSelf();
        }
    }
}

