using GiraffeRoller.entities.enemies;
using GiraffeRoller.Utility;
using Otter;
using System;

namespace GiraffeRoller.entities.bullets
{
    class ConcreteBlock : Entity
    {
        private Entity _destiny;
        private double angleInRadians = Rand.Angle;
        private Spritemap<string> _spriteMap;
        private int life = 350;//this number should be just slightly longer than their detection range for shooting.
        private bool shot;

        public ConcreteBlock(float x, float y, Entity destiny)
        {
            X = x; Y = y;
            _destiny = destiny;
            _spriteMap = new Spritemap<string>(Assets.CONCRETEBLOCK, 20, 20);
            _spriteMap.Add("spin", new int[] { 0, 1, 2, 3 }, new float[] { 7f, 7f });
            SetHitbox(4, 4, Global.Type.CONCRETEBLOCK);
            Collider.X = -2;
            Collider.Y = -2;
            _spriteMap.CenterOrigin();
            _spriteMap.Play("spin");
            Graphic = _spriteMap;
            GetDirection();
            //SoundManager.PlayRequestedSound("_gunshot", .2f);
        }

        public override void Update()
        {
            base.Update();
            if (!shot)
            {
                shot = true;
                SoundManager.PlayRequestedSound(RandomShot(), .7f);
            }
            X -= Global.enemyBulletSpeed * (float)Math.Cos(angleInRadians);
            Y -= Global.enemyBulletSpeed * (float)Math.Sin(angleInRadians);
            if (life <= 0)
                Destroy();
            if (life > 0)
                life--;
            HandleCollisions();
        }// end of update
        private string RandomShot()
        {
            switch (Rand.Int(1, 4))//one more than the last case number (so counting the default would be an additional one)
            {
                case 1:
                    return "_concreteShot";
                case 2:
                    return "_concreteShot2";
                case 3:
                    return "_concreteShot3";
                default:
                    return "_concreteShot4";
            }
        }

        public void HandleCollisions()//lets try this. I hope this works. It isn't. :O
        {
            var colt = Collider.Collide((int)X, (int)Y, Global.Type.PLAYER);
            if (colt != null)
            {
                Global.PlayerHealth -= 15;
                Global.Score -= 13;
                SoundManager.PlayRequestedSound(Shared.RandomBossLaugh(), .8f);
                Destroy();
            }
            var cart = Collider.Collide((int)X, (int)Y, Global.Type.SOLID);
            if (cart != null)
            {
                Destroy();
            }

            var colg = Collider.Collide((int)X, (int)Y, Global.Type.WILDGIRAFFE);
            var colrg = Collider.Collide((int)X, (int)Y, Global.Type.RUNNINGWILDGIRAFFE);
            if (colg != null || colrg != null)
            {
                WildGiraffe tr = (WildGiraffe)colg.Entity;
                tr.Health--;
                if (tr.Health <= 0)
                {
                    SoundManager.PlayRequestedSound("_giraffeDying", .5f);
                    tr.Destroy();
                }
                //play destruction sound here;
                //Console.WriteLine("hitting civiilaran");
                Destroy();
            }
        }


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