using System;
using Autodesk.Navisworks.Api;

namespace NavisworksTransport.Utils
{
    /// <summary>
    /// Navisworks单位转换工具类
    /// 提供统一的单位转换功能，避免代码重复
    /// </summary>
    public static class UnitsConverter
    {
        /// <summary>
        /// 获取Navisworks文档单位转换为米的系数
        /// </summary>
        /// <returns>转换系数（文档单位转换为米）</returns>
        public static double GetUnitsToMetersConversionFactor()
        {
            try
            {
                var units = Application.ActiveDocument.Units;
                return GetUnitsToMetersConversionFactor(units);
            }
            catch (Exception)
            {
                // 默认为米
                return 1.0;
            }
        }

        /// <summary>
        /// 获取指定单位转换为米的系数
        /// </summary>
        /// <param name="units">单位类型</param>
        /// <returns>转换系数（指定单位转换为米）</returns>
        public static double GetUnitsToMetersConversionFactor(Units units)
        {
            switch (units)
            {
                case Units.Millimeters:
                    return 0.001;
                case Units.Centimeters:
                    return 0.01;
                case Units.Meters:
                    return 1.0;
                case Units.Inches:
                    return 0.0254;
                case Units.Feet:
                    return 0.3048;
                case Units.Kilometers:
                    return 1000.0;
                case Units.Micrometers:
                    return 0.000001;
                case Units.Microinches:
                    return 0.0000000254;
                case Units.Mils:
                    return 0.0000254;
                case Units.Yards:
                    return 0.9144;
                case Units.Miles:
                    return 1609.43;
                default:
                    return 1.0;
            }
        }

        /// <summary>
        /// 获取米转换为Navisworks文档单位的系数
        /// </summary>
        /// <returns>转换系数（米转换为文档单位）</returns>
        public static double GetMetersToUnitsConversionFactor()
        {
            try
            {
                var units = Application.ActiveDocument.Units;
                return GetMetersToUnitsConversionFactor(units);
            }
            catch (Exception)
            {
                // 默认为米
                return 1.0;
            }
        }

        /// <summary>
        /// 获取米转换为指定单位的系数
        /// </summary>
        /// <param name="units">单位类型</param>
        /// <returns>转换系数（米转换为指定单位）</returns>
        public static double GetMetersToUnitsConversionFactor(Units units)
        {
            switch (units)
            {
                case Units.Millimeters:
                    return 1000.0; // 1米 = 1000毫米
                case Units.Centimeters:
                    return 100.0; // 1米 = 100厘米
                case Units.Meters:
                    return 1.0; // 1米 = 1米
                case Units.Inches:
                    return 39.37; // 1米 = 39.37英寸
                case Units.Feet:
                    return 3.281; // 1米 = 3.281英尺
                case Units.Kilometers:
                    return 0.001; // 1米 = 0.001公里
                case Units.Micrometers:
                    return 1000000.0; // 1米 = 1000000微米
                case Units.Microinches:
                    return 39370078.74; // 1米 = 39370078.74微英寸
                case Units.Mils:
                    return 39370.08; // 1米 = 39370.08密尔
                case Units.Yards:
                    return 1.094; // 1米 = 1.094码
                case Units.Miles:
                    return 0.000621; // 1米 = 0.000621英里
                default:
                    return 1.0;
            }
        }

        /// <summary>
        /// 将距离从文档单位转换为米
        /// </summary>
        /// <param name="distance">距离（文档单位）</param>
        /// <returns>距离（米）</returns>
        public static double ConvertToMeters(double distance)
        {
            return distance * GetUnitsToMetersConversionFactor();
        }

        /// <summary>
        /// 将距离从米转换为文档单位
        /// </summary>
        /// <param name="distanceInMeters">距离（米）</param>
        /// <returns>距离（文档单位）</returns>
        public static double ConvertFromMeters(double distanceInMeters)
        {
            return distanceInMeters * GetMetersToUnitsConversionFactor();
        }
    }
}