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

namespace GiraffeRoller.entities.bullets
{
    class Rocket : Entity
    {
        private Entity _destiny;
        private Spritemap<string> _spriteMap;
        //ai follow me code
        private bool isSeekingTarget = true;
        private float closestDistanceItem = 3650f;
        private Entity closestItem;
        private double angleInRadians = 1;//this is about 57 degrees. ;-)
        private const int ScanTimerMax = 50;//50 is fine for slower speeds
        private int scanAgain = ScanTimerMax;
        private int hitTimerMax = Rand.Int(100, 150);
        private bool _isCloseEnoughToPlayer;
        private bool rocketShot;

        public Rocket(float x, float y, Entity destiny)
        {
            X = x; Y = y;
            _destiny = destiny;
            _spriteMap = new Spritemap<string>(Assets.ROCKET, 30, 30);
            _spriteMap.Add("walkLeft", new int[] { 0, 1, 2, 3 }, new float[] { 7f, 7f });
            _spriteMap.Add("walkRight", new int[] { 0, 1, 2, 3 }, new float[] { 7f, 7f });
            SetHitbox(30, 30, Global.Type.ROCKET);
            Collider.X = -2;
            Collider.Y = -2;
            _spriteMap.CenterOrigin();
            _spriteMap.Play("walkRight");
            Graphic = _spriteMap;
            GetDirection();
        }

        public override void Update()
        {
            base.Update();
            if (!rocketShot)
            {
                rocketShot = true;
                SoundManager.PlayRequestedSound(RandomRocket());
            }
            //X -= Global.rocketSpeed * (float)Math.Cos(angleInRadians);
            //Y -= Global.rocketSpeed * (float)Math.Sin(angleInRadians);
            HandleCollisions();
            SeekPlayerMovement();
        }//end of update
        private string RandomRocket()
        {
            switch (Rand.Int(1, 17))//one more than the last case number (so counting the default would be an additional one)
            {
                case 1:
                    return "_rocketShot";
                case 2:
                    return "_rocketShot2";
                case 3:
                    return "_rocketShot3";
                case 4:
                    return "_rocketShot4";
                case 5:
                    return "_rocketShot5";
                case 6:
                    return "_rocketShot6";
                case 7:
                    return "_rocketShot7";
                case 8:
                    return "_rocketShot8";
                case 9:
                    return "_rocketShot9";
                case 10:
                    return "_rocketShot10";
                case 11:
                    return "_rocketShot11";
                case 12:
                    return "_rocketShot12";
                case 13:
                    return "_rocketShot13";
                case 14:
                    return "_rocketShot14";
                case 15:
                    return "_rocketShot15";
                case 16:
                    return "_rocketShot16";
                default:
                    return "_rocketShot";
            }
        }
        public void SeekPlayerMovement()
        {
            if (isSeekingTarget)
            {
                ScanAllTargets();
            }
            else if (!AmICloseEnoughToPlayer())//travel to target
            {
                X -= Global.rocketSpeed * (float)Math.Cos(angleInRadians);
                Y -= Global.rocketSpeed * (float)Math.Sin(angleInRadians);

                if (X >= Global.PlayerX)//this needs to be a range of values for where the player is actually left of. so, there should be a dead zone where the animal doesn't move!
                {
                    Shared.ChangeAnimTo("walkLeft", _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else
                {
                    Shared.ChangeAnimTo("walkRight", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            else if (_isCloseEnoughToPlayer)
            {
                if (X >= Global.PlayerX)//this needs to be a range of values for where the player is actually left of. so, there should be a dead zone where the animal doesn't move!
                {
                    Shared.ChangeAnimTo("walkLeft", _spriteMap);
                    _spriteMap.FlippedX = true;
                }
                else
                {
                    Shared.ChangeAnimTo("walkRight", _spriteMap);
                    _spriteMap.FlippedX = false;
                }
            }
            if (scanAgain <= 0)
            {
                isSeekingTarget = true;
                closestItem = null;
                closestDistanceItem = 1670;
                scanAgain = ScanTimerMax;
            }
            if (scanAgain > 0)
                scanAgain--;
        }

        private void ScanAllTargets()
        {
            var anyPlayer = GetEntities<Player>();
            foreach (var item in anyPlayer)
            {
                var dist = Util.Distance(this, item);
                if (dist <= closestDistanceItem)
                {
                    closestDistanceItem = dist;
                    closestItem = item;
                }
            }
            if (closestItem != null)
            {
                isSeekingTarget = false;
                GetDirection();
            }
        }

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

        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 -= 5;
                Global.Score -= 3;
                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)_destiny.X + 20, (int)_destiny.Y + 40);
            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 void Destroy()
        {
            RemoveSelf();
        }

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