using Otter;
using System;

namespace PpbbffttMiningHa.entities.Bosses
{
    class DeadPrairieDog : Entity
    {
        private Spritemap<string> _spriteMap;
        private double speed = Rand.Float(2.0f, 6.0f);
        private float decayingnumber = 6;//initial force
        private double radianAngle = Rand.Float(.5f, 2.6f);
        private bool firstbounce = true;
        private bool secondbounce = false;
        private bool thirdbounce = false;
        private bool fourthbounce = false;
        private bool activateremove = false;
        private float triggerremove = 50;
        public static bool donebouncing = false;
        private bool isBouncing = true;
        private float _bounceY;
        private float _yo = 0;
        private bool isLeft = false;
        private int life = 300;

        public DeadPrairieDog(float x, float y, float bounceY)
        {
            X = x; Y = y; _bounceY = bounceY;
            SetHitbox(100, 100, (int)Global.Type.DEADPRAIRIEDOG);
            Collider.X = 0;
            Collider.Y = 0;
            _spriteMap = new Spritemap<string>(Assets.PRAIRIEDOG, 100, 100);
            _spriteMap.Add("spin", new int[] { 2 }, new float[] { 2, 2 });
            _spriteMap.Play("spin");
            _spriteMap.CenterOrigin();
            Layer = 1;
            Graphic = _spriteMap;
        }

        public override void Update()
        {
            base.Update();

            if (isBouncing)
            {

                if (X < 4)
                {
                    X = Global.MW;
                }
                else if (X > Global.MW)
                {
                    X = 4;
                }
                else
                {
                    donebouncing = false;
                    var colls = Collider.Collide(X, Y, (int)Global.Type.SOLID);
                    if (colls != null)
                    {
                        if (isLeft)
                            isLeft = false;
                        else
                            isLeft = true;
                    }
                    if (isLeft)
                        X += (float)speed * (float)Math.Cos(radianAngle);
                    else
                        X -= (float)speed * (float)Math.Cos(radianAngle);
                    var collup = Collider.Collide(X, Y + (float)speed, (int)Global.Type.SOLID);
                    if (collup != null)
                        Y += (float)speed;
                    else
                        Y -= decayingnumber * (float)Math.Sin(radianAngle);
                    _spriteMap.Angle += 16;//was 6
                    decayingnumber -= .1f;
                    if (Y > _bounceY + _yo && firstbounce)
                    {
                        firstbounce = false;
                        secondbounce = true;
                        decayingnumber = 4;//was 2
                        //SoundManager.PlayRequestedSound(RandomBounceSound(), .5f);
                    }
                    else if (Y > _bounceY + _yo && secondbounce)
                    {
                        decayingnumber = 2;
                        secondbounce = false;
                        thirdbounce = true;
                        //SoundManager.PlayRequestedSound(RandomBounceSound(), .5f);
                    }
                    else if (Y > _bounceY + _yo && thirdbounce)
                    {
                        decayingnumber = 1;
                        thirdbounce = false;
                        fourthbounce = true;
                        //SoundManager.PlayRequestedSound(RandomBounceSound(), .5f);
                    }
                    else if (Y > _bounceY + _yo && fourthbounce)
                    {
                        fourthbounce = false;
                        activateremove = true;
                        triggerremove = 0;
                        // SoundManager.PlayRequestedSound(RandomBounceSound(), .5f);
                    }
                    if (activateremove)
                    {
                        isBouncing = false;
                        donebouncing = true;
                        _spriteMap.Angle = 0;
                        decayingnumber = 30;
                        triggerremove -= .5f;
                        var colld = Collider.Collide(X, Y, (int)Global.Type.SOLID);
                        if (colld != null || X < 0 || X >= Global.MW || Y < 0 || Y >= Global.MH)
                        {
                            X += 0;
                        }
                    }
                }
            }
            /*if (donebouncing)
            {
                var dirt = Collider.Collide((int)X, (int)Y, Global.Type.SOLID);
                if (dirt != null)
                    Y += 0;
                else
                    Y += accel;
                accel += .2f;
            }*/
            else if (donebouncing)
            {
                if (life > 0)
                    life--;
                if (life <= 0)
                    Destroy();
            }
        }

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

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


