using Otter;
using System;

namespace PenniesAreGold.objects
{
    class Bullet : Entity
    {
        private Entity _destiny;
        private double angleInRadians = 1;
        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;
            SetHitbox(2, 2, Global.Type.BULLET);
            Graphic = Image.CreateCircle(1, Color.Black);
            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.
        {
            var colt = Collider.Collide((int)X, (int)Y, Global.Type.MININGFACILITY);
            if (colt != null)
            {
                MiningFacility t = (MiningFacility)colt.Entity;
                t.FacilityHealth--;
                if (t.FacilityHealth <= 0)
                    t.Destroy();
                //play destruction sound here;
                Destroy();
            }

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

            var colg = Collider.Collide((int)X, (int)Y, Global.Type.TROOP);
            if (colg != null)
            {
                Troop tr = (Troop)colg.Entity;
                Console.WriteLine("troophurt!: {0}", tr.TroopHealth);
                tr.TroopHealth--;
                if (tr.TroopHealth <= 0)
                    tr.Destroy();
                Console.WriteLine("troophurt!: {0}", tr.TroopHealth);
                //play destruction sound here;
                Destroy();
            }

            var colf = Collider.Collide((int)X, (int)Y, Global.Type.TROOPBUILDING);
            if (colf != null)
            {
                TroopBuilding t = (TroopBuilding)colf.Entity;
                t.TroopBuildingHealth--;
                if (t.TroopBuildingHealth <= 0)
                    t.Destroy();
                //play destruction sound here;
                Destroy();
            }
        }


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