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

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

    public void OnEnable()
    {
        targetscript = (ItemManager)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();
   
        GUI.enabled = targetscript.categoryFilter = EditorGUILayout.Toggle("CategoryFilter", targetscript.categoryFilter);
        EditorGUILayout.PropertyField(serializedObject.FindProperty("categories"), true);
        GUILayout.Space(100);
        GUI.enabled = true;
    }

    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 = GUI.Toolbar(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), currentTab, new string[] { "Main", "Interactable", "Options2", "Main", "Audio" });
        switch (currentTab)
        {
            case 0:
                item.id = EditorGUI.IntField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "ID", item.id);
                item.name = EditorGUI.TextField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), "Name", item.name);
                item.sprite = (Sprite)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight), "Sprite", item.sprite, typeof(Sprite), false); 
                item.categoryIndex = EditorGUI.Popup(new Rect(rect.x, rect.y + 100, rect.width, EditorGUIUtility.singleLineHeight),"Category", item.categoryIndex, targetscript.categories);
                item.description = EditorGUI.TextArea(new Rect(rect.x, rect.y +120, rect.width, 40), item.description);
                break;

            case 1:
                GUI.enabled = item.interactable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "Interactable", item.interactable);
                SerializedProperty unityEventProperty = itemProperty.FindPropertyRelative("interactableEvent");
                EditorGUI.PropertyField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), unityEventProperty);
                item.eventDelay = EditorGUI.FloatField(new Rect(rect.x, rect.y + 140, rect.width, EditorGUIUtility.singleLineHeight), "EventDelay", item.eventDelay); 
                item.hotbar = EditorGUI.Toggle(new Rect(rect.x, rect.y + 160, rect.width, EditorGUIUtility.singleLineHeight), "Hotbar", item.hotbar);
                GUI.enabled = true;
                break;

            case 2:
                GUI.enabled = item.stackable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "Stackable", item.stackable);
                item.maxStack = EditorGUI.IntField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), "MaxStack", item.maxStack);
                GUI.enabled = true;
                item.destructable= EditorGUI.Toggle(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight), "Destructable", item.destructable);
                break;

            case 4:
                item.interactAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "Interact", item.interactAudioClip, typeof(AudioClip), true); ;
                item.addAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), "Add", item.addAudioClip, typeof(AudioClip), true); ;
                item.removeAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight), "Remove", item.removeAudioClip, typeof(AudioClip), true); ;
                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 + 200;
    }

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

}