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

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

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

        }

        public override void Update()
        {
            base.Update();
            if (!waterShot)
            {
                waterShot = true;
                var rand = Rand.Int(0, 2);
                if (rand == 0)
                    SoundManager.PlayRequestedSound("_waterTowerShot", 2f);
                else
                    SoundManager.PlayRequestedSound("_waterTowerShot2", 2f);
            }
            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.PLAYER);
            if (colt != null)
            {
                Global.PlayerHealth -= 15;
                Global.Score -= 2;
                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();
                }
                Destroy();
            }
        }


        public void GetDirection()
        {
            angleInRadians = Rand.Angle;
            /*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()
        {
            SoundManager.PlayRequestedSound("_waterDropDie");
            RemoveSelf();
        }

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