using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewKeybindScript : MonoBehaviour
{
    private bool keybindWasChanged = true;
    [SerializeField] private string actionName;
    private KeyCode newKey;
    private InputManagerScript i;
    // Start is called before the first frame update
    void Start()
    {
        i = GameObject.Find("InputManager").GetComponent<InputManagerScript>();
    }

    // Update is called once per frame
    void Update()
    {
        remapKey();
    }

    public void remapKey()
    {
        if(keybindWasChanged == false)
        {
            if (Input.anyKey) // if a key is pressed
            {
                foreach (KeyCode kcode in System.Enum.GetValues(typeof(KeyCode)))  // goes through all keys to find out which was pressed
                {
                    if (Input.GetKey(kcode))
                    {
                        i.remapKey(actionName, kcode); // function from InputManager (changes key)
                        keybindWasChanged = true;
                        this.gameObject.SetActive(false); // makes the UI for changing the Key disappear
                        break;
                    }
                }
            }
        }
    }

    public void setChangingKey(string actionName)
    {
        keybindWasChanged = false;
        this.actionName = actionName;
    }
}
