using Otter;

namespace GiraffeRoller.ui.PauseMenu
{
    class PressKeyMessage : Entity
    {
        private bool _one = true;
        private int _player = 0;
        private string _keyBeingChanged = "";
        public static bool IsDestroy;

        public PressKeyMessage(int player, string keyBeingChanged)
        {
            _player = player;
            _keyBeingChanged = keyBeingChanged;
            if (player == 1)
                AddGraphic(Image.CreateRectangle(402, 242, Color.Green));
            else if (player == 2)
                AddGraphic(Image.CreateRectangle(402, 242, Color.Cyan));
            else if (player == 3)
                AddGraphic(Image.CreateRectangle(402, 242, Color.Magenta));
            else if (player == 4)
                AddGraphic(Image.CreateRectangle(402, 242, Color.Orange));
            AddGraphic(Image.CreateRectangle(400, 240, Color.Black));

            Layer = -10;
            Group = 100;//this is my pause menu group. (i know it's weird but you have to get used to it!)
        }

        public override void Update()
        {
            base.Update();
            X = Scene.CameraX + 150;
            Y = Scene.CameraY + 100;
            if (!PauseMenuBubble.IsChangingEscape && (Global.PausePersonSession.Controller.Button("PPEscape").Pressed || IsDestroy))
                Destroy();

            if (_one)
            {
                _one = false;
                WriteWord(250, 200, "Press any Key or Button: ", Color.White, 1f, -10);
                if (_player == 1)
                    WriteWord(300, 150, _keyBeingChanged, Color.Green, 1f, -10);
                else if (_player == 2)
                    WriteWord(300, 150, _keyBeingChanged, Color.Cyan, 1f, -10);
                else if (_player == 3)
                    WriteWord(300, 150, _keyBeingChanged, Color.Magenta, 1f, -10);
                else if (_player == 4)
                    WriteWord(300, 150, _keyBeingChanged, Color.Orange, 1f, -10);
            }
        }

        public void WriteWord(float x, float y, string word, Color color, float scale, int layer)
        {
            var a = 0;
            for (var i = 0; i < word.Length; i++)
            {
                Scene.Add(new PressKeyNumbers(x + a, y, word[i].ToString(), color, scale, layer));
                a += 8;
            }
        }

        public void Destroy()
        {
            var menunumbers = Scene.GetEntities<PressKeyNumbers>();
            foreach (var number in menunumbers)
            {
                number.RemoveSelf();
            }
            IsDestroy = false;
            RemoveSelf();
        }
    }
}
