﻿#region Copyright

/* -----------------------------------------------------------------------------
 *  Copyright © 2024 • Chomp Studios • All Rights Reserved
 * -----------------------------------------------------------------------------
 *  Date: 30/10/2024
 *  -----------------------------------------------------------------------------
 *  Description: Reusable code to render tooltips on the tools
 *  -----------------------------------------------------------------------------
 */

#endregion

#if UNITY_EDITOR

#region Imports

using UnityEditor;
using UnityEngine;

#endregion

namespace Workflow_Toolkit.Utilities {
    public abstract class TooltipElements : GUIDrawer {
        /// <summary>
        ///     Writes a tooltip in the last GUI rectangle drawn.
        /// </summary>
        /// <param name="tooltip">The tooltip text to display.</param>
        public static void WriteTooltip(string tooltip) {
            var typeRect = GUILayoutUtility.GetLastRect();
            GUI.Label(typeRect, new GUIContent("", tooltip));
        }

        /// <summary>
        ///     Renders a checkbox in the Window with a tooltip.
        /// </summary>
        /// <param name="label">The displayed label for the toggle.</param>
        /// <param name="value">The current bool state of the toggle.</param>
        /// <param name="tooltip">The tooltip text to display when hovering.</param>
        /// <returns>Returns the updated bool value after the toggle is clicked.</returns>
        public static bool Toggle(string label, bool value, string tooltip) {
            GUILayout.BeginHorizontal();
            var result = EditorGUILayout.Toggle(new GUIContent(label, tooltip), value);
            GUILayout.EndHorizontal();
            return result;
        }

        /// <summary>
        ///     Renders a button in the Window with a tooltip.
        /// </summary>
        /// <param name="label">The displayed text on the button.</param>
        /// <param name="tooltip">The tooltip text to display when hovering.</param>
        /// <param name="options">Optional layout parameters.</param>
        /// <returns>Returns true if the button was clicked.</returns>
        public static bool Button(string label, string tooltip, params GUILayoutOption[] options) {
            return GUILayout.Button(new GUIContent(label, tooltip), options);
        }

        /// <summary>
        ///     Renders a dropdown menu in the Window that shows a tooltip
        /// </summary>
        /// <param name="label">The displayed label next to the popup.</param>
        /// <param name="selectedIndex">The currently selected index in the list.</param>
        /// <param name="displayedOptions">An array of strings for the dropdown.</param>
        /// <param name="tooltip">The tooltip text to display when hovering.</param>
        /// <returns>Returns the updated index after a selection is made.</returns>
        public static int Popup(string label, int selectedIndex, string[] displayedOptions, string tooltip) {
            GUILayout.BeginHorizontal();
            var result = EditorGUILayout.Popup(new GUIContent(label, tooltip), selectedIndex, displayedOptions);
            GUILayout.EndHorizontal();
            return result;
        }

        /// <summary>
        ///     Renders a text field in the Window with a tooltip.
        /// </summary>
        /// <param name="label">The label displayed next to the text field.</param>
        /// <param name="text">The current string value in the text field.</param>
        /// <param name="tooltip">The tooltip text displayed when hovering.</param>
        /// <param name="options">Optional layout parameters.</param>
        /// <returns>Returns the updated string after editing the text field.</returns>
        public static string TextField(string label, string text, string tooltip, params GUILayoutOption[] options) {
            GUILayout.BeginHorizontal();
            var result = EditorGUILayout.TextField(new GUIContent(label, tooltip), text, options);
            GUILayout.EndHorizontal();
            return result;
        }

        /// <summary>
        ///     Renders a multi-line, word-wrapped text area in the Window with a tooltip.
        /// </summary>
        /// <param name="text">The current string value in the text area.</param>
        /// <param name="tooltip">The tooltip text displayed when hovering.</param>
        /// <param name="options">Optional layout parameters.</param>
        /// <returns>Returns the updated string after editing the text area.</returns>
        public static string TextArea(string text, string tooltip, params GUILayoutOption[] options) {
            var result = EditorGUILayout.TextArea(text, WorkflowStyles.WrappedTextArea, options);
            WriteTooltip(tooltip);
            return result;
        }
    }
}
#endif