// Stylized Water 3 by Staggart Creations (http://staggart.xyz)
// COPYRIGHT PROTECTED UNDER THE UNITY ASSET STORE EULA (https://unity.com/legal/as-terms)
//    • Copying or referencing source code for the production of new asset store, or public, content is strictly prohibited!
//    • Uploading this file to a public repository will subject it to an automated DMCA takedown request.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Rendering;
#if URP
using UnityEngine.Rendering.Universal;
#endif

namespace StylizedWater3
{
    public class HelpWindow : EditorWindow
    {
        //Window properties
        private static readonly int width = 600;
        private static readonly int height = 640;

        private Changelog changelog;
        
        private bool underwaterExtensionInstalled;
        private bool dynamicEffectsInstalled;
        [NonSerialized] private string projectDetails;
        private Vector2 projectDetailsScrollPos;
        
		[MenuItem("Window/Stylized Water 3/Hub Window", false, 1001)]
        public static void ShowWindow()
        {
            ShowWindow(false);
        }

        public static void ShowWindow(bool installation)
        {
            HelpWindow editorWindow = EditorWindow.GetWindow<HelpWindow>(true, AssetInfo.ASSET_NAME, true);

            //Open somewhat in the center of the screen
            editorWindow.position = new Rect((Screen.currentResolution.width / 2f) - (width * 0.5f), (Screen.currentResolution.height / 2f) - (height * 0.5f), (width * 2), height);

            //Fixed size
            editorWindow.maxSize = new Vector2(width, height);
            editorWindow.minSize = new Vector2(width, height);

            if (installation) editorWindow.selectedSectionIndex = 1;
            
            editorWindow.Show();
        }

        [MenuItem("Window/Stylized Water 3/Forum", false, 1002)]
        public static void OpenForum()
        {
            AssetInfo.OpenForumPage();
        }

        private class Constants
        {
            public readonly GUIStyle sectionScrollView = "PreferencesSectionBox";
            public readonly GUIStyle sectionElement = "PreferencesSection";
            public readonly GUIStyle selected = "OL SelectedRow";
            public readonly GUIStyle sectionHeader = new GUIStyle(EditorStyles.largeLabel);

            public Constants()
            {
                sectionScrollView = new GUIStyle(sectionScrollView);
                sectionScrollView.overflow.bottom += 1;

                sectionHeader.fontStyle = FontStyle.Bold;
                sectionHeader.fontSize = 18;
                sectionHeader.margin.top = 10;
                sectionHeader.margin.left += 1;
                if (!EditorGUIUtility.isProSkin)
                    sectionHeader.normal.textColor = new Color(0.4f, 0.4f, 0.4f, 1.0f);
                else
                    sectionHeader.normal.textColor = new Color(0.7f, 0.7f, 0.7f, 1.0f);
            }
        }
        
        static Constants constants = null;
        private Section installationSection;
        private void OnEnable()
        {
            AssetInfo.VersionChecking.CheckForUpdate(false);
            AssetInfo.VersionChecking.CheckUnityVersion();
 
            underwaterExtensionInstalled = StylizedWaterEditor.UnderwaterRenderingInstalled();
            dynamicEffectsInstalled = StylizedWaterEditor.DynamicEffectsInstalled();
            
            Installer.Initialize();
            
            sections = new List<Section>();
            sections.Add(new Section("Home", DrawHome));
            installationSection = new Section("Installation", DrawInstallation);
            sections.Add(installationSection);
            sections.Add(new Section("Changelog", DrawChangelog));
            sections.Add(new Section("Integrations", DrawIntegrations));
            //sections.Add(new Section("Extensions", DrawExtensions));
            sections.Add(new Section("Support", DrawSupport));
            
        }

        private void OnFocus()
        {
            Installer.Initialize();
        }

