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

public class T1DBEditorWindow : EditorWindow
{
    public enum T1DBType
    {
        AnimActions,
        Animations,
        Sounds,
        Fx
    }
    public static Vector2 scrollPos;
    public static string selectedKey;
    public static SerializedProperty prop;
    public static Dictionary<string, string[]> db;
    public static T1DBType dbType;

    [MenuItem("Window/Turok 1/AnimActions Database")]
    public static void ShowWindowAnimActions()
    {
        ShowWindow(null, T1DBType.AnimActions);
    }
    [MenuItem("Window/Turok 1/Animations Database")]
    public static void ShowWindowAnimations()
    {
        ShowWindow(null, T1DBType.Animations);
    }
    [MenuItem("Window/Turok 1/Sounds Database")]
    public static void ShowWindowSounds()
    {
        ShowWindow(null, T1DBType.Sounds);
    }
    [MenuItem("Window/Turok 1/Fx Database")]
    public static void ShowWindowFx()
    {
        ShowWindow(null, T1DBType.Fx);
    }

    public static void ShowWindow(SerializedProperty editProp, T1DBType newDBType)
    {
        dbType = newDBType;
        scrollPos = new Vector2();
        selectedKey = "";
        prop = editProp;
        string windowTitle = "T1 ";
        switch (dbType)
        {
            case T1DBType.AnimActions:
            {
                db = T1Utils.DBAnimActions;
                windowTitle += "AnimActions";
                break;
            }
            case T1DBType.Animations:
            {
                db = T1Utils.DBAnimations;
                windowTitle += "Animations";
                break;
            }
            case T1DBType.Sounds:
            {
                db = T1Utils.DBSounds;
                windowTitle += "Sounds";
                break;
            }
            case T1DBType.Fx:
            {
                db = T1Utils.DBFx;
                windowTitle += "Fx";
                break;
            }
        }
        T1DBEditorWindow window = (T1DBEditorWindow)EditorWindow.GetWindow(typeof(T1DBEditorWindow), true, windowTitle, true);
        window.minSize = new Vector2(600.0f, 440.0f);
    }

    void OnDestroy()
    {
        if (prop != null && !string.IsNullOrEmpty(selectedKey))
        {
            prop.serializedObject.Update();
            switch (prop.propertyType)
            {
                case SerializedPropertyType.Integer:
                {
                    prop.intValue = int.Parse(selectedKey);
                    break;
                }
                case SerializedPropertyType.Float:
                {
                    prop.floatValue = float.Parse(selectedKey);
                    break;
                }
                case SerializedPropertyType.String:
                {
                    prop.stringValue = selectedKey;
                    break;
                }
            }
            prop.serializedObject.ApplyModifiedProperties();
        }
        prop = null;
    }

    void OnLostFocus()
    {
        if (prop != null)
            Close();
    }

    void OnGUI()
    {
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos, EditorStyles.helpBox);
        if (db != null)
        {
            foreach (KeyValuePair<string, string[]> entry in db)
            {
                EditorGUILayout.BeginHorizontal();
                if (prop == null)
                {
                    EditorGUILayout.SelectableLabel(entry.Key, EditorStyles.helpBox, GUILayout.Width(50), GUILayout.Height(EditorGUIUtility.singleLineHeight));
                }
                else
                {
                    if (GUILayout.Button(entry.Key, GUILayout.Width(50), GUILayout.Height(EditorGUIUtility.singleLineHeight)))
                    {
                        selectedKey = entry.Key;
                        Close();
                    }
                }
                if (entry.Value.Length > 0)
                {
                    if (dbType == T1DBType.AnimActions)
                    {
                        string label = entry.Value[0];
                        if (entry.Value.Length > 1)
                            label += " ---";
                        for (int i = 1; i < 5; i++)
                        {
                            if (i >= entry.Value.Length)
                                break;
                            label += " Arg " + (i - 1) + ": " + entry.Value[i];
                        }
                        EditorGUILayout.SelectableLabel(label, EditorStyles.helpBox, GUILayout.Height(EditorGUIUtility.singleLineHeight));
                    }
                    else
                    {
                        EditorGUILayout.SelectableLabel(entry.Value[0], EditorStyles.helpBox, GUILayout.Height(EditorGUIUtility.singleLineHeight));
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        EditorGUILayout.EndScrollView();
    }
}
