using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

// ver 1.1
// © 2018-9-25 gatosyocora

namespace VRCDeveloperTool
{

    public class HandPoseSetting : EditorWindow
    {

        private static string EditorPath = "Assets/VRCDeveloperTool/Editor/HandPoseAdder/";

        private static string handPoseName = "";
        private static AnimationClip handPoseAnimClip = null;

        [MenuItem("VRCDeveloperTool/HandPose Adder Setting")]
        private static void Create()
        {
            loadSettingData();
            GetWindow<HandPoseSetting>("HandPose Adder Setting");
        }

        private void OnGUI()
        {

            EditorGUILayout.PrefixLabel("Custom Hand Pose");

            handPoseName = EditorGUILayout.TextField("Hand Pose Name", handPoseName);

            handPoseAnimClip = EditorGUILayout.ObjectField(
                "Hand Pose AnimationClip",
                handPoseAnimClip,
                typeof(AnimationClip),
                true
            ) as AnimationClip;

            EditorGUI.BeginDisabledGroup(handPoseName == "" || handPoseAnimClip == null);
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Apply Setting"))
            {
                createSettingData(handPoseName, handPoseAnimClip);
            }
            EditorGUILayout.EndHorizontal();
            EditorGUI.EndDisabledGroup();

            EditorGUI.BeginDisabledGroup(!File.Exists(EditorPath + "SettingData.asset") || !File.Exists(EditorPath + "CustomHandPoseAdder.cs"));
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Load Setting"))
            {
                loadSettingData();
            }
            if (GUILayout.Button("Clear Setting"))
            {
                handPoseName = "";
                handPoseAnimClip = null;
                ClearSettingData();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUI.EndDisabledGroup();

        }

        // 設定データを保存
        private static void createSettingData(string name, AnimationClip animClip)
        {
            HandPoseSettingData.CreateSettingData(name, animClip);
            CreateCustomHandPoseAdderScript();
        }

        // 設定データを読み込み
        private static void loadSettingData()
        {
            HandPoseSettingData settingData = HandPoseSettingData.LoadSettingData();

            if (settingData == null) return;

            handPoseName = settingData.handPoseName;
            handPoseAnimClip = settingData.handPoseAnimClip;
        }

        // 追加した手の形のAnimationファイルをコピーするためのスクリプトを作成
        public static void CreateCustomHandPoseAdderScript()
        {
            string scriptAssetPath = EditorPath + "CustomHandPoseAdder.cs";
            StreamWriter sw = new StreamWriter(scriptAssetPath, false);

            sw.WriteLine("using UnityEngine;");
            sw.WriteLine("using UnityEditor;");
            sw.WriteLine("using VRCDeveloperTool;");
            sw.WriteLine("");
            sw.WriteLine("// Generated by HandPoseSetting.cs ver 1.1");
            sw.WriteLine("// © 2018-9-25 gatosyocora");
            sw.WriteLine("");
            sw.WriteLine("public class CustomHandPoseAdder : EditorWindow {");

            sw.WriteLine("  [MenuItem(\"CONTEXT/Motion/Add Hand pose '" + handPoseName + "'\", false, 8)]");
            sw.WriteLine("  private static void AddFPAnimationKeys(MenuCommand command) {");
            sw.WriteLine("      string animClipPath = \"" + AssetDatabase.GetAssetPath(handPoseAnimClip) + "\";");
            sw.WriteLine("      HandPoseAdder.AddHandPoseAnimationKeys(command, animClipPath);");
            sw.WriteLine("  }");

            sw.WriteLine("}");
            sw.Flush();
            sw.Close();

            AssetDatabase.ImportAsset(scriptAssetPath);
        }

        // 設定データを管理するファイルとCustomスクリプトを削除
        private static void ClearSettingData()
        {
            if (File.Exists(EditorPath + "SettingData.asset")) AssetDatabase.DeleteAsset(EditorPath + "SettingData.asset");
            if (File.Exists(EditorPath + "CustomHandPoseAdder.cs")) AssetDatabase.DeleteAsset(EditorPath + "CustomHandPoseAdder.cs");
        }

    }
}
