/*
 * @license Private Source License v1.0.0
 * @copyright 2023 Reinhardt Weyers
 *
 * @author Reinhardt Weyers
 *
 * @version 0.1.0
 * 
 * Created On: 04/12/25
 * Last edited: 04/12/25
 */

using UnityEditor;
using UnityEngine;

public class SecretManagerEditorWindow : EditorWindow
{
    private SecretManager secretManager;
    private string newKey = "";
    private string newValue = "";

    private Vector2 scrollPosition;

    [MenuItem("RetroDev/Secrets Editor")]
    public static void ShowWindow()
    {
        GetWindow<SecretManagerEditorWindow>("Secrets ");
    }

    private void OnEnable()
    {
        LoadSecretManager();
    }

    private void LoadSecretManager()
    {
        secretManager = AssetDatabase.LoadAssetAtPath<SecretManager>("Assets/RetroDev/Secrets/Lib/Resources/SecretManager.asset");
        if (secretManager == null)
        {
            Debug.LogError("SecretManager asset not found at specified path.");
            Close();
        }
    }

    private void OnGUI()
    {
        EditorGUILayout.Space(10);
        EditorGUILayout.LabelField("Secrets Editor", EditorStyles.boldLabel);

        EditorGUILayout.Space();

        GUILayout.Label("Manage all your secret values for use during runtime. Values stored here are hidden from code decompilers like ILSpy.", EditorStyles.textArea);

        EditorGUILayout.Space(10);

        EditorGUILayout.HelpBox($"Please note that this is for client secrets only.", MessageType.Warning, true);

        EditorGUILayout.Space(10);

        EditorGUILayout.LabelField("Active Secrets:");

        // Begin a scroll view
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.ExpandHeight(true));

        if (secretManager != null)
        {
            if (secretManager.keyValuePairs.Count == 0)
            {
                EditorGUILayout.LabelField("No active secrets", EditorStyles.boldLabel);
            }
            else
            {
                foreach (var pair in secretManager.keyValuePairs)
                {
                    EditorGUILayout.BeginHorizontal();

                    // Display key field
                    string key = EditorGUILayout.TextField(pair.Key, GUILayout.ExpandWidth(true));

                    // Display value field
                    string value = EditorGUILayout.TextField(pair.Value, GUILayout.ExpandWidth(true));

                    // Update the value if it's changed
                    if (pair.Value != value)
                    {
                        secretManager.keyValuePairs[pair.Key] = value;
                        EditorUtility.SetDirty(secretManager);
                    }

                    // Add a delete button to remove the key-value pair
                    if (GUILayout.Button("Delete", GUILayout.Width(60)))
                    {
                        secretManager.keyValuePairs.Remove(pair.Key);
                        EditorUtility.SetDirty(secretManager);
                    }

                    EditorGUILayout.EndHorizontal();
                }
            }

            EditorGUILayout.Space(40);

            // Add New Key-Value Pair section
            EditorGUILayout.BeginVertical();

            EditorGUILayout.PrefixLabel("Secret Key");
            newKey = EditorGUILayout.TextField(newKey, GUILayout.ExpandWidth(true));

            EditorGUILayout.PrefixLabel("Secret Value");
            newValue = EditorGUILayout.TextField(newValue, GUILayout.ExpandWidth(true));

            EditorGUILayout.EndVertical();

            // Add a new key-value pair
            if (GUILayout.Button("Add Secret", GUILayout.ExpandWidth(true)))
            {
                if (secretManager != null && !string.IsNullOrEmpty(newKey))
                {
                    if (!secretManager.keyValuePairs.ContainsKey(newKey))
                    {
                        secretManager.keyValuePairs.Add(newKey, newValue);
                        EditorUtility.SetDirty(secretManager);
                        newKey = "";
                        newValue = "";
                    }
                    else
                    {
                        Debug.LogWarning("Key already exists. Please enter a unique key.");
                    }
                }
            }
        }

        // End the scroll view
        EditorGUILayout.EndScrollView();

        EditorGUILayout.Space();
    }
}