        private Texture m_HeaderImg;
        private Texture HeaderImg 
        { 
            get 
            { 
                if (m_HeaderImg == null)
                    m_HeaderImg = UI.CreateIcon("[encoded data, 92208 characters]");
                return m_HeaderImg;
            } 
        }
        private Texture m_ReviewIcon;
        private Texture ReviewIcon 
        { 
            get
            {
                if (m_ReviewIcon == null)
                    m_ReviewIcon = UI.CreateIcon("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAD0ElEQVRYCb1XaWxMURT+ph2GqUpF2libNtQyQlClIQSR1JYgokSkPyxVS/1C/KA/CJFUxBaRWEIJIVEk+FGSirYSZUgItYWOEkuVGqP7tL7zdKZ3xps3ozPpSb73zj33nu/cd+57595naruDiIhpemg0ajzxMamG0Ci8o9KolRE1hOgfiP8W8397dDgcoNqN6Ff0AGUZWzCBenVHt76mPnA4GZhO+mI1xLRN2Mf2FtWm6ncPAW1tTLlJtQKdzUCuLw2Q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m_ReviewIcon; 
            } 
        }
		private Texture m_DocIcon;
        private Texture DocIcon 
        { 
            get
            {
                if (m_DocIcon == null)
                    m_DocIcon = UI.CreateIcon("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEzklEQVRYCbVXTUhjVxR+L8nExtHEn6KtadMZ7DCLQVtShC5MRYrVYkFEQUUQxYWLrkRdjGhVxFIcLUPp1k0XrlwquKiVUFroDJV2OiJjFVRSicaf4kz+TGLmO7fvPl7eezeNZXrhcs89f/fknPPd+yLv7+9LZiOdTjO2LMtmYgOP63N/DocjQ2dtbS1jzzcyN+AMZf0eDutB4/ycIngOfRv0/ZifKj5yWsjIbLyJcy3RaDR5dXVFUZjpMB4OTttstpt5eXkWMJr29vYCWN8SGugEogBeJz2v1/tzJBJ5AfIKUxjF9PT0uz09PXeXlpYCDQ0NZeFw+AT6zAfWrEMUQJSshoeHPV1dXQ/z8/MzC6pxiezsIUudYN1dX18/Gxoa+gpl/fbo6Ohv8Io0qqakDRk0E7ygtPf399+C8KGZAueRPS+R2+3OA/8R9nfKy8v/PDk5ucDeyXXNVlEGmNHc3Nx2MBi8dLlcIj1pZ2cn2tvb666vry+zWCxUpjKPx/N4YGDg/dHR0d9CoVDWIGw8el109EukhYWFIHrga5CnmIXEMxsTExPN4H+u+GK9gv75fXV1tbqpqenJ8fHxc8hN7WV0rZnPp3B2z0xwTV4QJXLAl2txcfEJbN/T24syQJCSUMNYKpWSkFq9nbqntBNcz8/PE8XFxXZcQFY0JWusRCLhQgPLRUVFUjKZdI6Pj/fA8DvVGISotqWk5PP5HqEEQZApTHEU//ghuJ5jlmDexExiSlNTU3fQI15kgkpjJZ52iAKIkNLY2Nittra21dcwtEZEwyEdgOzKeqfUdHz8BT3qJ6/CMNwlIhiG4Vjq7u72wPBL7k27kjyXwfUQCJXFgHlRBtgFMjg4+Mfh4eElaqvqAVaJyspKx+zs7D30SBwIeFZYWGi12+2GEm1vb0f6+vrcjY2NbyAQitgYAI9Q92tu0B4wOkcP3AdJV6sKI9wLdDVvxGKx5PLy8jPQDzDzMZkdVnXMzMx8gs2IwjCkTVSCEAIr29ra+giGP6nedMTFxQX1AfXLY52IbRWIf6CRGQMQZICl8/T0NK5/DamUVqtVLikpsSs3H3U/G4I7hYtNV7W2OimDYW1t7S8oQQiyDBiWlpZaNzY2WgsKClQEZDmcyiUcogAYDNFgt1tbW39Ag9m5B2QsDJqwLjmdTvv8/Lyvvb39ATKjBqPRPQZNZRQOUQ8wGHZ2dr4Ny0mRNQK4gcOrIa8WlFI1/U8wxPfA00AgEEfKDd3Nr1ulD9SDtMTm5mZ4ZGTknebm5goEeH0YrqysEAxH4TQDhppDOK4N3c11ampqPgP9hbI36IlKwGHog+GP3FkO6yVSza5oRZcakD3tSgmMAQhqxxrq4OAgjBctDdhlPRvOJbya6YqKCgdeP7qQRMOACBEKWJfjCv0VJdiFN/pV2aPANYtbsaGqqsojOt2MLwqAwXBycvJ2S0uLH4+hCkMzJ8gA/TJ6ij80k2fjiXqAwbCjo4NgOJ7NAckEZfw3MyY3vGA5Wb1CJdEnWYzOiMfjqVdwlkz/mvBJxiGb4dIGQQZD2XxcV1f3Db71KnHRxM0UrsPDXzf57OyMmtjwgwy41Dim7zpqUgN0NDrXISkAekcSWiN5d3dX3eNXS36/n+3x1aPy/0/iJfpMtnfwrhsgAAAAAElFTkSuQmCC");
				return m_DocIcon; 
            } 
        }
		private Texture m_FaqIcon;
        private Texture FaqIcon 
        { 
            get
            {
                if (m_FaqIcon == null)
                    m_FaqIcon = UI.CreateIcon("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAFRklEQVRYCa1XXyykVxT/vjEo1qKxSUuxiSUxTUtS3T5oMgm7NmM3EtKkL0v6sjw0G2lVmj548lCLCtG0kRINb4ZGttXSbiTYJbPjT2xaFqNhsBjs6CzKjD/9nc93ufPNmD/Sk5w5/++937nnnntHnJ+fFwgSEhIkyn7MZrMgiqIkHh0dCUFBQcLBwYFAvB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SebgjW5tbdkLCwvH0tLSnmArHo2Pj/tVQ01NTdQzhmkeFb12ZaQHg0/Q1dX1or+/fwGL/hTnXre6uip1NV+CabG4Lbfh+xP5q/E1LO53ME8jIyO9voRQL3R2HwL/kh+v/6Ae2DgeaVlZGR3fMeDf5KjGLUiUwU1shxXP8NMqZQae5ufnx+BFJN2GKOLXgd/x9vP4+vp60+zs7B7sNcxHHBoaYjyjN2JjY+nB4RVwNZuDg4Mj4XjZmzO1bblzfgvfTuYvNjY2Mp6nxejx3+NPhtQmecNF+J6enpXi4mJKfQ/wAT+GWn6Q8DriG6G35ubmNuMNcElp9EeWHx9LiPkV6HJLevvCq5WVlXosJDUsLCzQn4lx1jeqq6vn5YqnGnHba0R8odDW1uY0dnt7uyTX1dXRK4j4ory8vPs4829RRuRT4BRDgtVq3TcYDC/1er0FtUWNhi6oFqBU8aAucJoBZS1gz5TOYVBQ6/0QSCcgAOgOXkL5FEiFTNe1RxAnJugtcQItLbTYM+AyIOBBemb4H7n/AIKWOUx2b5M6AAAAAElFTkSuQmCC");
				return m_FaqIcon; 
            } 
        }
        private Texture m_DiscordIcon;
        private Texture DiscordIcon 
        { 
            get
            {
                if (m_DiscordIcon == null)
                    m_DiscordIcon = UI.CreateIcon("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAElElEQVRYCb1WbUxbZRS+t7TMQfnoYMIYA8rKwkLWaSTMqH80+7FFs8wM94FfhGXZD0O2337Fjx9qdFmMxsQtatTMMZdoZh1uEswWzOLClH1RGbD2Im3pQCylXytbW59z6VvfW+6FgoyTPL3ve85z3ve85557+oq7900K85XBPz8XLOtb5nSLx6cEnS47xTv97UOpMRuICwmAOc/n6Ru/KtPPfveYwk2nmGU4oQxkIpQBJqYiq0CgA5uKNwp5BTUyljwDLCDKBAWhZ4r5PBdSA3R6vgZo84B/QMg0A/kIcBPwBNAB/AJoyXswuIBOwM5IWjUwVwA7sMAeYAuQS4vdvRPySYMnDuIr+AfTAoDq6DZ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qg60Adb5TTJnhMw+8PcAl4A81n/QaELgMCMiAMOmbeZnFO6bbsdp6so5lgR1Ik0gGRuZJlIH/I8kayGgJucAoCKoBVgcs/ZSB+cr0gUTVg6mtlUkNqPktmu5fRzyST440lD0AAAAASUVORK5CYII=");
                return m_DiscordIcon; 
            } 
        }
        private Texture m_ForumIcon;
        private Texture ForumIcon 
        { 
            get
            {
                if (m_ForumIcon == null)
                    m_ForumIcon = UI.CreateIcon("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAH+0lEQVRYCaVXC2ycxRGe3f//77873yM5O36eH7EDtYNjGpPIgUYUqxSkFlRVNEUVIpC2gqatqoJoUYqoAFG1Ki1UKqpKH0hAyAskRFNCEhJcNyoxyE4dHCdxEjvBju07v+98r/+5nbn4N2fHlwQ68nhud2dnvp2dnd2f/XBnD/jtacilaaUs2wymRyBT1ACqnQafIsDmCsicAWM8O27ZNoCwgQsDxjIyMG8Q3vvj47Du7gey40Nd7+eahdaHtkHbX369oI/97JX2BR2LGgwBrEAAzQjgqwigGQEUIIBy1DMRQAQBJBBAOwJoQwA92B+bvnASKptasqYS8VnwBfzzZhe32UN7zs4PLvpRJWmxzZVVKx/xyVaoLOACYs4Y+FQOtgBI6TahgJFZAybSFoylpXN7nt/2LEZgH9oaX2RvySb7wfaupQYeDZWEf7427CupC0oQ8rnAxHAbJnpFIudEuBtZcskcFGwMTadh2FLgaN/kh+5Q6WYcPHNJI/9/OciSi0dfbPhCw49uKCuAYr8KSd2CWNpYrLOgrWMUiIpQv0Jm4DEKWrqSsAf3exN25w0xzZHlQCFJh/7UeN31W28qlcGyTJhKmQBzq3QUriRThgUZnNJQ7IXkJNyY2bjhCdR/8Epz5KnplDP+i5Yb67euLbIhlcmANRdmcKSjdRVJsUjhpFq3AelbWh94/sebd2HX/nzTZEmbobENgZqmZ+tDEmgm7vW893zTrtyvYZIE3ByKMXolq2++C7XzA7B9pWTta42lHhb0cJhJWXjOr+zgWkbphKiZJNz+r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vQKGGaxvvHLnQJ44ffL0nER0cwL6BvV1Tf1hT3/WiVFPOLOOyekLTr0wYNYb7KtvjcPiDvv7uYRimCQyJnqyzKLNt/Cds2w6jrEKm7bmAfF6VGdv92KrXvvH1lu8YUgMIg55yTu7iz6sQk1V80uPJiR6C7/3m6N9f7hBP48JHnGNo5dyC1Ee3aQSZ7vBJZGExt9j1QfxAIN63fuN6fx1X8CmHFRDPFw7PyTynghanmFMAg4fhdzs/fv+3h8w3yDb6nJWQaAvmt4GcIRMA4vkl4ruY7nb98EmjrUKMNq0OTtQqBV6Q8GhRYpITCmJ2M1HS3QH4rqS4cmsGejvbR7/53KntuzqtfabN/4umR3HIksi5ZVlAeZATBRxbSLhGWidx/B/dmSMfnhwfK4GRWo8+7g8EZS57bZAwMbnQsbik8ILHa5v7BN4HLDnSC6/tP/Huqx3mm6YtncZKNoDIsDLhg4UcO87n8mCh52yL1rGAYwOTdt/2juThM8MzQ5PDQ9bpnrNs6NzpWCxyPnW86/hYbHJYq6xa7hdsGSiqAvr0oNE7bLwViZn0IULRzRLDLcAnuIWhxJuLvvWQ8keCQNCuOBJUbISQl2Ogwy5FLqkOWcH+MStREQD/7ocLH765tfEGoG9N4yJ81HXhIOreiTxPWQBOixw77PQ50kU+kXQx9+PT9KBu6vSiCKB0IUhK5FhJkIcbVtj337POv6FpVdGKlw6N797RkXgGxyhzs/T/RsCx45ibk/O5S+1ggVu6SRIiFNc41hSTzn90ThGTAENP7OTCtefBvImcHxQIJ0L4TRC8VOAQAATwg/ZS3ctRxzr4P35rtWC9T6t3AAAAAElFTkSuQmCC");
                return m_ForumIcon; 
            } 
        }
        private Texture m_TwitterIcon;
        private Texture TwitterIcon 
        { 
            get
            {
                if (m_TwitterIcon == null)
                    m_TwitterIcon = UI.CreateIcon("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAE5ElEQVRYCZ3VW2iWdRzA8b1LnTs4U9FVzra5bB6W6WblYaQkREE3ItGBJLyNCC8CF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rOwO+GMCzSJWAlG7EGJuPhcgq7cBL+M6qCSebiV/haOpnXFbAh7Y1zGLUVqRKwqg+wDjaUy+j7/TUacAcu/QRYrUm4PU5kQ5rcRLgV9kwtTGRYJEsgn6es+l24pDaXe7wTrkBwyvlv1y1xYA+iJTDxswj6wt8vg8l4bpj4g0iUQA53V+M93EcHfkA1rNwqg3BPrdatuQ23w4Z1O+x+G9UJ34AJ9uMCHiQxMgGX0we3YgHacBA1cBIHSBR9fOlZUIr5mIXLuAR/4/ZUoBBBYgP8PSxMxuwPwAwdcA8cMBPpwtfVsyLoj2P8vRieIc/hONyqn2GisQhWIMKnYmzFSzA7l8pX6yhc5nQxyAOeej1wFa26AM24AYvy+xI4n81qf8Q++HA1HMC9tIrtKIeVhQ0HLsIOdMNJbeCO+NXmtTiTegt5rsA0vI21yEY7mvAlzNJGHEu4Wm1wO2ciPy6Hq03rSvlaz0CTCdyDzeL+eL63YjdOwwrGGk7ShRPwbxvUVbgCVyIavzp29v/0qmv/9is4ZAAAAABJRU5ErkJggg==");
                return m_TwitterIcon; 
            } 
        }

        private static Color defaultColor;
        
        private delegate void OnGUIDelegate();
        private class Section
        {
            public GUIContent content;
            public OnGUIDelegate drawFunction;

            public Section(string name, OnGUIDelegate drawFunction)
            {
                this.content = new GUIContent(name);
                this.drawFunction = drawFunction;
            }
        }
        
        private List<Section> sections;
        private int _selectedSectionIndex;
        private int selectedSectionIndex
        {
            get => _selectedSectionIndex;
            set
            {
                _selectedSectionIndex = value;
                if (_selectedSectionIndex >= sections.Count)
                    _selectedSectionIndex = 0;
                else if (_selectedSectionIndex < 0)
                    _selectedSectionIndex = sections.Count - 1;
            }
        }
        private Section selectedSection => sections[_selectedSectionIndex];

        private Vector2 menuScrollPos;
        private static Vector2 sectionScrollPos = Vector2.zero;
        
        private void OnGUI()
        {
            EditorGUIUtility.labelWidth = width / 4f;
            defaultColor = GUI.contentColor;

            if (constants == null) constants = new Constants();
            if (changelog == null) changelog = new Changelog();
            
            if (EditorApplication.isCompiling)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField(new GUIContent(" Compiling scripts...", EditorGUIUtility.FindTexture("cs Script Icon")), UI.Styles.H1);
                EditorGUILayout.Space();
                return;
            }
            
            Rect rect = EditorGUILayout.GetControlRect();
            rect.x -= 3f;
            rect.y -= 3f;
            rect.height = 85f;
            rect.width = width;
            GUI.DrawTexture(rect, HeaderImg);
            float backgroundTint = EditorGUIUtility.isProSkin ? 0f : 1f;
            EditorGUI.DrawRect(rect, new Color(backgroundTint, backgroundTint, backgroundTint, 0.66f));
            
            GUILayout.Space(-7f);
            EditorGUILayout.LabelField("<size=26><color=#fff><b>" + AssetInfo.ASSET_NAME + "</b></color></size>", UI.Styles.H1);
            GUILayout.Space(-5f);
            EditorGUILayout.LabelField("<size=12><color=#fff><i>by Staggart Creations</i></color></size>", UI.Styles.H1);
            GUILayout.Space(16f);

            GUILayout.Space(4f);

            if(sections == null) OnEnable();
            
            #if !URP
            UI.DrawNotification("The Universal Render Pipeline package isn't installed", MessageType.Error);
            #endif
            
            GUILayout.BeginHorizontal(); 
            {
                menuScrollPos = GUILayout.BeginScrollView(menuScrollPos, constants.sectionScrollView, GUILayout.Width(120f)); 
                {
                    //GUILayout.Space(40f);
                    for (int i = 0; i < sections.Count; i++)
                    {
                        var section = sections[i];

                        Rect elementRect = GUILayoutUtility.GetRect(section.content, constants.sectionElement, GUILayout.ExpandWidth(true));

                        if (section == selectedSection && Event.current.type == EventType.Repaint)
                        {
                            constants.selected.Draw(elementRect, false, false, section == selectedSection, false);
                        }

                        if (GUI.Toggle(elementRect, selectedSectionIndex == i, section.content, constants.sectionElement))
                        {
                            selectedSectionIndex = i;
                        }
                    }
                } 
                GUILayout.EndScrollView();

                GUILayout.Space(5f);

                GUILayout.BeginVertical(); 
                {
                    //GUILayout.Label(selectedSection.content, constants.sectionHeader);
                    GUILayout.Space(5f);
                    
                    sectionScrollPos = EditorGUILayout.BeginScrollView(sectionScrollPos);
                    selectedSection.drawFunction();
                    EditorGUILayout.EndScrollView();
                } 
                GUILayout.EndVertical();
            } 
            GUILayout.EndHorizontal();
        }
        
