// Asset Naming Manager — finds its own folder, wherever you dragged it.
using UnityEditor;

namespace AssetNamingManager
{
    /// <summary>
    /// Resolves the package folder at runtime instead of hard-coding it, so the tool keeps working
    /// after you move <c>AssetNamingManager</c> somewhere else in the project. Asset Store reviewers
    /// check for this; more importantly, buyers really do move folders.
    /// </summary>
    public static class NamingManagerPaths
    {
        const string Anchor = "/Editor/Core/NamingManagerPaths.cs";
        const string Fallback = "Assets/AssetNamingManager";

        static string _root;

        /// <summary>The package root, e.g. <c>Assets/AssetNamingManager</c>. No trailing slash.</summary>
        public static string PackageRoot
        {
            get
            {
                if (!string.IsNullOrEmpty(_root)) return _root;

                foreach (var guid in AssetDatabase.FindAssets("NamingManagerPaths t:MonoScript"))
                {
                    string path = AssetDatabase.GUIDToAssetPath(guid);
                    if (path != null && path.EndsWith(Anchor, System.StringComparison.Ordinal))
                    {
                        _root = path.Substring(0, path.Length - Anchor.Length);
                        return _root;
                    }
                }

                _root = Fallback;
                return _root;
            }
        }

        /// <summary>The Editor folder inside the package.</summary>
        public static string EditorRoot
        {
            get { return PackageRoot + "/Editor"; }
        }

        /// <summary>Path of the window stylesheet.</summary>
        public static string StyleSheetPath
        {
            get { return EditorRoot + "/UI/Styles/NamingManager.uss"; }
        }

        /// <summary>Path of the bundled documentation folder.</summary>
        public static string DocumentationRoot
        {
            get { return PackageRoot + "/Documentation"; }
        }

        /// <summary>Path of the demo content shipped with the package.</summary>
        public static string DemoRoot
        {
            get { return PackageRoot + "/Demo"; }
        }

        /// <summary>Clears the cached lookup. Called when assets move.</summary>
        public static void Invalidate()
        {
            _root = null;
        }

        // With Fast Enter Play Mode - the default for new Unity 6.6 projects - the domain is
        // not reloaded, so statics survive between Play sessions. Clear the cache here so a
        // session never starts holding something the previous one left behind.
        [InitializeOnEnterPlayMode]
        static void ResetOnEnterPlayMode()
        {
            Invalidate();
        }
    }
}
