using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using WS;
#if PLATFORM_STEAMVR
using Valve.VR;
#endif

///<summary>SteamVR按键事件编辑</summary>
public class SteamVRActionEditor : EditorWindow
{
#if PLATFORM_STEAMVR
    [MenuItem("Tools/Framework/SteamVR按键事件绑定")]
    public static void ShowVoice()
    {
        SteamVRActionEditor window = EditorWindow.GetWindow<SteamVRActionEditor>("SteamVR按键事件绑定");
        window.Show();
    }

    ///<summary>滑杆</summary>
    private Vector2 pos;
    ///<summary>输入键配置</summary>
    private List<SteamVRKeyAction> Config;
    ///<summary>编辑器数据</summary>
    private List<EditorData> EditorDatas = new List<EditorData>();
    ///<summary>路径</summary>
    private string path;
    ///<summary>所有事件</summary>
    private string[] actions;
    ///<summary>临时数据</summary>
    private EditorData tmpdata;

    ///<summary>编辑器数据</summary>
    private class EditorData
    {
        ///<summary>所有事件</summary>
        public string[] Actions;
        ///<summary>事件索引</summary>
        public int ActionIndex;
        ///<summary>按键</summary>
        public VRKey Key;
    }

    private void OnEnable()
    {
        SteamVR_Input.InitializeFile(false, false);
        path = $"{Application.dataPath}/Resources/Config/Framework/SteamVRKeyActionConfig.txt";
        if (ConfigHelper.ConfigExists(path))
        {
            Config = ConfigHelper.FileLoadConfig<List<SteamVRKeyAction>>(path);
        }
        actions = SteamVR_Input.actionFile.action_sets[0].actionsList.Select(a => a.shortName).ToArray();
        for (int i = 0; i < Config.Count; i++)
        {
            EditorData data = new EditorData();
            data.Actions = actions;
            for (int j = 0; j < data.Actions.Length; j++)
            {
                if (data.Actions[j] == Config[i].Action)
                {
                    data.ActionIndex = j;
                    break;
                }
            }
            data.Key = Config[i].Key;
            EditorDatas.Add(data);
        }
        tmpdata = new EditorData { Actions = actions };
    }

    private void OnGUI()
    {
        pos = EditorGUILayout.BeginScrollView(pos);
        EditorGUILayout.BeginHorizontal();
        tmpdata.Key = (VRKey)EditorGUILayout.EnumPopup(tmpdata.Key, GUILayout.Width(200));
        tmpdata.ActionIndex = EditorGUILayout.Popup(tmpdata.ActionIndex, tmpdata.Actions);
        if (GUILayout.Button("添加", GUILayout.Width(100)))
        {
            if (EditorDatas.Exists(e => e.Key == tmpdata.Key))
            {
                ShowNotification(new GUIContent($"按键 {tmpdata.Key} 已存在"), 2);
                return;
            }
            EditorDatas.Add(tmpdata);
            tmpdata = new EditorData { Actions = actions };
        }
        EditorGUILayout.EndHorizontal();

        for (int i = 0; i < EditorDatas.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label(EditorDatas[i].Key.ToString(), GUILayout.Width(200));
            EditorDatas[i].ActionIndex = EditorGUILayout.Popup(EditorDatas[i].ActionIndex, EditorDatas[i].Actions);
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        if (GUILayout.Button("保存", GUILayout.Width(100)))
        {
            Config.Clear();
            for (int i = 0; i < EditorDatas.Count; i++)
            {
                SteamVRKeyAction action = new SteamVRKeyAction { Key = EditorDatas[i].Key };
                action.Action = EditorDatas[i].Actions[EditorDatas[i].ActionIndex];
                Config.Add(action);
            }
            ConfigHelper.SaveConfig(Config, path);
            AssetDatabase.Refresh();
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndScrollView();
    }
#endif
}