using System.Threading.Tasks;
using NUnit.Framework;
using Robust.Shared.ContentPack;
using Robust.Shared.EntitySerialization;
using Robust.Shared.EntitySerialization.Components;
using Robust.Shared.EntitySerialization.Systems;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Utility;
using static Robust.UnitTesting.Shared.EntitySerialization.EntitySaveTestComponent;

namespace Robust.UnitTesting.Shared.EntitySerialization;

[TestFixture]
public sealed partial class BackwardsCompatibilityTest
{
    /// <summary>
    /// Check that v6 maps can be loaded. This simply tries to load a file and doesn't do a lot of extra validation.
    /// </summary>
    /// <remarks>
    /// The file was pilfered from content integration tests ("floor3x3.yml") and modified slightly.
    /// </remarks>
    [Test]
    public async Task TestLoadV6()
    {
        var server = StartServer(new ServerIntegrationOptions {ExtraPrototypes = PrototypeV6});
        await server.WaitIdleAsync();
        var entMan = server.EntMan;
        var mapSys = server.System<SharedMapSystem>();
        var loader = server.System<MapLoaderSystem>();
        var meta = server.System<MetaDataSystem>();
        var tileMan = server.ResolveDependency<ITileDefinitionManager>();
        var resourceManager = server.ResolveDependency<IResourceManagerInternal>();

        tileMan.Register(new TileDef("Space"));
        tileMan.Register(new TileDef("A"));
        tileMan.Register(new TileDef("B"));
        var gridPath = new ResPath($"{nameof(MapDataV6Grid)}.yml");
        resourceManager.MountString(gridPath.ToString(), MapDataV6Grid);

        MapId mapId = default;
        EntityUid mapUid = default;

        Entity<TransformComponent, EntitySaveTestComponent> map;
        Entity<TransformComponent, EntitySaveTestComponent> ent;
        Entity<TransformComponent, EntitySaveTestComponent> grid;

        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(0));
        await server.WaitPost(() => mapUid = mapSys.CreateMap(out mapId));
        await server.WaitPost(() => Assert.That(loader.TryLoadGrid(mapId, gridPath, out _)));

