using SoulstormReplayReader.Core;

string[] filePaths =
{
    // @"TestReplays\replay_6p_2v2v2_001.rec",
    // @"TestReplays\gens_builds\ork_many_researches.rec",
//     @"TestReplays\lag\lag3.rec",
//     "TestReplays/buildTest/ork_all_things.rec",
//     "TestReplays/buildTest/eldar_all_things.rec",
//     "TestReplays/buildTest/necr_all_things.rec",
//     "TestReplays/buildTest/sisters_all_things.rec",
     // "TestReplays/buildTest/eldar_aspect.rec",
//     "TestReplays\\Unfair\\Cheats\\deletedArmy_4.rec",
     // "TestReplays\\Tests\\steam_skip_data_test_1.rec",
    // "TestReplays\\Tests\\some_error.rec",
    // "TestReplays\\790059-W-DE-L-CSM.rec"
    "TestReplays\\4p_withBots.rec",
    // "TestReplays\\unification_anon_2_races.rec"
    // "TestReplays\\Unfair\\Bugs\\SoB_2commanders_1squad.rec",
    // @"TestReplays\Unfair\Cheats\eresArchitect_delet.rec",
    // @"TestReplays\Broken\#860994_empty_name.rec",
    // "TestReplays\\Unfair\\Cheats\\mono_in_1_min.rec",
//     @"TestReplays\old\BM.rec"
//      "TestReplays\\Unfair\\Bugs\\necr_action_110.rec",
};
string[] dirPaths =
{
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Many",
    // "TestReplays\\Long",
    // "TestReplays\\Tests",
    // "TestReplays\\Broken",
    // "TestReplays\\gens_builds",
    // "TestReplays\\Unfair\\Cheats",
    // "TestReplays\\Unfair\\Bugs",
};

var replayPaths = filePaths.Concat(dirPaths.SelectMany(Directory.EnumerateFiles)).ToArray().AsSpan(); // .Select(path => "..\\..\\..\\" + path)

foreach (var replayPath in replayPaths)
{
    var stream = new MemoryStream(File.ReadAllBytes(replayPath), false); // 8192

// #if DEBUGLOGGING
    // Console.WriteLine(replayPath);
// #endif

    using var replayReader = new SsReplayReader(stream)
    {
        CheckForBugs = true,
        SkipImages = true
    };
    var replay = replayReader.ReadFull();
    
    Console.WriteLine(string.Join(' ', replay.WinConditions));
    
// #if DEBUGLOGGING
    // Console.WriteLine(replay.TotalTicks);
    // Console.WriteLine(replay.Actions?.GroupBy(x => x.Tick).Count());
// #endif

    // foreach (var player in replay.Players.Where(player => player.BugChecker != null))
    // {
    //     if (player.BugChecker.HasAccusations())
    //     {
    //         Console.WriteLine($"\n{filePath}");
    //         Console.WriteLine($"{player.Name} [{player.Race}]:\n  {player.BugChecker.GetAccusationsList()}");
    //     }
    // }
}


//     Console.WriteLine(@$"
// ---------GAME SETTINGS---------
//     world seed: {replay.WorldSeed}
//        ai diff: {replay.GameSettings.AiDifficulty}
//      start res: {replay.GameSettings.StartResources}
//     lock teams: {replay.GameSettings.LockTeams}
//      cheats on: {replay.GameSettings.CheatsOn}
// start location: {replay.GameSettings.StartingLocation}
//     game speed: {replay.GameSettings.GameSpeed}
//    res sharing: {replay.GameSettings.ResourceSharing}
//       res rate: {replay.GameSettings.ResourceRate}
//       glob: {replay.ConditionsString}
// ");
// }

// foreach (var cmd in replayReader.Actions)
// {
//     actions.Add(cmd);
// }
// foreach (var action in replay.Actions)
// {
//     var player = replay.Players[action.PlayerId];
//     if (player.ResolvedRace == RaceEnum.Necrons)
//     {
//             
//     }
// }
// TO WATCH
// #351207-W-ORK-L-NEC.rec
// #351190-W-ORK-L-NEC.rec
// #274008.rec

// foreach (var cmd in actions.OrderBy(a => a))
// {
//     Console.Write($"{cmd} ");
// }

// foreach (var cmd in actions.OrderBy(a => a.Key).GroupBy(a => a.Key))
// {
//     foreach (var a in cmd.DistinctBy(a => a.Value.Value).OrderBy(a => a.Value.Value))
//     {
//         Console.WriteLine($"{a.Key} {a.Value.Key} {a.Value.Value}");
//     }
// }

// var emptyAction = new EmptyAction();
// IGameAction action = emptyAction;
// Console.WriteLine(0 == null);

// var inStream = new BufferedStream(File.OpenRead(@"TestReplays/gameTest/12_actions.rec"), 65536);
// var outStream = new BufferedStream(File.OpenWrite(@"TestReplays/gameTest/12_actions_renamed.rec"), 65536);
// ReplayService.Rename(inStream, outStream, "12 acutiomos").Wait();

/* primary commands:
 117 - build building
 2 - destroy building
 */

//1 1 e8 3 0 0 2 6 66 c3 0 0 0 54 1 61 0 1c 2
//1 1 e8 3 0 0 2 6 64 c3 0 0 0 bc fd a0 0 2c 1


/*
---------GAME SETTINGS---------
       ai diff: 1
     start res: 0
    lock teams: 1
     cheats on: 0
start location: 2
    game speed: 0
   res sharing: 2
      res rate: 0
 */

// foreach (var kv in replayReader.ComPairs)
// {
//     commands.Add(kv);
// }


//     Console.WriteLine(@$".
// ------------GENERAL------------
//           name: {replay.IngameName}
//            mod: {replay.ModName}
//        version: {replay.Version}
//         engine: {replay.EngineName}
//   engine addon: {replay.EngineAddon}
// active players: {replay.ActivePlayersCount}
//          teams: {replay.TeamsCount}
//    total ticks: {replay.TotalTicks}
// .
// --------------LAG--------------
//     lag chunks: {replay.StreamInfo.LagChunks}
// .
// --------------MAP--------------
//       map name: {replay.Map.Name}
//       map size: {replay.Map.Size}
// ");
//
//     
// foreach (var msg in replay.ChatMessages)
// {
//     Console.WriteLine(msg.ToString());
// }
// }

// Console.WriteLine(@$"
// ---------GAME SETTINGS---------
//        ai diff: {replay.GameSettings.AiDifficulty}
//      start res: {replay.GameSettings.StartResources}
//     lock teams: {replay.GameSettings.LockTeams}
//      cheats on: {replay.GameSettings.CheatsOn}
// start location: {replay.GameSettings.StartingLocation}
//     game speed: {replay.GameSettings.GameSpeed}
//    res sharing: {replay.GameSettings.ResourceSharing}
//       res rate: {replay.GameSettings.ResourceRate}
// ");
//
