using LOoSE_sLOTS.entities;
using LOoSE_sLOTS.Utilities;
using Otter;
using System;

namespace LOoSE_sLOTS.objects
{
    class Bullet : Entity
    {
        private Entity _destiny;
        private double angleInRadians = 1;
        private Spritemap<string> _spriteMap;
        private int life = 150;//this number should be just slightly longer than their detection range for shooting.

        public Bullet(float x, float y, Entity destiny)
        {
            X = x; Y = y;
            _destiny = destiny;
            _spriteMap = new Spritemap<string>(Assets.BULLET, 10, 10);
            _spriteMap.Add("spin", new int[] { 0, 1, 2, 3, 2, 1 }, new float[] { 5f, 5f });
            SetHitbox(4, 4, Global.Type.BULLET);
            Collider.X = -2;
            Collider.Y = -2;
            _spriteMap.CenterOrigin();
            _spriteMap.Play("spin");
            Graphic = _spriteMap;
            GetDirection();
        }

        public override void Update()
        {
            base.Update();
            X -= Global.enemyBulletSpeed * (float)Math.Cos(angleInRadians);
            Y -= Global.enemyBulletSpeed * (float)Math.Sin(angleInRadians);
            if (life <= 0)
                Destroy();
            if (life > 0)
                life--;
            HandleCollisions();
        }

        public void HandleCollisions()//lets try this. I hope this works. It isn't. :O
        {
            var colt = Collider.Collide((int)X, (int)Y, Global.Type.SLOTMACHINE);
            if (colt != null)
            {
                RealSlotMachine t = (RealSlotMachine)colt.Entity;
                t.Health--;
                //play destruction sound here;
                Destroy();
            }

            var colp = Collider.Collide((int)X, (int)Y, Global.Type.PLAYER);
            if (colp != null)
            {
                Global.PlayerHealth -= 5;
                Scene.Add(new GameCoin(X, Y+50, Y + 32));
                SoundManager.PlayRequestedSound(RandomHey());
                Destroy();
            }

            var colg = Collider.Collide((int)X, (int)Y, Global.Type.CIVILIAN);
            if (colg != null)
            {
                Civilian tr = (Civilian)colg.Entity;
                tr.Health--;
                if (tr.Health <= 0)
                    tr.Destroy();
                //play destruction sound here;
                Destroy();
            }
        }

        private string RandomHey()
        {
            switch (Rand.Int(1, 5))//one more than the last
            {
                case 1:
                    return "_hey";
                case 2:
                    return "_hey2";
                case 3:
                    return "_hey3";
                case 4:
                    return "_hey4";
                default:
                    return "_hey";
            }
        }


        public void GetDirection()
        {
            var vector1 = new Point((int)_destiny.X, (int)_destiny.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 void Destroy()
        {
            RemoveSelf();
        }

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