using System;
using System.Threading;
using System.Threading.Tasks;

namespace NavisworksTransport.Commands
{
    /// <summary>
    /// 启动动画参数类
    /// </summary>
    public class StartAnimationParameters
    {
        /// <summary>
        /// 要播放动画的路径
        /// </summary>
        public PathRoute TargetRoute { get; set; }

        /// <summary>
        /// 动画速度（米/秒）
        /// </summary>
        public double AnimationSpeed { get; set; } = 1.0;

        /// <summary>
        /// 是否循环播放
        /// </summary>
        public bool Loop { get; set; } = false;

        /// <summary>
        /// 是否自动启动
        /// </summary>
        public bool AutoStart { get; set; } = true;

        public PathPlanningResult ValidateParameters()
        {
            if (TargetRoute == null)
                return PathPlanningResult.ValidationFailure("必须指定目标路径");
            
            if (AnimationSpeed <= 0)
                return PathPlanningResult.ValidationFailure("动画速度必须大于0");

            if (TargetRoute.Points.Count < 2)
                return PathPlanningResult.ValidationFailure("路径必须包含至少2个点");

            return PathPlanningResult.Success("参数验证通过");
        }
    }

    /// <summary>
    /// 动画启动结果类
    /// </summary>
    public class StartAnimationResult
    {
        public string AnimationId { get; set; }
        public double TotalDuration { get; set; }
        public int PathPointCount { get; set; }
        public long ExecutionTimeMs { get; set; }
        public bool IsStarted { get; set; }
    }

    /// <summary>
    /// 启动动画Command
    /// </summary>
    public class StartAnimationCommand : CommandBase
    {
        private readonly StartAnimationParameters _parameters;

        public StartAnimationCommand(StartAnimationParameters parameters)
            : base("StartAnimation", "启动动画", "启动路径动画播放")
        {
            _parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
        }

        protected override PathPlanningResult ValidateParameters()
        {
            return _parameters.ValidateParameters();
        }

        protected override async Task<PathPlanningResult> ExecuteInternalAsync(CancellationToken cancellationToken)
        {
            var startTime = DateTime.UtcNow;
            
            UpdateProgress(10, "初始化动画系统...");
            
            var result = new StartAnimationResult
            {
                PathPointCount = _parameters.TargetRoute.Points.Count
            };
            
            await Task.Run(() =>
            {
                UpdateProgress(30, "准备动画路径...");
                
                // 简化实现：基本的动画启动逻辑
                result.AnimationId = Guid.NewGuid().ToString();
                result.TotalDuration = _parameters.TargetRoute.TotalLength / _parameters.AnimationSpeed;
                
                UpdateProgress(60, "启动动画播放...");

                // 简化实现：实际的动画播放由AnimationControlViewModel中的PathAnimationManager处理
                result.IsStarted = true;
                
                UpdateProgress(90, "完成动画启动...");
            }, cancellationToken);
            
            result.ExecutionTimeMs = (long)(DateTime.UtcNow - startTime).TotalMilliseconds;
            
            UpdateProgress(100, "动画启动完成");
            
            return PathPlanningResult<StartAnimationResult>.Success(result, 
                $"动画启动成功：路径 {_parameters.TargetRoute.Name}，预计时长 {result.TotalDuration:F1} 秒");
        }

        public static StartAnimationCommand CreateForRoute(PathRoute route, double speed = 1.0, bool loop = false)
        {
            return new StartAnimationCommand(new StartAnimationParameters
            {
                TargetRoute = route,
                AnimationSpeed = speed,
                Loop = loop,
                AutoStart = true
            });
        }
    }
}