/*           INFINITY CODE          */
/*     https://infinity-code.com    */

using UnityEditor;
using UnityEngine;

namespace InfinityCode.TreeTools.Editors
{
    public abstract class Toolbar
    {
        public const int BUTTON_WIDTH = 32;
        public static GUIContent helpContent;
        public static GUIContent settingsContent;
        public static GUIContent updateAvailableContent;
        
        private static bool inited;
        private static GUIStyle updateAvailableStyle;

        public static void DrawHelpSettings()
        {
            if (GUILayout.Button(settingsContent, EditorStyles.toolbarButton, GUILayout.Width(BUTTON_WIDTH)))
            {
                Prefs.Open(new Vector2(Event.current.mousePosition.x, 21));
            }

            if (Updater.hasNewVersion)
            {
                if (GUILayout.Button(updateAvailableContent, updateAvailableStyle, GUILayout.Width(BUTTON_WIDTH)))
                {
                    Updater.OpenWindow();
                }
            }

            if (GUILayout.Button(helpContent, EditorStyles.toolbarButton, GUILayout.Width(BUTTON_WIDTH)))
            {
                DrawHelpMenu();
            }
        }

        private static void DrawHelpMenu()
        {
            GenericMenu menu = new GenericMenu();

            menu.AddItem(new GUIContent("Documentation"), false, Links.OpenLocalDocumentation);
            menu.AddItem(new GUIContent("Changelog"), false, Links.OpenChangelog);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Asset Store"), false, Links.OpenAssetStore);
            menu.AddItem(new GUIContent("Homepage"), false, Links.OpenHomepage);
            menu.AddItem(new GUIContent("Support"), false, Links.OpenSupport);
            menu.AddItem(new GUIContent("Forum"), false, Links.OpenForum);
            menu.AddItem(new GUIContent("Discord"), false, Links.OpenDiscord);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Check Updates"), false, Updater.OpenWindow);
            menu.AddItem(new GUIContent("About"), false, About.OpenWindow);

            menu.ShowAsContext();
        }

        public static void InitGeneral()
        {
            if (inited) return;
            inited = true;

            settingsContent = new GUIContent(Icons.settings, "Settings");
            helpContent = new GUIContent(Icons.help, "Help");
            updateAvailableContent = new GUIContent(Icons.updateAvailable, "Update Available");
            updateAvailableStyle = new GUIStyle(EditorStyles.toolbarButton)
            {
                padding = new RectOffset(4, 4, 4, 4)
            };
        }
    }
}