namespace NavisworksTransport.Core.Models
{
    /// <summary>
    /// 碰撞检测配置
    /// </summary>
    public class CollisionDetectionConfig
    {
        /// <summary>
        /// 碰撞检测容差（模型单位，内部使用）
        /// </summary>
        public double DetectionTolerance { get; set; }
        public int FrameRate { get; set; }
        public double DurationSeconds { get; set; }
        public bool CollisionDetectionEnabled { get; set; }
        public bool ReportGenerationEnabled { get; set; }
        
        // 虚拟物体参数
        public double VirtualObjectLength { get; set; }
        public double VirtualObjectWidth { get; set; }
        public double VirtualObjectHeight { get; set; }

        /// <summary>
        /// 序列化为JSON字符串（简化版）
        /// 注意：JSON中容差以米为单位存储（用户可读），内部转换为模型单位
        /// </summary>
        public string ToJson()
        {
            double toleranceMeters = DetectionTolerance * Utils.UnitsConverter.GetUnitsToMetersConversionFactor();
            return $"{{\"DetectionToleranceMeters\":{toleranceMeters},\"FrameRate\":{FrameRate},\"DurationSeconds\":{DurationSeconds},\"CollisionDetectionEnabled\":{CollisionDetectionEnabled.ToString().ToLower()},\"ReportGenerationEnabled\":{ReportGenerationEnabled.ToString().ToLower()}}}";
        }

        /// <summary>
        /// 从JSON字符串反序列化（简化版）
        /// </summary>
        public static CollisionDetectionConfig FromJson(string json)
        {
            if (string.IsNullOrEmpty(json))
                return new CollisionDetectionConfig();

            try
            {
                var config = new CollisionDetectionConfig();
                
                // 简化的JSON解析
                var parts = json.Replace("{", "").Replace("}", "").Replace("\"", "").Split(',');
                foreach (var part in parts)
                {
                    var keyValue = part.Split(':');
                    if (keyValue.Length == 2)
                    {
                        var key = keyValue[0].Trim();
                        var value = keyValue[1].Trim();
                        
                        switch (key)
                        {
                            case "DetectionToleranceMeters":
                                if (double.TryParse(value, out var detectionToleranceMeters))
                                    config.DetectionTolerance = detectionToleranceMeters * Utils.UnitsConverter.GetMetersToUnitsConversionFactor();
                                break;
                            case "FrameRate":
                                if (int.TryParse(value, out var frameRate))
                                    config.FrameRate = frameRate;
                                break;
                            case "DurationSeconds":
                                if (double.TryParse(value, out var duration))
                                    config.DurationSeconds = duration;
                                break;
                            case "CollisionDetectionEnabled":
                                if (bool.TryParse(value, out var collisionEnabled))
                                    config.CollisionDetectionEnabled = collisionEnabled;
                                break;
                            case "ReportGenerationEnabled":
                                if (bool.TryParse(value, out var reportEnabled))
                                    config.ReportGenerationEnabled = reportEnabled;
                                break;
                        }
                    }
                }
                
                return config;
            }
            catch
            {
                return new CollisionDetectionConfig();
            }
        }

        /// <summary>
        /// 合并配置（覆盖配置优先）
        /// </summary>
        public CollisionDetectionConfig MergeWith(CollisionDetectionConfig overrideConfig)
        {
            if (overrideConfig == null) return this;
            return new CollisionDetectionConfig
            {
                DetectionTolerance = overrideConfig.DetectionTolerance > 0
                    ? overrideConfig.DetectionTolerance : DetectionTolerance,
                FrameRate = overrideConfig.FrameRate > 0
                    ? overrideConfig.FrameRate : FrameRate,
                DurationSeconds = overrideConfig.DurationSeconds > 0
                    ? overrideConfig.DurationSeconds : DurationSeconds,
                CollisionDetectionEnabled = overrideConfig.CollisionDetectionEnabled,
                ReportGenerationEnabled = overrideConfig.ReportGenerationEnabled
            };
        }
    }
}