using LOoSE_sLOTS.Scenes;
using LOoSE_sLOTS.UI;
using LOoSE_sLOTS.Utilities;
using Otter;
using System;
using System.Collections;

namespace LOoSE_sLOTS.entities
{
    class RedPickupTruck : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool pickup;

        private static float speed = 1.5f;

        private static int _yo = 100;
        private static int _xo = 200;

        public static float IntroX = Global.I_MW / 2;
        public static float IntroY = Global.I_MH / 2;
        private static bool isGone;
        private bool clickForNextLevel;
        private bool one;
        private bool start;

        public RedPickupTruck(float x, float y)
        {
            X = x; Y = y;
            X = x; Y = y;
            SetHitbox(80, 50, (int)Global.Type.PICKUPTRUCK);
            Collider.X = -40;
            Collider.Y = -25;
            _spriteMap = new Spritemap<string>(Assets.BIGREDTRUCK, 100, 100);
            _spriteMap.Add("on", new int[] { 0, 1 }, new float[] { 7f, 7f });
            _spriteMap.Play("on");
            _spriteMap.CenterOrigin();
            Layer = 2;
            Graphic = _spriteMap;
            SoundManager.PlayRequestedSound("_truckSound", .5f);
        }

        public override void Update()
        {
            base.Update();
            X += speed;
            Global.PickupTruckX = X;
            Global.PickupTruckY = Y;
            #region putting this here because it wont work in the constructor?
            if (!start)
            {
                start = true;
                Global.WriteWord(20, Global.I_MH - 16, "To Skip Casino Intro Press X", Color.Blue, 1f, -1, Global.GAME.Scene, 0, 6);//shadow first
                Global.WriteWord(19, Global.I_MH - 15, "To Skip Casino Intro Press X", Color.Grey, 1f, -1, Global.GAME.Scene, 0, 6);//regular text on top(second)
            }
            #endregion
            var cn = Global.PlayerSession.Controller;
            if (cn.Button("X").Pressed && !Global.SkipCasinoIntro)
            {
                Console.WriteLine("x button pressed");
                Global.SkipCasinoIntro = true;
            }
            if (Timer > 100 && !pickup)
            {
                pickup = true;
                Global.GAME.Coroutine.Start(LetsChat(Global.GAME.Scene));
            }
            if (Global.SkipCasinoIntro && !one)
            {
                one = true;
                LoadNextLevel();
            }
            if (isGone)
            {
                isGone = false;
                var entities = GetEntities<Numbers>(); //remove intercom previous message.
                foreach (var item in entities)
                {
                    item.Destroy();
                }
                Global.WriteWord(IntroX - _xo, IntroY - _yo, "Click Enter to GO TO THE CASINO!", Color.Blue, 2f, 1, Global.GAME.Scene, 0);//shadow first
                Global.WriteWord(IntroX - _xo - 2, IntroY - _yo - 2, "Click Enter to GO TO THE CASINO!", Color.White, 2f, 1, Global.GAME.Scene, 0);//regular text on top(second)
                clickForNextLevel = true;
            }
            if ((Global.PlayerSession.Controller.Button("LeftMouseClick").Pressed || Global.PlayerSession.Controller.Button("Start").Pressed) && clickForNextLevel)
            {
                clickForNextLevel = false;
                LoadNextLevel();

            }
        }//end update

        private void LoadNextLevel()
        {
            SoundManager.PlayRequestedSound("_click");
            Global.GAME.RemoveScene();
            Global.GAME.AddScene(new Casino());
        }

        public static IEnumerator LetsChat(Scene scene)
        {
            if (!Global.SkipCasinoIntro)
            {
                Global.WriteWord(IntroX - _xo, IntroY - _yo, "Get Me Out Of This Thing!", Color.Gold, 2f, 1, scene, 150);//shadow first
                Global.WriteWord(IntroX - _xo - 2, IntroY - _yo - 2, "Get Me Out Of This Thing!", Color.White, 2f, 1, scene, 150);//regular text on top(second)
                SoundManager.PlayRequestedSound("_getMeOutOfThisThing");
                yield return Coroutine.Instance.WaitForFrames(150);
            }
            if (!Global.SkipCasinoIntro)
            {
                Global.WriteWord(IntroX - _xo, IntroY - _yo, "Oh no! ... What's That?", Color.Gold, 2f, 1, scene, 60);//shadow first
                Global.WriteWord(IntroX - _xo - 2, IntroY - _yo - 2, "Oh no! ... What's That?", Color.White, 2f, 1, scene, 60);//regular text on top(second)
                SoundManager.PlayRequestedSound("_ohNoWhatsThat");
                yield return Coroutine.Instance.WaitForFrames(60);
            }
            if (!Global.SkipCasinoIntro)
            {
                speed = 0;
                yield return Coroutine.Instance.WaitForFrames(60);
                Global.WriteWord(IntroX - _xo, IntroY - _yo, "There she is. Let's take her away.", Color.Red, 2f, 1, scene, 150);//shadow first
                Global.WriteWord(IntroX - _xo - 2, IntroY - _yo - 2, "There she is. Let's take her away.", Color.White, 2f, 1, scene, 150);//regular text on top(second)
                SoundManager.PlayRequestedSound("_thereSheIsLetsTakeHerAway");
                yield return Coroutine.Instance.WaitForFrames(150);
            }
            if (!Global.SkipCasinoIntro)
            {
                speed = 0;
                Global.AllowedToCollideWithTruck = true;
                SoundManager.PlayRequestedSound("_smash");
                yield return Coroutine.Instance.WaitForFrames(30);
                Global.WriteWord(IntroX - _xo, IntroY - _yo, "Oh", Color.Gold, 2f, 1, scene, 60);//shadow first
                Global.WriteWord(IntroX - _xo - 2, IntroY - _yo - 2, "Oh", Color.White, 2f, 1, scene, 60);//regular text on top(second)
                SoundManager.PlayRequestedSound("_oh");
                yield return Coroutine.Instance.WaitForFrames(60);
            }
            if (!Global.SkipCasinoIntro)
            {
                speed = 3;
                Global.WriteWord(IntroX - _xo, IntroY - _yo, "Nooooooooooooooooooooooooo!!!", Color.Gold, 2f, 1, scene, 150);//shadow first
                Global.WriteWord(IntroX - _xo - 2, IntroY - _yo - 2, "Nooooooooooooooooooooooooo!!!", Color.White, 2f, 1, scene, 150);//regular text on top(second)
                SoundManager.PlayRequestedSound("_noo");
                yield return Coroutine.Instance.WaitForFrames(150);
                isGone = true;
            }
        }

        public void Destroy()
        {
            RemoveSelf();
        }

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