        void DrawHome()
        {
            UI.DrawNotification(Installer.HasError, "One or more errors were detected in your project", "View", () => { selectedSectionIndex = 1; }, MessageType.Error);
            
            using (new EditorGUILayout.VerticalScope())
            {
                UI.DrawH2("Thank you for licensing Stylized Water 3!");

                EditorGUILayout.LabelField("This window houses common functionality and installation information.", EditorStyles.label);

                EditorGUILayout.Space();

                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button(new GUIContent("<b><size=12>  Write a review</size></b>\n<i>  Support further development</i>", ReviewIcon), UI.Styles.Button, GUILayout.Height(45f), GUILayout.Width(240f)))
                    {
                        AssetInfo.OpenReviewsPage();
                    }
                    GUILayout.FlexibleSpace();
                }
                EditorGUILayout.Space();

            }

            EditorGUILayout.Space();
            
            using (new EditorGUILayout.VerticalScope())
            {
                UI.DrawH2("Extension assets");
                
                DrawExtensions();
                
                EditorGUILayout.Space();
            }
            
            EditorGUILayout.Space();

            using (new EditorGUILayout.VerticalScope())
            {
                #if URP
                UI.DrawH2("Quick set up");

                GUILayout.Space(5f);
                EditorGUILayout.LabelField("<i>Options can be found under GameObject->3D Object->Water</i>", UI.Styles.WordWrapLabel);
                #endif
                
                EditorGUILayout.Space();
            }
        }

        void DrawInstallation()
        {
            using (new EditorGUILayout.VerticalScope(EditorStyles.textArea))
            {
                foreach (Installer.SetupItem context in Installer.SetupItems)
                {
                    if (UI.DrawSetupItem(context))
                    {
                        return;
                    }
                }
            }
        }

        void DrawChangelog()
        {
            using (new EditorGUILayout.VerticalScope())
            {
                EditorGUILayout.Separator();
                
                changelog.Draw();
            }
        }
        
        void DrawIntegrations()
        {   
            UI.DrawH2("Fog integrations");

            foreach (FogIntegration.Integration integration in FogIntegration.Integrations)
            {
                if(integration.asset == FogIntegration.Assets.None || integration.asset == FogIntegration.Assets.UnityFog) continue;
                
                UI.DrawIntegration(integration);
                EditorGUILayout.Space();
                
                if (underwaterExtensionInstalled && (integration.underwaterCompatible == false))
                {
                    UI.DrawNotification("Editing of shader files for this asset is required to incorporate support for the Underwater Rendering extension. (See documentation section called \"Transparent Materials\")", MessageType.Warning);
                }
            }
            
            EditorGUILayout.Space();

        }

        void DrawExtensions()
        {
            using (new EditorGUILayout.VerticalScope())
            {
                EditorGUILayout.LabelField("Installed", EditorStyles.boldLabel);
                for (int i = 0; i < Extension.installed.Length; i++)
                {
                    Extension extension = Extension.installed[i];
                    
                    //EditorGUILayout.LabelField(extension.name);
                    UI.DrawExtension(extension.name, extension.description, true, extension.assetStoreID, extension.icon);
                }

                if (Extension.available.Length > 0)
                {
                    EditorGUILayout.Space();
                    
                    EditorGUILayout.LabelField("Available for purchase", EditorStyles.boldLabel);
                    
                    for (int i = 0; i < Extension.available.Length; i++)
                    {
                        Extension extension = Extension.available[i];

                        //EditorGUILayout.LabelField(extension.name);
                        UI.DrawExtension(extension.name, extension.description, false, extension.assetStoreID, extension.icon);
                        
                        EditorGUILayout.Separator();
                    }
                }
            }
        }
        
        void DrawSupport()
        {
            UI.DrawNotification(AssetInfo.VersionChecking.unityVersionType != AssetInfo.VersionChecking.UnityVersionType.Release, $"You are using a {AssetInfo.VersionChecking.unityVersionType} version of Unity. You may run into issues at own risk.", MessageType.Warning);

            using (new EditorGUILayout.VerticalScope())
            {
                EditorGUILayout.BeginHorizontal();

                if (GUILayout.Button(new GUIContent("<b><size=12>  Documentation</size></b>\n<i><size=11>  Usage instructions</size></i>", DocIcon), UI.Styles.Button))
                {
                    Application.OpenURL(AssetInfo.DOC_URL);
                }
                if (GUILayout.Button(new GUIContent("<b><size=12>  FAQ/Troubleshooting</size></b>\n<i><size=11>  Common issues and solutions</size></i>", FaqIcon), UI.Styles.Button))
                {
                    Application.OpenURL(AssetInfo.DOC_URL + "?section=troubleshooting-faq-3");
                }
                EditorGUILayout.EndHorizontal();
            }
            
            EditorGUILayout.Space();

            using (new EditorGUILayout.VerticalScope())
            {
                UI.DrawNotification("Be sure to consult the documentation, it already covers many common topics", MessageType.Info);
                
                EditorGUILayout.Space();

                //Buttons box
                EditorGUILayout.BeginHorizontal();
                
                if (GUILayout.Button(new GUIContent("<b><size=12>  Discord</size></b>\n<i><size=11>  Access support</size></i>", DiscordIcon), UI.Styles.Button))
                {
                    Application.OpenURL(AssetInfo.DISCORD_INVITE_URL);
                }
                if (GUILayout.Button(new GUIContent("<b><size=12>  Forum</size></b>\n<i><size=11>  Join the discussion</size></i>", ForumIcon), UI.Styles.Button))
                {
                    Application.OpenURL(AssetInfo.FORUM_URL);
                }                
                if (GUILayout.Button(new GUIContent("<b><size=12>  X</size></b>\n<i><size=11>  Follow developments</size></i>", TwitterIcon), UI.Styles.Button))
                {
                    Application.OpenURL("https://twitter.com/search?q=staggart%20creations&f=user");
                }
                
                EditorGUILayout.EndHorizontal(); //Buttons box

            }

            EditorGUILayout.Space();
            
            if (projectDetails == null) projectDetails = GetProjectDetails();

            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField("Project details", EditorStyles.boldLabel);
                if (GUILayout.Button("Refresh", EditorStyles.miniButton))
                {
                    projectDetails = GetProjectDetails();
                }
            }
            projectDetailsScrollPos = EditorGUILayout.BeginScrollView(projectDetailsScrollPos, EditorStyles.helpBox, GUILayout.Height(280f));
            EditorGUILayout.LabelField(projectDetails, UI.Styles.WordWrapLabel);
            EditorGUILayout.EndScrollView();

            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField("Supplying this information is always useful!", EditorStyles.miniLabel);
                
                if (GUILayout.Button(new GUIContent("  Copy to clipboard", EditorGUIUtility.IconContent("Clipboard").image)))
                {
                    EditorGUIUtility.systemCopyBuffer = projectDetails;
                    EditorApplication.Beep();
                }
            }
        }
        
        private static string GetProjectDetails()
        {
            StringBuilder stringBuilder = new StringBuilder();
            
            stringBuilder.AppendLine($"Version: v{AssetInfo.INSTALLED_VERSION}");
            stringBuilder.AppendLine($"Install location: {AssetInfo.GetRootFolder()}");
            stringBuilder.AppendLine($"Unity version: {AssetInfo.VersionChecking.GetUnityVersion()}");

            UnityEditor.PackageManager.PackageInfo urpPackage = AssetInfo.GetURPPackage();
            if (urpPackage != null)
            {
                stringBuilder.AppendLine($"URP version: {urpPackage.version}");
            }
            else
            {
                stringBuilder.AppendLine($"URP version: NONE");
            }
            
            stringBuilder.AppendLine($"");
            
            BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
            
            stringBuilder.AppendLine($"OS: {SystemInfo.operatingSystem}");
            stringBuilder.AppendLine($"Platform: {EditorUserBuildSettings.activeBuildTarget}");
            
            NamedBuildTarget buildTargetName = NamedBuildTarget.FromBuildTargetGroup(buildTargetGroup);
            string scriptingBackend = PlayerSettings.GetDefaultScriptingBackend(buildTargetName).ToString();
            stringBuilder.AppendLine($"Scripting backend: {scriptingBackend}");

            stringBuilder.AppendLine($"Color space: {PlayerSettings.colorSpace}");
            stringBuilder.AppendLine($"Graphics API(s): (Auto:{PlayerSettings.GetUseDefaultGraphicsAPIs(EditorUserBuildSettings.activeBuildTarget)}) {String.Join(" -> ", PlayerSettings.GetGraphicsAPIs(EditorUserBuildSettings.activeBuildTarget))}");
            stringBuilder.AppendLine($"Tessellation support: {SystemInfo.supportsTessellationShaders}");
            stringBuilder.AppendLine($"Compute shader support: {SystemInfo.supportsComputeShaders}");
            stringBuilder.AppendLine($"Async GPU readback support: {SystemInfo.supportsAsyncGPUReadback}");
            stringBuilder.AppendLine($"Async compute support: {SystemInfo.supportsAsyncCompute}");

            #if UNITY_6000_0_OR_NEWER && URP
            stringBuilder.AppendLine($"GPU Resident Drawer: {UniversalRenderPipeline.asset.gpuResidentDrawerMode != GPUResidentDrawerMode.Disabled}");
            #endif
            
            string VRProvider = "";
            #if XR && UNITY_2020_3_OR_NEWER
            EditorBuildSettings.TryGetConfigObject(UnityEngine.XR.Management.XRGeneralSettings.k_SettingsKey, out UnityEditor.XR.Management.XRGeneralSettingsPerBuildTarget buildTargetSettings);

            if (buildTargetSettings && buildTargetSettings.SettingsForBuildTarget(buildTargetGroup))
            {
                if (buildTargetSettings.SettingsForBuildTarget(buildTargetGroup).AssignedSettings.activeLoaders.Count > 0)
                {
                    for (int i = 0; i < buildTargetSettings.SettingsForBuildTarget(buildTargetGroup).AssignedSettings.activeLoaders.Count; i++)
                    {
                        VRProvider += $"{buildTargetSettings.SettingsForBuildTarget(buildTargetGroup).AssignedSettings.activeLoaders[i].name} + ";
                    }
                }
            }
            #endif
            stringBuilder.AppendLine($"Active VR plugin(s): {VRProvider}");
            
            stringBuilder.AppendLine($"");
            
            stringBuilder.AppendLine($"First available fog integration: {FogIntegration.GetFirstInstalled().name}");
            stringBuilder.AppendLine($"Underwater Rendering installed: {StylizedWaterEditor.UnderwaterRenderingInstalled()}");
            stringBuilder.AppendLine($"Dynamic Effects installed: {StylizedWaterEditor.DynamicEffectsInstalled()}");
            #if URP
            stringBuilder.AppendLine($"Render feature present: {(StylizedWaterRenderFeature.GetDefault() ? "True" : "False")}");
            #endif

            #if URP
            stringBuilder.AppendLine($"");
            
            stringBuilder.AppendLine($"Depth texture option disabled (anywhere): {PipelineUtilities.IsDepthTextureOptionDisabledAnywhere(out var _)}");
            stringBuilder.AppendLine($"Opaque texture option disabled (anywhere): {PipelineUtilities.IsOpaqueTextureOptionDisabledAnywhere(out var _)}");
            #endif
            
            stringBuilder.AppendLine($"");
            
            stringBuilder.AppendLine($"Async shader compilation: {EditorSettings.asyncShaderCompilation}");
            stringBuilder.AppendLine($"Caching Shader Preprocessor: {EditorSettings.cachingShaderPreprocessor}");
            stringBuilder.AppendLine($"Strict shader variant matching: {PlayerSettings.strictShaderVariantMatching}");

            stringBuilder.AppendLine($"");
            
            Shader defaultShader = ShaderConfigurator.GetDefaultShader();
            
            if(defaultShader == null)
            {
                stringBuilder.AppendLine($"[Failed to find default shader!]");
            }
            else
            {
                ShaderMessage[] shaderMessages = ShaderConfigurator.GetErrorMessages(defaultShader);
                
                stringBuilder.AppendLine($"Shader compile errors ({shaderMessages?.Length ?? 0}):");
                if (shaderMessages != null)
                {
                    foreach (var t in shaderMessages)
                    {
                        stringBuilder.AppendLine($"• {t.message} {t.file}:{t.line} ({t.platform})");
                    }
                }             
            }
            
            ConsoleWindowUtility.GetConsoleLogCounts(out var errors, out var _, out var _);
            stringBuilder.AppendLine($"Scripting compile errors ({errors}):");

            return stringBuilder.ToString();
        }
        
        private class Changelog
        {
            private GUIStyle changelogStyle;
            private string changelogContent;
            private Vector2 scrollPos;

            public float maxHeight = 500f;
            
            public Changelog()
            {
                string changelogPath = AssetDatabase.GUIDToAssetPath("793931a466dcf8743a80dc24cd9f9a56");
                TextAsset textAsset = ((TextAsset)AssetDatabase.LoadAssetAtPath(changelogPath, typeof(TextAsset)));

                if (textAsset == null) return;

                changelogContent = textAsset.text;

                //Format version header
                changelogContent = Regex.Replace(changelogContent, @"^[0-9].*", "<size=18><b>Version $0</b></size>", RegexOptions.Multiline);
                //Format headers
                changelogContent = Regex.Replace(changelogContent, @"(\w*What's New?|Added:|Changed:|Fixed:|Removed:\w*)", "<size=14><b>$0</b></size>", RegexOptions.Multiline);
                //Indent items with dashes
                changelogContent = Regex.Replace(changelogContent, @"^-.*", "  $0", RegexOptions.Multiline);
                
                changelogStyle = new GUIStyle(GUI.skin.label);
                changelogStyle.fontSize = 12;
                changelogStyle.alignment = TextAnchor.UpperLeft;
                changelogStyle.richText = true;
                changelogStyle.wordWrap = true;
            }

            public void Draw()
            {
                if (changelogContent == null)
                {
                    UI.DrawNotification("Changelog text file could not be found. It probably wasn't imported from the asset store...", MessageType.Error);
                    return;
                }
                
                scrollPos = GUILayout.BeginScrollView(scrollPos, EditorStyles.textArea, GUILayout.MaxHeight(maxHeight));
                GUILayout.TextArea(changelogContent, changelogStyle);
                GUILayout.EndScrollView();
            }
        }
    }
}