using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Autodesk.Navisworks.Api;

namespace NavisworksTransport.Utils.CoordinateSystem
{
    /// <summary>
    /// 当前文档级的 fragment 默认 Up 运行时状态。
    /// 自动从 CoordinateSystemManager 派生，不暴露独立设置。
    /// </summary>
    internal static class FragmentDefaultUpContext
    {
        private static readonly ConcurrentDictionary<int, string> RuntimeDocumentKeys =
            new ConcurrentDictionary<int, string>();

        public static string GetCurrentDocumentKey(Document doc = null)
        {
            doc = doc ?? Autodesk.Navisworks.Api.Application.ActiveDocument;
            if (doc == null || doc.IsClear)
            {
                return string.Empty;
            }

            int runtimeDocumentId = RuntimeHelpers.GetHashCode(doc);
            return GetOrCreateDocumentKey(runtimeDocumentId, doc.FileName, doc.Title);
        }

        internal static string GetOrCreateDocumentKey(int runtimeDocumentId, string fileName, string title)
        {
            if (runtimeDocumentId <= 0)
            {
                return string.Empty;
            }

            if (RuntimeDocumentKeys.TryGetValue(runtimeDocumentId, out string existingKey) &&
                !string.IsNullOrWhiteSpace(existingKey))
            {
                return existingKey;
            }

            string resolvedKey;
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                resolvedKey = $"file:{fileName}";
            }
            else if (!string.IsNullOrWhiteSpace(title))
            {
                resolvedKey = $"runtime:{runtimeDocumentId}|title:{title}";
            }
            else
            {
                resolvedKey = $"runtime:{runtimeDocumentId}";
            }

            RuntimeDocumentKeys[runtimeDocumentId] = resolvedKey;
            return resolvedKey;
        }

        public static string GetCurrentHostUpAxis(Document doc = null)
        {
            doc = doc ?? Autodesk.Navisworks.Api.Application.ActiveDocument;
            if (doc != null && !doc.IsClear)
            {
                UnitVector3D worldUp = doc.CurrentViewpoint.Value.WorldUpVector;
                if (!worldUp.IsZero)
                {
                    return Math.Abs(worldUp.Y) >= Math.Abs(worldUp.Z) ? "Y" : "Z";
                }

                Vector3D documentUp = doc.UpVector;
                if (!documentUp.IsZero)
                {
                    return Math.Abs(documentUp.Y) >= Math.Abs(documentUp.Z) ? "Y" : "Z";
                }
            }

            return "Z";
        }

        /// <summary>
        /// 获取当前文档的 Fragment Default Up 轴。
        /// 自动从 CoordinateSystemManager 的 ResolvedType 派生。
        /// YUp → "Y", ZUp → "Z"。
        /// </summary>
        public static string GetCurrentDocumentFragmentDefaultUpAxis(Document doc = null)
        {
            return CoordinateSystemManager.Instance.ResolvedType == CoordinateSystemType.YUp ? "Y" : "Z";
        }

        public static bool IsSupportedAxis(string axisName)
        {
            return string.Equals(axisName, "Y", StringComparison.OrdinalIgnoreCase) ||
                   string.Equals(axisName, "Z", StringComparison.OrdinalIgnoreCase);
        }

        internal static void ResetForTests()
        {
            RuntimeDocumentKeys.Clear();
        }
    }
}
