//Created by Neodrop
//neodrop@unity3d.ru
using UnityEngine;
using UnityEditor;
//#pragma warning disable

[CustomEditor(typeof(AI_Range))]
public class AI_RangeEditor : Editor
{

    private AI_Range _target;
    private SerializedObject _serializedObject;
    private bool _changed;
    private Rect _startRect;
    private float _startRectHeight = 100;

    void OnEnable()
    {
        _target = target as AI_Range;
        _serializedObject = new SerializedObject(_target);
    }

    [MenuItem("Antares/AI/Add Range Zones")]
    public static void AddRangeZones()
    {
        Selection.activeGameObject.AddComponent<AI_Range>();
    }

    [MenuItem("Antares/AI/Add Range Zones", true)]
    public static bool AddRangeZonesValidation()
    {
        return Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<AI_Range>() == null;
    }

    public override void OnInspectorGUI()
    {
        _startRect = GUILayoutUtility.GetLastRect();
        _startRect.y += 65;
        _serializedObject.Update();
        _changed = false;

        EditorGUILayout.Separator();
        GUILayout.BeginHorizontal();
        GUILayout.Label("Show View Rings");
        _target.showRings = EditorGUILayout.Toggle(_target.showRings);
        if (GUI.changed)
        {
            _changed = true;
            EditorUtility.SetDirty(_target.gameObject);
        }
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Add Range Zone", EditorStyles.miniButtonLeft, GUILayout.Width(128)))
        {
            _target.showRings = true;
            _target.CreateMarker();
            _changed = true;
        }
        if (GUILayout.Button("Clear All Zones", EditorStyles.miniButtonRight, GUILayout.Width(128)))
        {
            Undo.RegisterSceneUndo("Clear All Zones");
            ClearAllZones();
        }
        GUILayout.EndHorizontal();
        ShowRingsSettings();

        if (_changed) _serializedObject.ApplyModifiedProperties();
        GUILayout.Space(-25);
        AntaresGameObjectCustom.Routines.DrawFooter();
    }

    void ClearAllZones()
    {
        _target.showRings = false;
        foreach (AI_RangeMarker tr in _target.markersComponents)
        {
            DestroyImmediate(tr.gameObject);
        }
        _target.markersComponents.Clear();
        _changed = true;
    }

    void ShowRingsSettings()
    {
        int count = _target.markersComponents.Count;
        for (int i = 0; i < count; i++)
        {
            GUI.Box(new Rect(3, _startRect.y, _startRect.width - 6, _startRectHeight), "");
            _startRect.y += _startRectHeight + 2;

            string zoneNameBefore = _target.markersComponents[i].zoneName;
            _target.markersComponents[i].zoneName = EditorGUILayout.TextField(_target.markersComponents[i].zoneName,
                                                                              GUILayout.Width(256));
            if (zoneNameBefore != _target.markersComponents[i].zoneName)
            {
                _changed = true;
            }

            GUILayout.BeginHorizontal();
            Color colorBefore = _target.markersComponents[i].color;
            _target.markersComponents[i].color = EditorGUILayout.ColorField(_target.markersComponents[i].color, GUILayout.Width(128));
            if (_target.markersComponents[i].color != colorBefore)
            {
                _changed = true;
            }
            _target.markersComponents[i].radius = Vector3.Distance(_target.markersComponents[i].transform.position,
                                                                   _target.transform.position);
            GUILayout.Label("Radius : " + _target.markersComponents[i].radius);
            GUILayout.EndHorizontal();

            float angleBefore = _target.markersComponents[i].angleView;
            _target.markersComponents[i].angleView = EditorGUILayout.Slider(_target.markersComponents[i].angleView, 1f, 360f, GUILayout.Width(256)); //.FloatField(new GUIContent("Angle : ", "Angle of view"), _target.markersComponents[i].angleView, GUILayout.Width(256));
            if (_target.markersComponents[i].angleView != angleBefore)
            {
                _changed = true;
                EditorUtility.SetDirty(_target.markersComponents[i].gameObject);
            }

            GUILayout.BeginHorizontal();
            Transform targetBefore = _target.markersComponents[i].parentTransform;
            _target.markersComponents[i].parentTransform =
                EditorGUILayout.ObjectField(_target.markersComponents[i].parentTransform, typeof(Transform),
                                            GUILayout.Width(128)) as Transform;
            if (_target.markersComponents[i].parentTransform != targetBefore)
            {
                _changed = true;
            }

            GUI.enabled = _target.markersComponents[i].parentTransform != null;
            GUILayout.Label(new GUIContent("   Axis : ", "The LooakAt axis of the selected target object"));
            AI_RangeMarker.Axis axisBefore = _target.markersComponents[i].parentTransformLookAtAxis;
            _target.markersComponents[i].parentTransformLookAtAxis = (AI_RangeMarker.Axis)EditorGUILayout.EnumPopup(_target.markersComponents[i].parentTransformLookAtAxis, GUILayout.Width(128));
            if (_target.markersComponents[i].parentTransformLookAtAxis != axisBefore)
            {
                _changed = true;
            }
            GUILayout.EndHorizontal();
            GUI.enabled = true;

            if (GUILayout.Button("Delete Zone", EditorStyles.miniButton, GUILayout.Width(128)))
            {
                Undo.RegisterSceneUndo("Delete Zone");
                AI_RangeMarker marker = _target.markersComponents[i];
                _target.markersComponents.Remove(marker);
                DestroyImmediate(marker.gameObject);
                count--;
            }
            EditorGUILayout.Separator();
        }
    }
}

[CustomEditor(typeof(AI_RangeMarker))]
public class AI_RangeMarkerEditor : Editor
{
    private AI_RangeMarker _target;

    void OnEnable()
    {
        _target = target as AI_RangeMarker;
    }

    public override void OnInspectorGUI()
    {
        EditorGUILayout.Separator();
        GUILayout.Label("DO NOT REMOVE THIS COMPONENT MANUALLY");
        EditorGUILayout.Separator();
        ;
        _target.radius = Vector3.Distance(_target.transform.position, _target.holder.transform.position);

        GUILayout.Label("Radius : " + _target.radius);
    }
}
