using LOoSE_sLOTS.objects;
using LOoSE_sLOTS.UI;
using LOoSE_sLOTS.Utilities;
using Otter;
using System;

namespace LOoSE_sLOTS.entities
{
    class SlotMachine : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool isFirstClick;
        private bool isSecondClick;
        public Collider detectionBox = new BoxCollider(20, 400, (int)Global.Type.SLOTMACHINEDETECTOR);
        public static bool IsDead;

        public SlotMachine(float x, float y)
        {
            X = x; Y = y;
            X = x; Y = y;
            SetHitbox(20, 40, (int)Global.Type.SLOTMACHINE);
            detectionBox.Entity = this;
            Colliders.Add(Collider, detectionBox);
            Collider.X = -10;
            Collider.Y = -20;
            detectionBox.X = -10;
            detectionBox.Y = -20;
            _spriteMap = new Spritemap<string>(Assets.SLOTMACHINE, 50, 50);
            _spriteMap.Add("dead", new int[] { 5 }, new float[] { 7f, 7f });
            _spriteMap.Add("on", new int[] { 0, 1 }, new float[] { 7f, 7f });
            _spriteMap.Add("dying", new int[] { 2, 3, 4 }, new float[] { 3f, 3f });
            _spriteMap.Play("on");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
            Layer = Rand.Int(0, 2);//? will this work?
            //Layer = 0;//? will this work? it does because all the PUnicorns are always added AFTER my player. If they are 0 layer, they will be behind me.
            //angleInRadians = GetAngle(Rand.Float(0f, 359f));
            SoundManager.PlayRequestedSound(RandomStart());
        }

        public override void Update()
        {
            base.Update();
            if (!isFirstClick && !isSecondClick)
            {
                X = Global.GAME.Scene.MouseX;
                Y = 32;
            }
            else if (isFirstClick && !AmICollidingWithFloor(Collider))
                Y += 15;
            else if (isFirstClick && AmICollidingWithFloor(Collider))
            {
                isFirstClick = false;
                Shared.ChangeAnimTo("dying", _spriteMap);
                isSecondClick = true;
            }
            else if (isSecondClick)
            {
                X += 0;
                Y += 0;
            }
            //Console.WriteLine("_spritemap.CurrentAnim: {0}", _spriteMap.CurrentAnim);
            //Console.WriteLine("_spritemap.CurrentFrame: {0}", _spriteMap.CurrentFrame);
            //Console.WriteLine("_spritemap.CurrentFrameIndex: {0}", _spriteMap.CurrentFrameIndex);
            if (_spriteMap.CurrentAnim == "dying" && _spriteMap.CurrentFrameIndex == 2)//check this
            {
                Shared.ChangeAnimTo("dead", _spriteMap);
                Collider.X = -10;
                Collider.Y = 0;
                Collider.Width = 20;
                Collider.Height = 20;
                IsDead = true;
                RemoveCollider(detectionBox);
                Global.GAME.Scene.Add(new SlotMachine(0, 0));
                for(var i = 0; i < 10; i++)
                {
                    Global.GAME.Scene.Add(new Coin(X, Y, Y+12));
                }
                SoundManager.PlayRequestedSound("_smash", .5f);
                Global.NumberOfSlotMachinesDropped++;
                var ch = Collide(X, Y, (int)Global.Type.INTROCHARACTER);
                if (ch != null)//this collision worked. Lets use it to clear the screen.
                {
                    Global.GaveHimHisMoney = true;
                    ch.X = Global.I_MW / 2;//this never worked
                    ch.Y = Global.I_MH / 2;//this never worked
                    Global.GAME.Scene.Add(new TransistionDown(0, 0));//and this never worked.
                    SoundManager.PlayRequestedSound("_longerCreepyVibe");
                }
            }

            var cn = Global.PlayerSession.Controller;
            if (!isSecondClick && (cn.Button("LeftMouseClick").Pressed || cn.Button("RightMouseClick").Pressed))
            {
                if (!isFirstClick)
                    isFirstClick = true;
            }
        }//end update

        private string RandomStart()
        {
            switch (Rand.Int(1, 3))//one more than the last
            {
                case 1:
                    return "_bleep";
                case 2:
                    return "_bleepTwo";
                default:
                    return "_bleep";
            }
        }

        public bool AmICollidingWithFloor(Collider c)
        {
            var slot = c.Collide(X, Y, (int)Global.Type.SLOTMACHINE);
            var floor = c.Collide(X, Y, (int)Global.Type.SOLID);
            if (floor != null || slot != null)
                return true;
            else
                return false;
        }

        public void Destroy()
        {
            RemoveSelf();
        }

        public override void Render()
        {
            base.Render();
            //Collider.Render(Color.Blue);
            //detectionBox.Render(Color.Orange);
        }
    }
}


