using System.Linq;
using TrajectoryPlanningVisualization;

namespace TrajectoryPlanningVisualizationVerificationHost;

internal static class WebAssetChecks
{
    public static void Run()
    {
        BuildsDeterministicSmokeSnapshots();

        string html = EmbeddedWebAssets.ReadText("index.html");
        string css = EmbeddedWebAssets.ReadText("app.css");
        string js = EmbeddedWebAssets.ReadText("app.js");

        Verification.True(html.Contains("路径总览"), "overview Chinese title");
        Verification.True(html.Contains("LS / ST"), "LS/ST tab");
        Verification.True(html.Contains("曲率与运动学"), "kinematics tab");
        Verification.True(html.Contains("周期历史"), "history tab");
        Verification.True(html.Contains("生效配置"), "configuration panel");
        Verification.True(css.Contains("--current-trajectory: #1769aa"), "scientific current color");
        Verification.True(css.Contains("stroke-width: 1.1"), "thin scientific line");
        Verification.True(css.Contains("main > section[hidden] { display: none !important; }"),
            "hidden sections cannot be overridden by grid rules");
        Verification.True(css.Contains(".axis-tick-y"), "SVG y ticks have a semantic class");
        Verification.True(css.Contains(".axis-label-y"), "SVG y labels have a semantic class");
        Verification.True(css.Contains(".world-empty-state"), "empty overview has a semantic state");
        Verification.True(css.Contains(".world-coarse"), "coarse path semantic class");
        Verification.True(css.Contains(".world-local-g2"), "Local G2 semantic class");
        Verification.True(css.Contains(".world-segment-active"), "active direction semantic class");
        Verification.True(css.Contains(".world-current"), "current trajectory semantic class");
        Verification.True(css.Contains(".marker-vehicle"), "vehicle marker semantic class");
        Verification.True(css.Contains(".marker-plan-start"), "plan start marker semantic class");
        Verification.True(css.Contains(".marker-gear-switch"), "gear-switch marker semantic class");
        Verification.True(css.Contains(".marker-final-goal"), "final goal marker semantic class");
        Verification.True(js.Contains("末点后无时间区间"), "jerk terminal explanation");
        Verification.True(js.Contains("__TRAJECTORY_VISUALIZATION_TEST__"), "test hook flag is explicit");
        Verification.True(js.Contains("Object.freeze"), "test hooks are frozen");
        Verification.True(js.Contains("未收到 Local G2 路径"), "empty overview names the missing source");
        Verification.True(html.Contains("occupancy-grid"), "single occupancy canvas exists");
        Verification.True(html.Contains("world-overlay"), "SVG trajectory overlay exists");
        Verification.True(js.Contains("atob"), "compact occupancy bitset is decoded in browser");
        Verification.True(!html.Contains("http://") && !html.Contains("https://"), "page has no CDN URL");
    }

    private static void BuildsDeterministicSmokeSnapshots()
    {
        PlanningVisualizationStaticSnapshot staticSnapshot = SampleSnapshotFactory.CreateStaticSnapshot();
        PlanningVisualizationDynamicSnapshot dynamicSnapshot = SampleSnapshotFactory.CreateDynamicSnapshot(1L);

        Verification.Equal(2, staticSnapshot.DirectionSegments.Count, "smoke snapshot has two direction segments");
        Verification.True(staticSnapshot.StaticMarkers.Any(marker => marker.Kind == "gear-switch"), "smoke snapshot has a gear marker");
        Verification.True(staticSnapshot.ConfigurationGroups.Count > 0, "smoke snapshot has effective configuration");
        foreach (string chartId in new[] { "ls", "st", "curvature-s", "curvature-t", "velocity-t", "acceleration-t", "jerk-t", "yaw-rate-t" })
        {
            Verification.True(dynamicSnapshot.Charts.Any(chart => chart.Id == chartId), "smoke snapshot includes chart " + chartId);
        }
    }
}
