using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;

namespace NavisworksTransport.UnitTests.Integration
{
    /// <summary>
    /// 动画播放控制集成测试（覆盖计划 T13）。
    ///
    /// 验证 PathAnimationManager 播放链路完整：
    /// - 播放开始（playing 阶段帧前进）
    /// - 暂停生效（paused 阶段帧停止，pauseVerified=true）
    /// - 恢复播放（resumed 阶段帧继续前进）
    /// - 播放完成（finished 阶段状态 Finished）
    /// </summary>
    [TestClass]
    [TestCategory("NavisworksIntegration")]
    public class AnimationPlaybackControlAutomationTests
    {
        private const int TestFrameRate = 15;
        private const double TestDurationSeconds = 5.0;

        [TestMethod]
        [Timeout(240000)]
        public async Task GroundVirtualObject_PlaybackControl_PauseResumeWork()
        {
            using (var client = new NavisworksTestAutomationClient())
            {
                await client.EnsureServiceReadyAsync(TimeSpan.FromSeconds(90)).ConfigureAwait(false);

                JObject response = await client.AnimationPlaybackControlAsync(
                    "Ground",
                    180,
                    TestFrameRate,
                    TestDurationSeconds).ConfigureAwait(false);
                Assert.IsTrue(response.Value<bool>("ok"), "播放控制序列失败: " + (string)response["error"]);

                JObject data = (JObject)response["data"];

                // 动画参数
                Assert.IsTrue((int)data["totalFrames"] > 0, "总帧数应大于 0");
                Assert.AreEqual("自动测试_Ground", (string)data["route"]["name"], "播放控制路径不匹配");

                // 暂停验证：暂停期间帧数不变
                Assert.IsTrue((bool)data["pauseVerified"], "暂停期间帧数仍在前进");
                Assert.AreEqual((int)data["pausedFrame"], (int)data["frameAfterPause"], "暂停后帧数不应变化");

                // 各阶段状态
                JArray stages = (JArray)data["stages"];
                Assert.IsNotNull(stages, "缺少 stages");
                Assert.AreEqual(4, stages.Count, "阶段数应为 4（playing/paused/resumed/finished）");

                Assert.AreEqual("playing", (string)stages[0]["stage"]);
                Assert.AreEqual("paused", (string)stages[1]["stage"]);
                Assert.AreEqual("resumed", (string)stages[2]["stage"]);
                Assert.AreEqual("finished", (string)stages[3]["stage"]);

                // 帧前进：playing < paused < resumed < finished
                int playingFrame = (int)stages[0]["frame"];
                int pausedFrame = (int)stages[1]["frame"];
                int resumedFrame = (int)stages[2]["frame"];
                int finishedFrame = (int)stages[3]["frame"];
                Assert.IsTrue(playingFrame > 0, "播放开始后帧数应大于 0");
                Assert.IsTrue(pausedFrame > playingFrame, "暂停帧应晚于播放开始帧");
                Assert.IsTrue(resumedFrame > pausedFrame, "恢复后帧数应前进");
                Assert.IsTrue(finishedFrame >= resumedFrame, "完成帧不应早于恢复帧");

                // 完成状态
                Assert.AreEqual("Finished", (string)stages[3]["state"], "动画最终状态应为 Finished");
            }
        }
    }
}
