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

[CreateAssetMenu(fileName = "InputPreset", menuName = "ScriptableObjects/InputPreset", order = 1)]

public class InputPreset : ScriptableObject
{
    public InputPresetData data;
}


[System.Serializable]
public class InputPresetData{
    public KeyCode leftInput;
    public KeyCode rightInput;
    public KeyCode jumpInput;
    public KeyCode interactingKey;
}



public static class InputManager{
    public static InputPresetData m_data;
    public static InputPresetData data(){
        if(m_data==null){Init();}
        return m_data;
    }

    public static void Init(){
        if(PlayerPrefs.HasKey("inputSettings")){
            Debug.Log("Trying to load input data : " + PlayerPrefs.GetString("inputSettings"));
            try{
                InputPresetData preset = JsonUtility.FromJson<InputPresetData>(PlayerPrefs.GetString("inputSettings"));
                m_data = preset;
            }catch(Exception error){
                Debug.Log("Error loading controls, Initiating default \n Error:" + error.Message);
                resetDefault();
            }
        }else{
            resetDefault();
        }
    }

    static void resetDefault(){
        m_data = new InputPresetData();
        m_data.leftInput = KeyCode.A;
        m_data.rightInput = KeyCode.D;
        m_data.jumpInput = KeyCode.Space;
        m_data.interactingKey=KeyCode.W;

        PlayerPrefs.SetString("inputSettings", JsonUtility.ToJson(m_data));
        PlayerPrefs.Save();
    }

    public static void Save(){
        PlayerPrefs.SetString("inputSettings", JsonUtility.ToJson(m_data));
        PlayerPrefs.Save();
    }
}