using System.IO;
using Xunit;

namespace TeslaConsole.DiffTests
{
    /// <summary>
    /// Characterization tests for the data-driven product catalog (AppRegistry + RedPlanet\Apps.xml).
    /// These assert the catalog reproduces the EXACT LaunchData the previously-hardcoded
    /// SiteManagement code produced, so the refactor is behavior-preserving. Catalog logic is new
    /// (absent from the original exe), so these run against the recovered build only.
    /// </summary>
    public class CatalogTests : IClassFixture<DifferentialFixture>
    {
        private readonly DifferentialFixture _fx;
        private readonly string _catalog;

        public CatalogTests(DifferentialFixture fx)
        {
            _fx = fx;
            _catalog = AssemblyPaths.RecoveredCatalog;
            Assert.True(File.Exists(_catalog), "Catalog not found next to the build: " + _catalog);
        }

        private string Entry(string launchKey, string w = "", string h = "")
            => _fx.Recovered.Run("CatalogEntry", new[] { _catalog, launchKey, w, h });

        [Fact]
        public void Catalog_Has_Five_Products_And_Fourteen_Entries()
            => Assert.Equal("products=5;entries=14",
                _fx.Recovered.Run("CatalogSummary", new[] { _catalog }));

        [Fact]
        public void RioJoy_Matches_Expected()
            => Assert.Equal(
                @"RIOJoy|87fbc2e6-6359-4ef4-96a5-df157823cff6|C:\Games\RIOJoy\app\RioJoy.Tray.exe||C:\Games\RIOJoy\app|True",
                Entry("87FBC2E6-6359-4EF4-96A5-DF157823CFF6"));

        // Each expected string is "DisplayName|LaunchKey|Exe|Args|WorkingDirectory|AutoRestart"
        // and matches exactly what the old hardcoded PodInfo_InstallProductCompleted emitted.

        [Fact]
        public void RedPlanet_GameClient_Matches_Original()
            => Assert.Equal(
                @"Red Planet 4.11|7d241b1f-ab6d-4e08-9c20-12294e743d94|C:\Games\RP411\rpl4opt.exe|-net 1501|C:\Games\RP411|True",
                Entry("7D241B1F-AB6D-4e08-9C20-12294E743D94"));

        [Fact]
        public void RedPlanet_GameClient_With_Resolution_Matches_Original()
            => Assert.Equal(
                @"Red Planet 4.11|7d241b1f-ab6d-4e08-9c20-12294e743d94|C:\Games\RP411\rpl4opt.exe|-net 1501 -res 1024 768|C:\Games\RP411|True",
                Entry("7D241B1F-AB6D-4e08-9C20-12294E743D94", "1024", "768"));

        [Fact]
        public void RedPlanet_LiveCamera_Matches_Original()
            => Assert.Equal(
                @"Red Planet 4.11 LC|57a0b3c2-d5cf-46d6-abed-a8f4a26ab086|C:\Games\RP411\rpl4opt.exe|-net 1501 -lc|C:\Games\RP411|True",
                Entry("57A0B3C2-D5CF-46d6-ABED-A8F4A26AB086"));

        [Fact]
        public void RedPlanet_LiveCamera_With_Resolution_Matches_Original()
            => Assert.Equal(
                @"Red Planet 4.11 LC|57a0b3c2-d5cf-46d6-abed-a8f4a26ab086|C:\Games\RP411\rpl4opt.exe|-net 1501 -res 1024 768 -lc|C:\Games\RP411|True",
                Entry("57A0B3C2-D5CF-46d6-ABED-A8F4A26AB086", "1024", "768"));

        [Fact]
        public void RedPlanet_MissionReview_Matches_Original()
            => Assert.Equal(
                @"Red Planet 4.11 MR|8f71d5c2-38e4-413c-8e22-88cad08774d2|C:\Games\RP411\rpl4opt.exe|-net 1501 -mr|C:\Games\RP411|True",
                Entry("8F71D5C2-38E4-413c-8E22-88CAD08774D2"));

        [Fact]
        public void Firestorm_Matches_Original()
            => Assert.Equal(
                @"BattleTech Firestorm|cc8500ed-a653-45a7-bef8-c332d30371a6|C:\Games\MW4\launcher.exe||C:\Games\MW4|True",
                Entry("CC8500ED-A653-45a7-BEF8-C332D30371A6"));

