using UnityEditor;
using UnityEngine;

namespace HighlightPlus {
    [CustomEditor(typeof(HighlightTrigger))]
    public class HighlightTriggerEditor : UnityEditor.Editor {

        SerializedProperty highlightOnHover, triggerMode, raycastCamera, raycastSource, raycastLayerMask, blockingLayerMask;
        SerializedProperty minDistance, maxDistance, respectUI, unhighlightOnUI, volumeLayerMask;
        SerializedProperty selectOnClick, selectedProfile, selectedAndHighlightedProfile, singleSelection, toggleOnClick, keepSelection;
        HighlightTrigger trigger;

        void OnEnable() {
            highlightOnHover = serializedObject.FindProperty("highlightOnHover");
            triggerMode = serializedObject.FindProperty("triggerMode");
            raycastCamera = serializedObject.FindProperty("raycastCamera");
            raycastSource = serializedObject.FindProperty("raycastSource");
            raycastLayerMask = serializedObject.FindProperty("raycastLayerMask");
            blockingLayerMask = serializedObject.FindProperty("blockingLayerMask");
            minDistance = serializedObject.FindProperty("minDistance");
            maxDistance = serializedObject.FindProperty("maxDistance");
            respectUI = serializedObject.FindProperty("respectUI");
            unhighlightOnUI = serializedObject.FindProperty("unhighlightOnUI");
            volumeLayerMask = serializedObject.FindProperty("volumeLayerMask");
            selectOnClick = serializedObject.FindProperty("selectOnClick");
            selectedProfile = serializedObject.FindProperty("selectedProfile");
            selectedAndHighlightedProfile = serializedObject.FindProperty("selectedAndHighlightedProfile");
            singleSelection = serializedObject.FindProperty("singleSelection");
            toggleOnClick = serializedObject.FindProperty("toggle");
            keepSelection = serializedObject.FindProperty("keepSelection");
            trigger = (HighlightTrigger)target;
            trigger.Init();
        }

        public override void OnInspectorGUI() {

			serializedObject.Update ();

			if (trigger.triggerMode == TriggerMode.RaycastOnThisObjectAndChildren) {
                if (!trigger.hasColliders && !trigger.hasColliders2D) {
					EditorGUILayout.HelpBox ("No collider found on this object or any of its children. Add colliders to allow automatic highlighting.", MessageType.Warning);
				}
            } else {
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
                if (trigger.triggerMode == TriggerMode.ColliderEventsOnlyOnThisObject) {
                    EditorGUILayout.HelpBox("This trigger mode is not compatible with the new input system.", MessageType.Error);
                }
#endif
#if HP2_PHYSICS2D
                if (trigger.GetComponent<Collider>() == null && trigger.GetComponent<Collider2D>() == null) {
#else
                if (trigger.GetComponent<Collider>() == null) {
#endif
					EditorGUILayout.HelpBox ("No collider found on this object. Add a collider to allow automatic highlighting.", MessageType.Error);
                }
            }

            EditorGUILayout.PropertyField(triggerMode);
            switch (trigger.triggerMode) {
                case TriggerMode.RaycastOnThisObjectAndChildren:
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(raycastCamera);
                    EditorGUILayout.PropertyField(raycastSource);
                    EditorGUILayout.PropertyField(raycastLayerMask);
                    EditorGUILayout.PropertyField(blockingLayerMask, new GUIContent("Blocking Layer Mask", "Objects in this layer mask will block the highlight if they are between the camera and the target."));
                    EditorGUILayout.PropertyField(minDistance);
                    EditorGUILayout.PropertyField(maxDistance);
                    EditorGUI.indentLevel--;
                    break;
                case TriggerMode.Volume:
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(volumeLayerMask);
                    EditorGUI.indentLevel--;
                    break;
            }

            if (trigger.triggerMode != TriggerMode.Volume) {
                EditorGUILayout.PropertyField(respectUI);
                if (respectUI.boolValue) {
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(unhighlightOnUI);
                    EditorGUI.indentLevel--;
                }
            }
            EditorGUILayout.PropertyField(highlightOnHover);
            EditorGUILayout.PropertyField(selectOnClick);
            if (selectOnClick.boolValue) {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(selectedProfile);
                EditorGUILayout.PropertyField(selectedAndHighlightedProfile);
                EditorGUILayout.PropertyField(singleSelection);
                EditorGUILayout.PropertyField(toggleOnClick);
                if (trigger.triggerMode == TriggerMode.RaycastOnThisObjectAndChildren) {
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PropertyField(keepSelection);
                    if (EditorGUI.EndChangeCheck()) {
                        // Update all triggers
                        HighlightTrigger[] triggers = Misc.FindObjectsOfType<HighlightTrigger>();
                        foreach(HighlightTrigger t in triggers) {
                            if (t.keepSelection != keepSelection.boolValue) {
                                t.keepSelection = keepSelection.boolValue;
                                EditorUtility.SetDirty(t);
                            }
                        }
                    }
                }
                EditorGUI.indentLevel--;
            }

            if (serializedObject.ApplyModifiedProperties()) {
                trigger.Init();
            }
        }

    }

}
