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

namespace BNG {

    public class VRKeyboardKey : MonoBehaviour {

        UnityEngine.UI.Button thisButton;
        TextMeshProUGUI thisButtonText;

        VRKeyboard vrKeyboard;

        public string Keycode;

        public string KeycodeShift;

        [HideInInspector]
        public bool UseShiftKey = false;

        void Awake() {
            thisButton = GetComponent<UnityEngine.UI.Button>();
            thisButtonText = GetComponentInChildren<TextMeshProUGUI>();

            // Assign click event handler
            if (thisButton != null) {                
                thisButton.onClick.AddListener(OnKeyHit);
            }

            vrKeyboard = GetComponentInParent<VRKeyboard>();
        }

        public virtual void ToggleShift() {
            UseShiftKey = !UseShiftKey;

            // Make sure the button exists
            if(thisButtonText == null) {
                return;
            }

            // Update text label
            if(UseShiftKey && !string.IsNullOrEmpty(KeycodeShift)) {
                thisButtonText.text = KeycodeShift;
            }
            else {
                thisButtonText.text = Keycode;
            }
        }

        public virtual void OnKeyHit() {
            OnKeyHit(UseShiftKey && !string.IsNullOrEmpty(KeycodeShift) ? KeycodeShift : Keycode);
        }

        public virtual void OnKeyHit(string key) {
            if(vrKeyboard != null) {
                vrKeyboard.PressKey(key);
            }
            else {
                Debug.Log("Pressed key " + key + ", but no keyboard was found");
            }
        }
    }
}