        Assert.That(entMan.Count<LoadedMapComponent>(), Is.EqualTo(0));
        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(2));
        ent = Find(nameof(ent), entMan);
        grid = Find(nameof(grid), entMan);

        Assert.That(ent.Comp1.ParentUid, Is.EqualTo(grid.Owner));
        Assert.That(grid.Comp1.ParentUid, Is.EqualTo(mapUid));

        Assert.That(meta.EntityPaused(ent), Is.False);
        Assert.That(meta.EntityPaused(grid), Is.False);
        Assert.That(meta.EntityPaused(mapUid), Is.False);

        Assert.That(entMan.GetComponent<MetaDataComponent>(ent).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.MapInitialized));
        Assert.That(entMan.GetComponent<MetaDataComponent>(grid).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.MapInitialized));
        Assert.That(entMan.GetComponent<MetaDataComponent>(mapUid).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.MapInitialized));

        await server.WaitPost(() => entMan.DeleteEntity(mapUid));
        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(0));

        var mapPath = new ResPath($"{nameof(MapDataV6Map)}.yml");
        resourceManager.MountString(mapPath.ToString(), MapDataV6Map);
        await server.WaitPost(() => Assert.That(loader.TryLoadMap(mapPath, out _, out _)));

        Assert.That(entMan.Count<LoadedMapComponent>(), Is.EqualTo(1));
        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(3));

        ent = Find(nameof(ent), entMan);
        grid = Find(nameof(grid), entMan);
        map = Find(nameof(map), entMan);

        Assert.That(ent.Comp1.ParentUid, Is.EqualTo(grid.Owner));
        Assert.That(grid.Comp1.ParentUid, Is.EqualTo(map.Owner));
        Assert.That(map.Comp1.ParentUid, Is.EqualTo(EntityUid.Invalid));

        Assert.That(meta.EntityPaused(ent), Is.True);
        Assert.That(meta.EntityPaused(grid), Is.True);
        Assert.That(meta.EntityPaused(map), Is.True);

        Assert.That(entMan.GetComponent<MetaDataComponent>(ent).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.Initialized));
        Assert.That(entMan.GetComponent<MetaDataComponent>(grid).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.Initialized));
        Assert.That(entMan.GetComponent<MetaDataComponent>(map).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.Initialized));

        await server.WaitPost(() => entMan.DeleteEntity(map));
        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(0));

        // Repeat test, but with the initialize maps option enabled.
        // Apparently mounted strings can only be read a single time.
        // So have to re-mount them.
        var mapPath2 = new ResPath($"{nameof(MapDataV6Map)}2.yml");
        resourceManager.MountString(mapPath2.ToString(), MapDataV6Map);

        var opts = DeserializationOptions.Default with {InitializeMaps = true};
        await server.WaitPost(() => Assert.That(loader.TryLoadMap(mapPath2, out _, out _, opts)));

        Assert.That(entMan.Count<LoadedMapComponent>(), Is.EqualTo(1));
        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(3));

        ent = Find(nameof(ent), entMan);
        grid = Find(nameof(grid), entMan);
        map = Find(nameof(map), entMan);

        Assert.That(ent.Comp1.ParentUid, Is.EqualTo(grid.Owner));
        Assert.That(grid.Comp1.ParentUid, Is.EqualTo(map.Owner));
        Assert.That(map.Comp1.ParentUid, Is.EqualTo(EntityUid.Invalid));

        Assert.That(meta.EntityPaused(ent), Is.False);
        Assert.That(meta.EntityPaused(grid), Is.False);
        Assert.That(meta.EntityPaused(map), Is.False);

        Assert.That(entMan.GetComponent<MetaDataComponent>(ent).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.MapInitialized));
        Assert.That(entMan.GetComponent<MetaDataComponent>(grid).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.MapInitialized));
        Assert.That(entMan.GetComponent<MetaDataComponent>(map).EntityLifeStage,
            Is.EqualTo(EntityLifeStage.MapInitialized));

        await server.WaitPost(() => entMan.DeleteEntity(map));
        Assert.That(entMan.Count<EntitySaveTestComponent>(), Is.EqualTo(0));
    }

    private const string MapDataV6Grid = @"
meta:
  format: 6
  postmapinit: false
tilemap:
  0: Space
  11: A
  89: B
entities:
- proto: """"
  entities:
  - uid: 2
    components:
    - type: MetaData
    - type: Transform
      parent: invalid
    - type: MapGrid
      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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
          version: 6
    - type: Broadphase
    - type: Physics
      bodyStatus: InAir
      angularDamping: 0.05
      linearDamping: 0.05
      fixedRotation: False
      bodyType: Dynamic
    - type: OccluderTree
    - type: EntitySaveTest
      id: grid
- proto: V6TestProto
  entities:
  - uid: 1
    components:
    - type: Transform
      pos: 0.5,0.5
      parent: 2
    - type: EntitySaveTest
      id: ent
";

    private const string MapDataV6Map = @"
meta:
  format: 6
  postmapinit: false
tilemap:
  0: Space
  11: A
  89: B
entities:
- proto: """"
  entities:
  - uid: 123
    components:
    - type: MetaData
    - type: Transform
    - type: Map
      mapPaused: True
    - type: EntitySaveTest
      id: map
  - uid: 2
    components:
    - type: MetaData
    - type: Transform
      parent: 123
    - type: MapGrid
      chunks:
        -1,-1:
          ind: -1,-1
          tiles: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
          version: 6
    - type: Broadphase
    - type: Physics
      bodyStatus: InAir
      angularDamping: 0.05
      linearDamping: 0.05
      fixedRotation: False
      bodyType: Dynamic
    - type: OccluderTree
    - type: EntitySaveTest
      id: grid
- proto: V6TestProto
  entities:
  - uid: 1
    components:
    - type: Transform
      pos: 0.5,0.5
      parent: 2
    - type: EntitySaveTest
      id: ent
";

    private const string PrototypeV6 = @"
- type: entity
  id: V6TestProto
  components:
  - type: EntitySaveTest
    list: [ 1, 2 ]
";
}
