using Otter;
using PpbbffttMiningHa.Utility;
using System;

namespace PpbbffttMiningHa.objects
{
    class DoorKey : 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 float accel = 0;
        private bool once;

        public DoorKey(float x, float y, float bounceY)
        {
            X = x; Y = y; _bounceY = bounceY;
            SetHitbox(20, 20, (int)Global.Type.DOORKEY);
            Collider.X = -10;
            Collider.Y = -10;
            _spriteMap = new Spritemap<string>(Assets.DOORKEY, 20, 20);
            _spriteMap.Add("spin", new int[] { 0 }, new float[] { 5f, 5f });
            _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)
            {
                /*
                 ok. we're done flying around. There's a game breaking edge case where IF you start this part of the process below the bottomrock
                 line there is no way of retrieving. it. This needs to happen before any of this code executes. Once it's satisfied, run this code.
                 */
                if (Y > 1927 && !once)
                    Y--;
                else //we are above that line. good!
                {
                    once = true;
                    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;
                    else
                        Y += accel;
                    accel += .2f;
                }

            }
            HandleCollisions();

        }//end of update
        private string RandomBounceSound()
        {
            switch (Rand.Int(1, 10))//one more than the last
            {
                case 1:
                    return "_keyBounce";
                case 2:
                    return "_keyBounce2";
                case 3:
                    return "_keyBounce3";
                case 4:
                    return "_keyBounce4";
                case 5:
                    return "_keyBounce5";
                case 6:
                    return "_keyBounce6";
                case 7:
                    return "_keyBounce7";
                case 8:
                    return "_keyBounce8";
                case 9:
                    return "_keyBounce9";

                default:
                    return "_keyBounce";
            }
        }
        private string RandomPickupSound()
        {
            switch (Rand.Int(1, 6))//one more than the last
            {
                case 1:
                    return "_keyPickup";
                case 2:
                    return "_keyPickup2";
                case 3:
                    return "_keyPickup3";
                case 4:
                    return "_keyPickup4";
                case 5:
                    return "_keyPickup5";
                case 6:
                    return "_keyPickup6";

                default:
                    return "_keyPickup";
            }
        }
        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 += 200;
                Global.HasKey = true;
                SoundManager.PlayRequestedSound(RandomPickupSound(), .5f); // sound, pickup keysound
                Destroy();
            }
        }
        public override void Render()
        {
            base.Render();
            //Collider.Render(Color.Magenta);
        }

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

