#if VRC_SDK_VRCSDK3 && UNITY_EDITOR
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEditorInternal;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase;
using VRCExpressionSetupTool.Editor.Layout;

namespace VRCExpressionSetupTool.Editor
{
    public class LayerClipsEditView
    {
        private static string title;
        private readonly List<KeyValuePair<string, List<ChildAnimatorState>>> states;
        private readonly ReorderableList clipList;

        public LayerClipsEditView(string title, List<KeyValuePair<string, List<ChildAnimatorState>>> states)
        {
            LayerClipsEditView.title = title;
            this.states = states;
            this.clipList = new ReorderableList(this.states,
                typeof(List<KeyValuePair<string, List<ChildAnimatorState>>>), 
                false, true, false, false)
            {
                drawElementCallback = this.DrawClipElement, 
                drawHeaderCallback = DrawClipHeader,
                elementHeight = 16f
            };
        }

        private static void DrawClipHeader(Rect rect)
        {
            rect.xMax /= 5f;
            GUI.Label(rect, title, EditorStyles.boldLabel);
            
            rect.xMin = rect.xMax;
            rect.xMax *= 2f;
            GUI.Label(rect, "表情(FX)", EditorStyles.label);
            
            rect.xMin = rect.xMax;
            rect.xMax *= 1.5f;
            GUI.Label(rect, "手(Gesture)", EditorStyles.label);
            
            rect.xMin = rect.xMax;
            rect.xMax *= 1.33f;
            GUI.Label(rect, "パク干渉(Mouth&Jow)", EditorStyles.label);
            
            rect.xMin = rect.xMax;
            rect.xMax *= 1.25f;
            GUI.Label(rect, "まばたき干渉(Eye&Eyelids)", EditorStyles.label);
        }

        private void DrawClipElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            rect.xMax /= 5f;
            GUI.Label(rect, this.states[index].Key, EditorStyles.label);

            rect.xMin = rect.xMax;
            rect.xMax *= 2f;
            EditorGUICustomLayout.ObjectField(rect, "", this.states[index].Value[0].state.motion, false,
                x => { this.states[index].Value[0].state.motion = x; });

            rect.xMin = rect.xMax;
            rect.xMax *= 1.5f;
            EditorGUICustomLayout.ObjectField(rect, "", this.states[index].Value[1].state.motion, false,
                x => { this.states[index].Value[1].state.motion = x; });

            rect.xMin = rect.xMax;
            rect.xMax *= 1.33f;
            var stateMachineBehaviour = this.states[index].Value[0].state.behaviours
                .FirstOrDefault(x => x is VRC_AnimatorTrackingControl);
            if (stateMachineBehaviour == null) stateMachineBehaviour = this.states[index].Value[0].state.AddStateMachineBehaviour<VRCAnimatorTrackingControl>();
            
            var trackingControl = (VRCAnimatorTrackingControl) stateMachineBehaviour;
            EditorGUICustomLayout.EnumPopup<VRC_AnimatorTrackingControl.TrackingType>(rect, "",
                trackingControl.trackingMouth,
                x => { trackingControl.trackingMouth = x; });

            rect.xMin = rect.xMax;
            rect.xMax *= 1.25f;
            EditorGUICustomLayout.EnumPopup<VRC_AnimatorTrackingControl.TrackingType>(rect, "",
                trackingControl.trackingEyes,
                x => { trackingControl.trackingEyes = x; });
        }

        public void OnGUI()
        {
            this.clipList.list = this.states;
            this.clipList.DoLayoutList();
        }
    }
}
#endif