using Otter;
using PenniesAreGold.player;
using PenniesAreGold.Utility;
using System;

namespace PenniesAreGold.objects
{
    class EnemyTroop : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool isSeekingTarget = true;
        private float closestDistanceItem = 3650f;
        private Entity closestItem;
        private double angleInRadians = 1;//this is about 57 degrees. ;-)
        private bool isHittingTarget = false;
        private int scanAgain = 100;
        private float troopX = 0;
        private float troopY = 0;
        private int firstTimer = 10;
        private int hitTimer = Rand.Int(40,50);
        private Collider troop;//if needed
        private bool one = true;

        public int EnemyTroopHealth = 10;//hmmm half of the current coins + 10?

        public EnemyTroop(float x, float y)
        {
            X = x; Y = y;
            X = x; Y = y;
            SetHitbox(10, 10, (int)Global.Type.ENEMYTROOP);
            Collider.X = -7;
            Collider.Y = -7;
            _spriteMap = new Spritemap<string>(Assets.ENEMYTROOP, 20, 20);
            _spriteMap.Add("standRight", new int[] { 1 }, new float[] { 5f, 5f });
            _spriteMap.Add("standLeft", new int[] { 0 }, new float[] { 5f, 5f });
            _spriteMap.Add("walkRight", new int[] { 1, 2 }, new float[] { 5f, 5f });
            _spriteMap.Add("walkLeft", new int[] { 0, 3 }, new float[] { 5f, 5f });
            _spriteMap.Add("shootRight", new int[] { 4, 5, }, new float[] { 5f, 5f });
            _spriteMap.Add("shootLeft", new int[] { 6, 7 }, new float[] { 5f, 5f });
            _spriteMap.Add("death", new int[] { 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }, new float[] { 5f, 5f });
            _spriteMap.Play("standLeft");
            _spriteMap.Color = Color.Random;
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
            //angleInRadians = GetAngle(Rand.Float(0f, 359f));
        }

        public override void Update()
        {
            base.Update();
            troopX = X; troopY = Y;
            if (_spriteMap.CurrentAnim == "death") //if you are dying, do it. don't do anything else!
            {
                if (_spriteMap.CurrentFrame == 11)
                {
                    Global.PlayTroopDeathSound();
                    this.RemoveSelf();
                    Global.kills++;
                    Global.Score++;
                }
            }
            else
            {
                if (isSeekingTarget)
                {
                    ScanAllTargets();
                }
                else if (!AmICollidingWithMyFriends())//if colliding with myself, move away.
                {
                    if (!AmICollidingWithWalls())
                    {
                        var speed = .5f;
                        X -= speed * (float)Math.Cos(angleInRadians);
                        Y -= speed * (float)Math.Sin(angleInRadians);
                        ChangeAnimTo("walkRight");
                        one = true;
                    }
                }
                else if (!AmCloseEnoughToAnythingToShootIt())//travel to target
                {
                    X -= Global.troopSpeed * (float)Math.Cos(angleInRadians);
                    Y -= Global.troopSpeed * (float)Math.Sin(angleInRadians);
                    ChangeAnimTo("walkLeft");
                    one = true;
                }
                else if (isHittingTarget)
                {
                    if (one || hitTimer <= 0)
                    {
                        hitTimer = Rand.Int(40, 50);
                        one = false;
                        Scene.Add(new Bullet(X, Y, closestItem));
                        SoundManager.PlayRequestedSound("_shot");
                    }
                    ChangeAnimTo("shootRight");
                    hitTimer--;
                }
                if (scanAgain <= 0)
                {
                    isSeekingTarget = true;
                    closestItem = null;
                    closestDistanceItem = 1670;
                    scanAgain = 100;
                }
                if (scanAgain > 0)
                    scanAgain--;
                //collide with yourself technology
                if (firstTimer > 0)
                    firstTimer--;
                if (firstTimer <= 0)
                {
                    if (troop != null)//you are still colliding with another girl. get a new angle.
                    {
                        angleInRadians = GetAngle(Rand.Float(0f, 359f));
                        firstTimer = 100;
                    }
                }
            }
        }

        public void ScanAllTargets()
        {
            var anyTeslas = GetEntities<Troop>();
            foreach (var item in anyTeslas)
            {
                var dist = Util.Distance(this, item);
                if (dist <= closestDistanceItem)
                {
                    closestDistanceItem = dist;
                    closestItem = item;
                }
            }

            var anyPlayer = GetEntities<Player>();
            foreach (var item in anyPlayer)
            {
                var dist = Util.Distance(this, item);
                if (dist <= closestDistanceItem)
                {
                    closestDistanceItem = dist;
                    closestItem = item;
                }
            }

            var anyGenerator = GetEntities<TroopBuilding>();
            foreach (var item in anyGenerator)
            {
                var dist = Util.Distance(this, item);
                if (dist <= closestDistanceItem)
                {
                    closestDistanceItem = dist;
                    closestItem = item;
                }
            }
            var anyFuel = GetEntities<MiningFacility>();
            foreach (var item in anyFuel)
            {
                var dist = Util.Distance(this, item);
                if (dist <= closestDistanceItem)
                {
                    closestDistanceItem = dist;
                    closestItem = item;
                }
            }
            if (closestItem != null)
            {
                isSeekingTarget = false;
                GetDirection();
            }
        }

        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 AmICollidingWithMyFriends()
        {
            troop = Collider.Collide(X, Y, (int)Global.Type.ENEMYTROOP);
            if (troop != null)
                return true;
            else
                return false;
        }

        public bool AmICollidingWithWalls()
        {
            var wall = Collider.Collide(X, Y, (int)Global.Type.SOLID);
            if (wall != null)
                return true;
            else
                return false;
        }

        public bool AmCloseEnoughToAnythingToShootIt()
        {
            //get the distance to closest item. if close shoot at it!
            var dist = Util.Distance(this, closestItem);
            if (dist <= 100)
            { 
                isHittingTarget = true;
                return true;
            }
            else
            {
                isHittingTarget = false;
                return false;
            }
        }

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

        public void Destroy()
        {
            if (_spriteMap.CurrentAnim != "death")
                _spriteMap.Play("death");
        }

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