        // BattleTech 4.11 is a new product (no original counterpart); these pin the
        // catalog entries the console registers on BT pods.

        [Fact]
        public void BattleTech_GameClient_Matches_Expected()
            => Assert.Equal(
                @"BattleTech 4.11|f4c957fd-72f7-4c5f-8971-28095007e8df|C:\Games\BT411\btl4.exe|-net 1501|C:\Games\BT411|True",
                Entry("F4C957FD-72F7-4C5F-8971-28095007E8DF"));

        [Fact]
        public void BattleTech_GameClient_With_Resolution_Matches_Expected()
            => Assert.Equal(
                @"BattleTech 4.11|f4c957fd-72f7-4c5f-8971-28095007e8df|C:\Games\BT411\btl4.exe|-net 1501 -res 1024 768|C:\Games\BT411|True",
                Entry("F4C957FD-72F7-4C5F-8971-28095007E8DF", "1024", "768"));

        [Fact]
        public void BattleTech_LiveCamera_Matches_Expected()
            => Assert.Equal(
                @"BattleTech 4.11 LC|f4c957fd-72f7-4c5f-8971-28095007e8d0|C:\Games\BT411\btl4.exe|-net 1501 -lc|C:\Games\BT411|True",
                Entry("F4C957FD-72F7-4C5F-8971-28095007E8D0"));

        [Fact]
        public void BattleTech_MissionReview_Matches_Expected()
            => Assert.Equal(
                @"BattleTech 4.11 MR|f4c957fd-72f7-4c5f-8971-28095007e8d1|C:\Games\BT411\btl4.exe|-net 1501 -mr|C:\Games\BT411|True",
                Entry("F4C957FD-72F7-4C5F-8971-28095007E8D1"));

        // TeslaRel410 — the DOSBox-X preservation pods. All six entries launch
        // pod-launch.exe; the mode arg ("bt"/"rp") selects the game, LC/MR boot
        // identically (the console assigns the role via the egg hostType), and
        // {res} is intentionally absent (output size is fixed per rig).

        [Fact]
        public void Rel410_BT_GameClient_Matches_Expected()
            => Assert.Equal(
                @"BT4.10|135019c7-2c2f-4c38-96be-c7db39994ab0|C:\Games\TeslaPod410\pod-launch.exe|bt|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB0"));

        [Fact]
        public void Rel410_BT_Resolution_Choice_Has_No_Effect()
            => Assert.Equal(
                @"BT4.10|135019c7-2c2f-4c38-96be-c7db39994ab0|C:\Games\TeslaPod410\pod-launch.exe|bt|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB0", "1024", "768"));

        [Fact]
        public void Rel410_BT_LiveCamera_Matches_Expected()
            => Assert.Equal(
                @"BT4.10 LC|135019c7-2c2f-4c38-96be-c7db39994ab1|C:\Games\TeslaPod410\pod-launch.exe|bt|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB1"));

        [Fact]
        public void Rel410_BT_MissionReview_Matches_Expected()
            => Assert.Equal(
                @"BT4.10 MR|135019c7-2c2f-4c38-96be-c7db39994ab2|C:\Games\TeslaPod410\pod-launch.exe|bt|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB2"));

        [Fact]
        public void Rel410_RP_GameClient_Matches_Expected()
            => Assert.Equal(
                @"RP4.10|135019c7-2c2f-4c38-96be-c7db39994ab3|C:\Games\TeslaPod410\pod-launch.exe|rp|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB3"));

        [Fact]
        public void Rel410_RP_LiveCamera_Matches_Expected()
            => Assert.Equal(
                @"RP4.10 LC|135019c7-2c2f-4c38-96be-c7db39994ab4|C:\Games\TeslaPod410\pod-launch.exe|rp|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB4"));

        [Fact]
        public void Rel410_RP_MissionReview_Matches_Expected()
            => Assert.Equal(
                @"RP4.10 MR|135019c7-2c2f-4c38-96be-c7db39994ab5|C:\Games\TeslaPod410\pod-launch.exe|rp|C:\Games\TeslaPod410|True",
                Entry("135019C7-2C2F-4C38-96BE-C7DB39994AB5"));
    }
}
