using System;
using KellysMarvelousAdventure.Utilities;
using Otter;

namespace KellysMarvelousAdventure.entities
{
    class Bullet : Entity
    {
        private Entity _destiny;
        private Spritemap<string> _spriteMap;
        private double angleInRadians = 1;
        private int life = 150;
        public bool IsDeflected = false;

        public Bullet(float x, float y, Entity destiny)
        {
            X = x; Y = y;
            _destiny = destiny;
            SetHitbox(20, 20, Global.Type.BULLET);
            _spriteMap = new Spritemap<string>(Assets.CATBULLET, 20, 20);
            _spriteMap.Add("main", new int[] { 0, 1, 2 }, new float[] { 1f, 1f });
            _spriteMap.Play("main");
            Graphic = _spriteMap;
            //Graphic = Image.CreateCircle(1, Color.Black);
            GetDirection();
        }

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

        public void HandleCollisions()//lets try this. I hope this works.
        {
            var colp = Collider.Collide((int)X, (int)Y, Global.Type.PLAYER);
            if (colp != null)
            {
                Global.PlayerHealth--;
                SoundManager.PlayRequestedSound("_girlOw");
                Destroy();
            }
            //try this first.
            var colwagon = Collider.Collide((int)X, (int)Y, Global.Type.WAGON);
            if (colwagon != null)
            {
                var rand = Rand.Int(1, 4);//remember one more than the last.
                if (rand == 1)
                    SoundManager.PlayRequestedSound("_wagonHit", .5f);
                else if (rand == 2)
                    SoundManager.PlayRequestedSound("_wagonHit2", .5f);
                else
                    SoundManager.PlayRequestedSound("_wagonHit3", .5f);
                var angleInFrisbees = MathHelper.ToDegrees((float)angleInRadians);
                angleInRadians = MathHelper.ToRadians(angleInFrisbees - 180);
                if (!Collider.HasTag((int)Global.Type.DEFLECTEDBULLET))//if it doesn't have the deflected bullet change, change it now.
                {
                    Collider.RemoveTag((int)Global.Type.BULLET);
                    Collider.AddTag((int)Global.Type.DEFLECTEDBULLET);
                }
            }
        }

        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();
        }
    }
}
