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

namespace NavisworksTransport.UnitTests.Integration
{
    [TestClass]
    [TestCategory("NavisworksIntegration")]
    [TestCategory("NavisworksIntegration.Ground")]
    public class AutoPathGridGenerationAutomationTests
    {
        private const string BaselineRouteName = "测试基准";
        private const double BaselineObjectLengthInMeters = 0.4;
        private const double BaselineObjectWidthInMeters = 0.4;
        private const double BaselineSafetyMarginInMeters = 0.05;
        private const double BaselineGridSizeInMeters = 0.3;

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

                JObject response = await client.RunAutoPathAsync(
                    "Ground",
                    BaselineRouteName,
                    BaselineObjectLengthInMeters,
                    BaselineObjectWidthInMeters,
                    BaselineSafetyMarginInMeters,
                    BaselineGridSizeInMeters).ConfigureAwait(false);
                Assert.IsTrue(response.Value<bool>("ok"), "测试 HTTP 接口返回失败: " + (string)response["error"]);

                JObject data = (JObject)response["data"];
                Assert.IsNotNull(data, "缺少测试结果 data");

                JObject sourceRoute = (JObject)data["sourceRoute"];
                Assert.IsNotNull(sourceRoute, "缺少 sourceRoute");
                Assert.AreEqual(BaselineRouteName, (string)sourceRoute["name"], "自动路径测试必须使用精确命名的测试基准路径作为起终点来源");
                Assert.AreEqual("Ground", (string)sourceRoute["pathType"], "源路径类型不匹配");

                JObject generatedRoute = (JObject)data["generatedRoute"];
                Assert.IsNotNull(generatedRoute, "缺少 generatedRoute");
                Assert.AreEqual("Ground", (string)generatedRoute["pathType"], "生成路径类型不匹配");
                Assert.IsTrue((int)generatedRoute["pointCount"] >= 2, "生成路径至少应包含起点和终点");
                Assert.IsTrue((double)generatedRoute["length"] > 0, "生成路径长度必须大于 0");

                JObject planningParameters = (JObject)data["planningParameters"];
                Assert.IsNotNull(planningParameters, "缺少 planningParameters");

                Assert.AreEqual(BaselineObjectLengthInMeters, (double)planningParameters["objectLengthInMeters"], 1e-9, "物体长度参数不匹配");
                Assert.AreEqual(BaselineObjectWidthInMeters, (double)planningParameters["objectWidthInMeters"], 1e-9, "物体宽度参数不匹配");
                Assert.AreEqual(BaselineSafetyMarginInMeters, (double)planningParameters["safetyMarginInMeters"], 1e-9, "安全间隙参数不匹配");
                Assert.AreEqual(BaselineGridSizeInMeters, (double)planningParameters["gridSizeInMeters"], 1e-9, "网格大小参数不匹配");

                JObject segmentValidation = (JObject)data["segmentValidation"];
                Assert.IsNotNull(segmentValidation, "缺少 segmentValidation");
                Assert.AreEqual(0, (int)segmentValidation["blockedSampleCount"], "生成路径存在穿过不可通行网格的采样点");
                Assert.AreEqual(0, (int)segmentValidation["blockedCellInteriorIntersectionCount"], "生成路径线段穿过了不可通行网格的实际归属区域");
                Assert.AreEqual(0, (int)segmentValidation["invalidSampleCount"], "生成路径存在落在网格外的采样点");
            }
        }
    }
}
