using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using UnityEditor.Events;

[CustomEditor(typeof(Main))]
public class CustomMain : Editor
{
    private Main targetscript;
    private ReorderableList reorderableList;
    bool fold = false;
    bool showinteractable = false;
    int currentTab;

    public void OnEnable()
    {
        targetscript = (Main)target;
        reorderableList = new ReorderableList(serializedObject, serializedObject.FindProperty("items"), true, true, true, true);
        reorderableList.drawHeaderCallback += DrawHeader;
        reorderableList.drawElementCallback += DrawElement;
        reorderableList.elementHeightCallback += ElementHeightCallback;
        reorderableList.onAddCallback += AddItem;
        reorderableList.onRemoveCallback += RemoveItem;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        reorderableList.DoLayoutList();
    }

    private void DrawHeader(Rect rect)
    {
        GUI.Label(rect, "Items");
    }

    private void DrawElement(Rect rect, int index, bool active, bool focused)
    {
        EditorGUI.BeginChangeCheck();
        Item item = targetscript.items[index];
        SerializedProperty itemProperty = serializedObject.FindProperty("items").GetArrayElementAtIndex(index);
       
        currentTab = GUILayout.Toolbar(currentTab, new string[] { "Main", "Prop", });
        switch (currentTab)
        {
            case 0:
                item.id = EditorGUI.IntField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), "ID", item.id);
                item.name = EditorGUI.TextField(new Rect(rect.x, rect.y + 20, rect.width, EditorGUIUtility.singleLineHeight), "Name", item.name);
                item.description = EditorGUI.TextField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "Description", item.description);
                GUILayout.BeginHorizontal();
                item.sprite = (Sprite)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), "Sprite", item.sprite, typeof(Sprite), false);
                GUILayout.EndHorizontal();
                break;

            case 1:
                item.id = EditorGUI.IntField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), "TEST", item.id);
                break;
        }

        if (EditorGUI.EndChangeCheck())
        {
            EditorUtility.SetDirty(target);
            serializedObject.ApplyModifiedProperties();
        }
    }

    private float ElementHeightCallback(int index)
    {
        float propertyHeight = EditorGUI.GetPropertyHeight(reorderableList.serializedProperty.GetArrayElementAtIndex(index), true);
        return propertyHeight + 180;
    }

    private void AddItem(ReorderableList list)
    {
        targetscript.items.Add(new Item());
        EditorUtility.SetDirty(target);
    }

    private void RemoveItem(ReorderableList list)
    {
        targetscript.items.RemoveAt(list.index);
        EditorUtility.SetDirty(target);
    }

    private void OnDisable()
    {
        reorderableList.drawHeaderCallback -= DrawHeader;
        reorderableList.drawElementCallback -= DrawElement;
        reorderableList.onAddCallback -= AddItem;
        reorderableList.onRemoveCallback -= RemoveItem;
    }

}