using Otter;
using PpbbffttMiningHa.Utility;
using System;

namespace PpbbffttMiningHa.entities.MapEntities
{
    class FlyingDirt : Entity
    {
        private Spritemap<string> _spriteMap;
        private double speed = Rand.Float(4.0f, 8.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 = 5;
        private bool donebouncing = false;
        private bool isBouncing = true;
        private float _bounceY;
        private float _yo = 0;
        private bool isLeft = false;
        private float accel = 0f;
        private bool runOnce;
        private int deathTimer = 0;
        private int deathTimerMax = 300;

        public FlyingDirt(float x, float y, float bounceY)
        {
            X = x; Y = y; _bounceY = bounceY;
            //_destiny = destiny;
            _spriteMap = new Spritemap<string>(Assets.TILES, 32, 32);
            _spriteMap.Add("walkLeft", new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new float[] { 7f, 7f });
            _spriteMap.Add("walkRight", new int[] { 0 }, new float[] { 7f, 7f });
            SetHitbox(8, 8, Global.Type.FLYINGDIRT);
            Collider.X = -4;//was -2
            Collider.Y = -4;//was -2
            _spriteMap.CenterOrigin();
            //Rectangle tileSize = new Rectangle(0, 0, 32, 32);
            //titleImage.ClippingRegion = tileSize;
            //AddGraphic(titleImage);
            _spriteMap.Alpha = .5f;
            _spriteMap.Scale = .2f;
            _spriteMap.Play("walkLeft");
            Graphic = _spriteMap;
            //GetDirection();
        }

        public override void Update()
        {
            base.Update();
            if (!runOnce)
            {
                runOnce = true;
                var colls = Collider.Collide(X, Y, (int)Global.Type.SOLID);
                if (colls != null)
                {
                    RemoveSelf();
                }
            }
            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(), .1f);
                    }
                    else if (Y > _bounceY + _yo && secondbounce)
                    {
                        decayingnumber = 2;
                        secondbounce = false;
                        thirdbounce = true;
                        SoundManager.PlayRequestedSound(RandomBounceSound(), .1f);
                    }
                    else if (Y > _bounceY + _yo && thirdbounce)
                    {
                        decayingnumber = 1;
                        thirdbounce = false;
                        fourthbounce = true;
                        SoundManager.PlayRequestedSound(RandomBounceSound(), .1f);
                    }
                    else if (Y > _bounceY + _yo && fourthbounce)
                    {
                        fourthbounce = false;
                        activateremove = true;
                        triggerremove = 0;
                        SoundManager.PlayRequestedSound(RandomBounceSound(), .1f);
                    }
                    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);
                var rockb = Collider.Collide((int)X, (int)Y, Global.Type.BOTTOMROCK);
                if (dirt != null || rockb != null)
                {
                    Y += 0;
                    deathTimer = deathTimerMax;
                }
                else
                    Y += accel;
                accel += .2f;
                if (deathTimer > 0)
                    deathTimer--;
                if (deathTimer <= 0)
                    Destroy();
            }
            HandleCollisions();
            //SeekPlayerMovement();
        }//end of update
        private string RandomBounceSound()
        {
            switch (Rand.Int(1, 9))//one more than the last
            {
                case 1:
                    return "_ballBounce";
                case 2:
                    return "_ballBounce2";
                case 3:
                    return "_ballBounce3";
                case 4:
                    return "_ballBounce4";
                case 5:
                    return "_ballBounce5";
                case 6:
                    return "_ballBounce6";
                case 7:
                    return "_ballBounce7";
                case 8:
                    return "_ballBounce8";

                default:
                    return "_ballBounce";
            }
        }

        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.Score++;
                Global.DirtInHand++;
                SoundManager.PlayRequestedSound("_blip"); // sound, pickup sound?
                Destroy();
            }
        }

        public void Destroy()
        {
            RemoveSelf();
        }

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

