using System;
using System.Collections.Generic;
using Autodesk.Navisworks.Api;
using NavisworksTransport.Core.Animation;

namespace NavisworksTransport.Core.Collision
{
    /// <summary>
    /// 批处理碰撞检测处理器实现
    /// 纯计算接口实现，复用现有的PathAnimationManager和ClashDetectiveIntegration
    /// </summary>
    public class BatchCollisionProcessor : IBatchCollisionProcessor
    {
        private PathAnimationManager _animationManager;
        private ClashDetectiveIntegration _clashIntegration;

        public BatchCollisionProcessor()
        {
            _animationManager = PathAnimationManager.GetInstance();
            _clashIntegration = ClashDetectiveIntegration.Instance;
        }

        /// <summary>
        /// 预计算动画帧和碰撞（纯计算，无UI操作）
        /// </summary>
        public List<AnimationFrame> PrecomputeFrames(
            PathRoute route,
            ModelItem animatedObject,
            bool isVirtualObject,
            double virtualObjectLength,
            double virtualObjectWidth,
            double virtualObjectHeight,
            int frameRate,
            double duration,
            double detectionGap,
            List<ModelItem> manualDetectionTargets = null,
            double objectRotationCorrection = 0.0,
            List<ModelItem> excludedObjects = null)
        {
            try
            {
                LogManager.Info($"[批处理] 开始预计算动画帧 - 路径: {route.Name}, 帧率: {frameRate}, 时长: {duration}秒, 角度修正: {objectRotationCorrection:F1}°");

                // 设置动画管理器参数（但不触发UI操作）
                _animationManager.SetRoute(route);
                _animationManager.SetAnimatedObject(animatedObject);
                _animationManager.SetVirtualObjectParameters(isVirtualObject, virtualObjectLength, virtualObjectWidth, virtualObjectHeight);
                _animationManager.SetAnimationParameters(frameRate, duration, detectionGap);
                _animationManager.SetObjectRotationCorrectionDirect(objectRotationCorrection);

                if (manualDetectionTargets != null && manualDetectionTargets.Count > 0)
                {
                    _animationManager.SetManualCollisionTargets(manualDetectionTargets);
                }

                // 设置排除列表（关键：批处理时使用排除列表）
                if (excludedObjects != null && excludedObjects.Count > 0)
                {
                    _animationManager.SetExcludedObjectsAndClearCache(excludedObjects);
                    LogManager.Info($"[批处理] 已设置排除列表，共 {excludedObjects.Count} 个物体");
                }

                // 调用预计算方法（内部不涉及UI操作）
                var frames = _animationManager.PrecomputeAnimationFramesInternal();

                LogManager.Info($"[批处理] 预计算完成 - 生成 {frames.Count} 帧");

                return frames;
            }
            catch (Exception ex)
            {
                LogManager.Error($"[批处理] 预计算动画帧失败: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 创建并运行ClashDetective测试（纯计算，无UI操作）
        /// </summary>
        public string CreateAndRunClashDetectiveTest(
            List<CollisionResult> precomputedCollisions,
            double detectionGap,
            string pathName,
            string routeId,
            ModelItem animatedObject,
            bool isVirtualObject,
            int frameRate,
            double duration,
            double virtualObjectLength,
            double virtualObjectWidth,
            double virtualObjectHeight,
            List<Point3D> pathPoints = null,
            int? detectionRecordId = null)
        {
            try
            {
                LogManager.Info($"[批处理] 开始创建ClashDetective测试 - 路径: {pathName}");

                // 调用新的批处理专用方法（不触发UI操作）
                var testName = _clashIntegration.CreateAllAnimationCollisionTestsForBatch(
                    precomputedCollisions,
                    detectionGap,
                    routeId,
                    animatedObject,
                    isVirtualObject,
                    frameRate,
                    duration,
                    virtualObjectLength,
                    virtualObjectWidth,
                    virtualObjectHeight,
                    pathPoints,
                    detectionRecordId
                );

                LogManager.Info($"[批处理] ClashDetective测试创建完成 - 测试名称: {testName}");

                return testName;
            }
            catch (Exception ex)
            {
                LogManager.Error($"[批处理] 创建ClashDetective测试失败: {ex.Message}");
                throw;
            }
        }
    }
}