using LOoSE_sLOTS.objects;
using LOoSE_sLOTS.Utilities;
using Otter;
using System;

namespace LOoSE_sLOTS.entities
{
    class Sniper : 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 Health = 10;//hmmm half of the current coins + 10? hmm.

        public Sniper(float x, float y)
        {
            X = x; Y = y;
            X = x; Y = y;
            SetHitbox(8, 20, (int)Global.Type.SNIPER);
            Collider.X = -5;
            Collider.Y = -10;
            _spriteMap = new Spritemap<string>(Assets.SNIPER, 50, 50);
            _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 }, 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)
                {
                    SoundManager.PlayRequestedSound("_iDontWantToDie");
                    Scene.Add(new GameCoin(X, Y, Y + 12));
                    Scene.Add(new GameCoin(X, Y, Y + 12));
                    Scene.Add(new GameCoin(X, Y, Y + 12));
                    Scene.Add(new GameCoin(X, Y, Y + 12));
                    Scene.Add(new GameCoin(X, Y, Y + 12));
                    RemoveSelf();
                    Scene.Add(new DeadSniper(X, Y));
                    Global.kills++;
                    Global.score += 10;
                    Global.NumberOfSnipers--;
                }
            }
            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;
                        //Console.WriteLine("i'm not colliding with friends, and not with walls");
                    }
                }
                else if (!AmCloseEnoughToAnythingToShootIt())//travel to target
                {
                    X -= Global.troopSpeed * (float)Math.Cos(angleInRadians);
                    Y -= Global.troopSpeed * (float)Math.Sin(angleInRadians);
                    ChangeAnimTo("walkLeft");
                    one = true;
                    //Console.WriteLine("Not close enough to shoot");
                }
                else if (isHittingTarget)
                {
                    if (one || hitTimer <= 0)
                    {
                        hitTimer = Rand.Int(40, 50);
                        one = false;
                        Scene.Add(new Bullet(X, Y, closestItem));
                        SoundManager.PlayRequestedSound("_shot", .2f);
                        //Console.WriteLine("Hitting Target");
                    }
                    ChangeAnimTo("shootRight");
                    hitTimer--;
                }
                if (scanAgain <= 0)
                {
                    isSeekingTarget = true;
                    closestItem = null;
                    closestDistanceItem = 1670;
                    scanAgain = 100;
                    //Console.WriteLine("scanning again");
                }
                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;
                        //Console.WriteLine("still colliding with friends");
                    }
                }
            }
        }

        public void ScanAllTargets()
        {
            var anyTeslas = GetEntities<RealSlotMachine>();
            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<Civilian>();
            foreach (var item in anyGenerator)
            {
                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.SNIPER);
            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();
        }
    }
}
