using Otter;
using PpbbffttMiningHa.Utility;
using System;

namespace PpbbffttMiningHa.entities
{
    class Bomb : Entity
    {
        private Spritemap<string> _spriteMap;
        public int GeneratorHealth = 100;
        private float closestDistanceItem = 1650f;
        private Entity closestItem;
        private double angleInRadians = 1;//this is about 57 degrees. ;-)
        private readonly float startingX = 0;
        private readonly float startingY = 0;
        public int deathTimer = 200;
        private readonly double angle = Rand.Angle;
        private bool one = true;
        public bool CanDie = false;
        private float accel = 0f;
        private bool _useInMenu;

        public Bomb(float x, float y, int layer = 0, bool useInMenu = false)
        {
            X = x; Y = y; _useInMenu = useInMenu;
            startingX = x; startingY = y;
            SetHitbox(20, 20, (int)Global.Type.BOMB);
            Collider.X = -10;
            Collider.Y = -10;
            _spriteMap = new Spritemap<string>(Assets.BOMB, 32, 32);
            _spriteMap.Add("main", new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }, new float[] { 5f, 5f });
            _spriteMap.Play("main");
            _spriteMap.CenterOrigin();
            Layer = layer;
            Graphic = _spriteMap;
            SoundManager.PlayRequestedSound(RandomAddSound());
        }

        public override void Update()
        {
            base.Update();
            if (_spriteMap.CurrentAnim == "main" && _spriteMap.CurrentFrameIndex == 21)
            {
                Scene.Add(new Explosion(X, Y, Rand.Float(1f, 50f), _useInMenu)); //add possible explosion that rips up the tiles around it!
                SoundManager.PlayRequestedSound(RandomExplosionSound());
                Global.Done = 0;
                Global.CameraTimer = 3;
                Global.IsScreenShake = true;
                RemoveSelf();
            }
            var dirt = Collider.Collide((int)X, (int)Y, Global.Type.SOLID);
            var rockb = Collider.Collide((int)X, (int)Y, Global.Type.BOTTOMROCK);
            if (dirt != null || rockb != null)
            {
                //SoundManager.PlayRequestedSound(Shared.RandomBossLaugh(), .8f);
                Y += 0;
            }
            else
            {
                Y += accel;
            }
            accel += .2f;
        }//end of update

        private string RandomAddSound()
        {
            switch (Rand.Int(1, 7))//one more than the last
            {
                case 1:
                    return "_bombSetDown";
                case 2:
                    return "_bombSetDown2";
                case 3:
                    return "_bombSetDown3";
                case 4:
                    return "_bombSetDown4";
                case 5:
                    return "_bombSetDown5";
                case 6:
                    return "_bombSetDown6";

                default:
                    return "_bombSetDown";
            }
        }

        private string RandomExplosionSound()
        {
            switch (Rand.Int(1, 11))//one more than the last
            {
                case 1:
                    return "_bombShort";
                case 2:
                    return "_bombShort2";
                case 3:
                    return "_bombShort3";
                case 4:
                    return "_ballBounce4";
                case 5:
                    return "_bombMedium";
                case 6:
                    return "_bombMedium2";
                case 7:
                    return "_bombMedium3";
                case 8:
                    return "_bombLong";
                case 9:
                    return "_bombLong2";
                case 10:
                    return "_bombLong3";

                default:
                    return "_bombShort";
            }
        }

        public void GetDirection()
        {
            var vector1 = new Point((int)closestItem.X + 50, (int)closestItem.Y + 50);
            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 double GetAngle(float angle)
        {
            return MathHelper.ToRadians(angle);
        }

        public override void Render()//mainly for testing colliders :D
        {
            base.Render();
            //Collider.Render();
        }

        public void Destroy()
        {
            RemoveSelf();
        }
    }
}


