using GiraffeRoller.entities.enemies;
using GiraffeRoller.Utility;
using Otter;
using System;

namespace GiraffeRoller.entities
{
    class TestPowerShot : 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 startingX = 0;
        private float startingY = 0;
        public int deathTimer = 200;
        private double angle = Rand.Angle;
        private int particleTimer = 10;
        private bool one = true;
        private bool two = true;

        public TestPowerShot(float x, float y)
        {
            X = x; Y = y;
            startingX = x; startingY = y;
            SetHitbox(10, 10, (int)Global.Type.WATERDROP);
            Collider.X = -5;
            Collider.Y = -5;
            _spriteMap = new Spritemap<string>(Assets.POWERDROP, 10, 10);
            _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();

            if (isSeekingTarget)
            {
                ScanAllTargets();
            }
            else
            {
                if (two)
                {
                    two = false;
                    SoundManager.PlayRequestedSound("_powerDropShot");
                }
                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();
            HandleCollisions();
        }//end of update

        public void ScanAllTargets()
        {
            var anyScaryGirls = GetEntities<Player>();
            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("_powerDropShot");
                }
                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 HandleCollisions()//lets try this. I hope this works. It isn't. :O
        {
            var colt = Collider.Collide((int)X, (int)Y, Global.Type.PLAYER);
            if (colt != null)
            {
                Global.PlayerHealth -= 20;
                Global.Score -= 20;
                SoundManager.PlayRequestedSound(Shared.RandomBossLaugh(), .8f);
                Destroy();
            }
            var cart = Collider.Collide((int)X, (int)Y, Global.Type.SOLID);
            if (cart != null)
            {
                Destroy();
            }

            var colg = Collider.Collide((int)X, (int)Y, Global.Type.WILDGIRAFFE);
            var colrg = Collider.Collide((int)X, (int)Y, Global.Type.RUNNINGWILDGIRAFFE);
            if (colg != null || colrg != null)
            {
                WildGiraffe tr = (WildGiraffe)colg.Entity;
                tr.Health--;
                if (tr.Health <= 0)
                {
                    SoundManager.PlayRequestedSound("_giraffeDying", .5f);
                    tr.Destroy();
                }
                //play destruction sound here;
                //Console.WriteLine("hitting civiilaran");
                Destroy();
            }
        }

        public void GetDirection()
        {
            var vector1 = new Point((int)closestItem.X + 50, (int)closestItem.Y + 50);
            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 override void Render()//mainly for testing colliders :D
        {
            base.Render();
            //Collider.Render();
        }

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

