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

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

    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];

        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();
        
       if (fold = EditorGUI.Foldout(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight),fold, "Foldout"))
            {
                item.executable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 100, rect.width, EditorGUIUtility.singleLineHeight), "Executable", item.executable);
                item.stackable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 120, rect.width, EditorGUIUtility.singleLineHeight), "Stackable", item.stackable);
                item.removeable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 140, rect.width, EditorGUIUtility.singleLineHeight), "Removeable", item.removeable);
                item.hotbar = EditorGUI.Toggle(new Rect(rect.x, rect.y + 160, rect.width, EditorGUIUtility.singleLineHeight), "Hotbar", item.hotbar);
            }

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

    private float ElementHeightCallback(int indexer)
    {
        float propertyHeight = EditorGUI.GetPropertyHeight(reorderableList.serializedProperty.GetArrayElementAtIndex(indexer), 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;
    }

}