using LOoSE_sLOTS.Utilities;
using Otter;
using System;

namespace LOoSE_sLOTS.objects
{
    class GameCoin : 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 GameCoin(float x, float y, float bounceY)
        {
            X = x; Y = y; _bounceY = bounceY;
            SetHitbox(8, 8, (int)Global.Type.COIN);
            Collider.X = -4;
            Collider.Y = -4;
            _spriteMap = new Spritemap<string>(Assets.COIN, 10, 10);
            _spriteMap.Add("spin", new int[] { 0, 1, 2, 3 }, new float[] { 2, 2 });
            _spriteMap.Play("spin");
            _spriteMap.CenterOrigin();
            Layer = 1;
            Graphic = _spriteMap;
        }

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

                if (X < 0)
                {
                    X = Global.MW - 5;
                }
                else if (X >= Global.MW)
                {
                    X = 1;
                }
                else
                {
                    donebouncing = false;
                    var colls = Collider.Collide(X, Y, (int)Global.Type.SOLID);
                    if (colls != null)
                        X += 0;
                    if (isLeft)
                        X += (float)speed * (float)Math.Cos(radianAngle);
                    else
                        X -= (float)speed * (float)Math.Cos(radianAngle);
                    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(), .4f);
                    }
                    else if (Y > _bounceY + _yo && secondbounce)
                    {
                        decayingnumber = 2;
                        secondbounce = false;
                        thirdbounce = true;
                        SoundManager.PlayRequestedSound(RandomBounceSound(), .4f);
                    }
                    else if (Y > _bounceY + _yo && thirdbounce)
                    {
                        decayingnumber = 1;
                        thirdbounce = false;
                        fourthbounce = true;
                        SoundManager.PlayRequestedSound(RandomBounceSound(), .4f);
                    }
                    else if (Y > _bounceY + _yo && fourthbounce)
                    {
                        fourthbounce = false;
                        activateremove = true;
                        triggerremove = 30;
                        SoundManager.PlayRequestedSound(RandomBounceSound(), .4f);
                    }
                    if (activateremove)
                    {
                        isBouncing = false;
                        donebouncing = true;
                        _spriteMap.Angle = 0;
                        decayingnumber = 0;
                        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;
                        }
                    }
                }
            }
            else if (donebouncing)
            {
                if (life > 0)
                    life--;
                if (life <= 0)
                    Destroy();
            }
        }
        private string RandomBounceSound()
        {
            switch (Rand.Int(1, 21))//one more than the last
            {
                case 1:
                    return "_coinBounce";
                case 2:
                    return "_coinBounce2";
                case 3:
                    return "_coinBounce3";
                case 4:
                    return "_coinBounce4";
                case 5:
                    return "_coinBounce5";
                case 6:
                    return "_coinBounce6";
                case 7:
                    return "_coinBounce7";
                case 8:
                    return "_coinBounce8";
                case 9:
                    return "_coinBounce9";
                case 10:
                    return "_coinBounce10";
                case 11:
                    return "_coinBounce11";
                case 12:
                    return "_coinBounce12";
                case 13:
                    return "_coinBounce13";
                case 14:
                    return "_coinBounce14";
                case 15:
                    return "_coinBounce15";
                case 16:
                    return "_coinBounce16";
                case 17:
                    return "_coinBounce17";
                case 18:
                    return "_coinBounce18";
                case 19:
                    return "_coinBounce19";
                case 20:
                    return "_coinBounce20";
                default:
                    return "_coinBounce";
            }
        }
        public override void Render()
        {
            base.Render();
            //Collider.Render(Color.Magenta);
        }

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

