using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

namespace ScrambleKit.Scene
{
    [Serializable]
    public struct MenuChoice
    {
        public KeyCode KeyStroke;
        public string ChoiceName;
        public string SceneToLoad;
    }

    public class TransitionMenu : MonoBehaviour
    {
        public Transform TextPrefab;
        public List<MenuChoice> Choices;

        private Text ChoiceText;

        void Start()
        {
            if (TextPrefab == null)
                return;

            Transform t = Instantiate(TextPrefab);
            GameObject canvas = GameObject.Find("Canvas");
            t.SetParent(canvas.transform, false);
            ChoiceText = t.GetComponent<Text>();
            SetChoiceText();
        }

        void Update()
        {
            ProcessKeyStrokes();
        }

        private void SetChoiceText()
        {
            if (ChoiceText == null)
                return;

            string s = "";
            foreach(MenuChoice c in Choices)
            {
                s += c.KeyStroke.ToString() + " - " + c.ChoiceName + "\n";
            }
            ChoiceText.text = s;
            Debug.Log("Setting choicetext to " + s);
        }

        private void ProcessKeyStrokes()
        {
            foreach (MenuChoice c in Choices)
            {
                if(Input.GetKey(c.KeyStroke))
                {
                    SceneManager.LoadScene(c.SceneToLoad);
                    break;
                }
            }
        }
    }
}