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

namespace LOoSE_sLOTS.entities
{
    class Boss : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool isSeekingTarget = true;
        private float closestDistanceItem = 1650f;
        private Entity closestItem;
        private double angleInRadians = 1;//this is about 57 degrees. ;-)
        private bool isHittingTarget = false;
        private int scanAgain = 100;
        private float scaryGirlX = 0;
        private float scaryGirlY = 0;
        private int firstTimer = 10;
        private int hitTimer = 50;
        private Collider scaryGirl;
        private bool one = true;
        private bool isDead;
        private bool isAllowedToDie = false;
        private int endGameTimer = 400;

        public Boss(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(50, 50, (int)Global.Type.BOSS);
            Collider.X = -20;
            Collider.Y = -20;
            _spriteMap = new Spritemap<string>(Assets.BOSS, 100, 100);
            _spriteMap.Add("walk", new int[] { 0, 1, 2, 3 }, new float[] { 5f, 5f });
            _spriteMap.Add("attack", new int[] { 4, 5 }, new float[] { 15f, 15f });
            _spriteMap.Add("dead", new int[] { 6 }, new float[] { 15f, 15f });
            _spriteMap.Play("walk");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
        }

        public override void Update()
        {
            base.Update();
            scaryGirlX = X; scaryGirlY = Y;
            if (isAllowedToDie)
            {
                if (isDead && endGameTimer > 0)
                    endGameTimer--;
                if (isDead && endGameTimer <= 0)
                    Destroy();
            }
            else
            {
                //scan closest enemy entity (anything!)
                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("walk");
                        one = true;
                    }
                }
                else if (!AmCollidingWithAnything())//travel to target
                {
                    X -= Global.bossSpeed * (float)Math.Cos(angleInRadians);
                    Y -= Global.bossSpeed * (float)Math.Sin(angleInRadians);
                    ChangeAnimTo("walk");
                    one = true;
                }
                else if (isHittingTarget)
                {
                    if (one || hitTimer <= 0)
                    {
                        hitTimer = 50;
                        one = false;
                        //SoundManager.PlayRequestedSound("_bossHurt");Add later.
                    }
                    ChangeAnimTo("attack");
                    HandleCollisions();
                    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 (scaryGirl != null)//you are still colliding with another girl. get a new angle.
                    {
                        angleInRadians = GetAngle(Rand.Float(0f, 359f));
                        firstTimer = 100;
                    }
                }
                if (Global.BossHealth <= 0 && !isDead)
                {
                    isDead = true;
                    Global.score += 1000;
                    SoundManager.PlayRequestedSound("_iDontWantToDie");
                    Shared.ChangeAnimTo("dead", _spriteMap);
                    for (var j = 0; j < 50; j++)
                    {
                        Scene.Add(new DeadBoss(X, Y, Y + 12));
                    }
                    for (var i = 0; i < 100; i++)
                    {
                        Scene.Add(new GameCoin(X, Y, Y + 12));
                    }
                    endGameTimer = 400;
                    isAllowedToDie = true;
                }
            }
        }//end of update

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

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

        public bool AmCollidingWithAnything()
        {
            var colp = Collider.Collide((int)X, (int)Y, Global.Type.PLAYER);
            var cols = Collider.Collide((int)X, (int)Y, Global.Type.SLOTMACHINE);
            var colc = Collider.Collide((int)X, (int)Y, Global.Type.CIVILIAN);
            if (colp != null || cols != null || colc != null)
            {
                isHittingTarget = true;
                return true;
            }
            else
            {
                isHittingTarget = false;
                return false;
            }
        }

        public void HandleCollisions()//lets try this. I hope this works.
        {
            var colt = Collider.Collide((int)X, (int)Y, Global.Type.SLOTMACHINE);
            if (colt != null)
            {
                RealSlotMachine t = (RealSlotMachine)colt.Entity;
                SoundManager.PlayRequestedSound("_smash", .5f);
                t.Destroy();
                //play destruction sound here;
            }

            var colp = Collider.Collide((int)X, (int)Y, Global.Type.PLAYER);
            if (colp != null)
            {
                Global.PlayerHealth--;
                SoundManager.PlayRequestedSound("_oh");
            }

            var colc = Collider.Collide((int)X, (int)Y, Global.Type.CIVILIAN);
            if (colc != null)
            {
                Civilian c = (Civilian)colc.Entity;
                c.Destroy();
            }
        }

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

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

        public void Destroy()
        {
            isAllowedToDie = false;
            RemoveSelf();
            Global.GAME.RemoveScene();
            Global.GAME.AddScene(new WinScreen());
        }
    }
}


