using System;
using System.Numerics;

namespace NavisworksTransport.Utils.CoordinateSystem
{
    /// <summary>
    /// 自动路径规划 / 网格系统使用的宿主平面与高程语义辅助。
    /// 
    /// 关键约束：
    /// - 对外继续维持 ICoordinateSystem 的历史契约：
    ///   - ZUp: 水平面 = Host XY，高程 = Host Z
    ///   - YUp: 水平面 = Host XZ，高程 = Host Y
    /// - 对内通过 HostCoordinateAdapter 统一解释“哪根轴是宿主 up”。
    /// 
    /// 这样自动路径规划既不会继续把世界 Z 硬编码成高度轴，
    /// 也不会把旧的水平坐标 h1/h2 语义误替换成 raw canonical XY。
    /// </summary>
    public static class HostPlanarGridHelper
    {
        public static double GetElevation3(Vector3 hostPoint, HostCoordinateAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return adapter.ToCanonicalPoint3(hostPoint).Z;
        }

        public static Vector3 SetElevation3(Vector3 hostPoint, double elevation, HostCoordinateAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            Vector3 canonicalPoint = adapter.ToCanonicalPoint3(hostPoint);
            Vector3 canonicalResult = new Vector3(canonicalPoint.X, canonicalPoint.Y, (float)elevation);
            return adapter.FromCanonicalPoint3(canonicalResult);
        }

        public static (double h1, double h2) GetHorizontalCoords3(Vector3 hostPoint, HostCoordinateAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            switch (adapter.HostType)
            {
                case CoordinateSystemType.ZUp:
                    return (hostPoint.X, hostPoint.Y);
                case CoordinateSystemType.YUp:
                    return (hostPoint.X, hostPoint.Z);
                default:
                    throw new InvalidOperationException($"不支持的宿主坐标系: {adapter.HostType}");
            }
        }

        public static Vector3 CreateHostPoint3(double h1, double h2, double elevation, HostCoordinateAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            switch (adapter.HostType)
            {
                case CoordinateSystemType.ZUp:
                    return new Vector3((float)h1, (float)h2, (float)elevation);
                case CoordinateSystemType.YUp:
                    return new Vector3((float)h1, (float)elevation, (float)h2);
                default:
                    throw new InvalidOperationException($"不支持的宿主坐标系: {adapter.HostType}");
            }
        }

        public static (double min1, double max1, double min2, double max2) GetHorizontalRange3(
            Vector3 hostMin,
            Vector3 hostMax,
            HostCoordinateAdapter adapter)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            switch (adapter.HostType)
            {
                case CoordinateSystemType.ZUp:
                    return (hostMin.X, hostMax.X, hostMin.Y, hostMax.Y);
                case CoordinateSystemType.YUp:
                    return (hostMin.X, hostMax.X, hostMin.Z, hostMax.Z);
                default:
                    throw new InvalidOperationException($"不支持的宿主坐标系: {adapter.HostType}");
            }
        }
    }
}
