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

namespace LOoSE_sLOTS.objects
{
    class FiredCoin : Entity
    {
        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 FiredCoin(float x, float y, double destiny)
        {
            X = x; Y = y;
            _spriteMap = new Spritemap<string>(Assets.COIN, 10, 10);
            _spriteMap.Add("spin", new int[] { 0, 1, 2, 3, 2, 1 }, new float[] { 5f, 5f });
            SetHitbox(4, 4, Global.Type.FIREDCOIN);
            Collider.X = -2;
            Collider.Y = -2;
            _spriteMap.CenterOrigin();
            _spriteMap.Play("spin");
            Graphic = _spriteMap;
            angleInRadians = destiny;
        }

        public override void Update()
        {
            base.Update();
            X -= Global.PlayerFiredCoinSpeed * (float)Math.Cos(angleInRadians);
            Y -= Global.PlayerFiredCoinSpeed * (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 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();
            }

            var cols = Collider.Collide((int)X, (int)Y, Global.Type.SNIPER);
            if (cols != null)
            {
                Sniper ss = (Sniper)cols.Entity;
                ss.Health--;
                SoundManager.PlayRequestedSound("_sniperHurt");
                if (ss.Health <= 0)
                    ss.Destroy();
                //play destruction sound here;
                Destroy();
            }

            var colb = Collider.Collide((int)X, (int)Y, Global.Type.BOSS);
            if (colb != null)
            {
                SoundManager.PlayRequestedSound("_bossHurt");
                Global.BossHealth--;
                Destroy();
            }
        }

        public void Destroy()
        {
            Scene.Add(new GameCoin(X, Y, Y + 12));
            RemoveSelf();
        }

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