using System.Text.Json;
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Serialized;
using PkmnLib.Static;
using PkmnLib.Static.Species;
using PkmnLib.Tests.Integration;

namespace PkmnLib.Tests.Dynamic;

public class SerializationTests
{
    [Test]
    public async Task SerializePokemon()
    {
        var library = LibraryHelpers.LoadLibrary();
        await Assert.That(library.StaticLibrary.Species.TryGet("bulbasaur", out var species)).IsTrue();

        var pokemon = new PokemonImpl(library, species!, species!.GetDefaultForm(), new AbilityIndex
        {
            Index = 0,
            IsHidden = false,
        }, 10, 1000, Gender.Male, 0, "hardy");
        pokemon.LearnMove("tackle", MoveLearnMethod.LevelUp, 255);

        var data = pokemon.Serialize();
        await Assert.That(data).IsNotNull();
        await Assert.That(data.Species).IsEqualTo("bulbasaur");
        await Assert.That(data.Form).IsEqualTo("default");
        await Assert.That(data.Ability).IsEqualTo("overgrow");
        await Assert.That(data.Level).IsEqualTo((byte)10);
        await Assert.That(data.PersonalityValue).IsEqualTo((uint)1000);
        await Assert.That(data.Gender).IsEqualTo(Gender.Male);
        await Assert.That(data.Coloring).IsEqualTo((byte)0);
        await Assert.That(data.HeldItem).IsNull();
        await Assert.That(data.CurrentHealth).IsEqualTo((uint)29);
        await Assert.That(data.Happiness).IsEqualTo((byte)70);

        await Assert.That(data.Moves).Count().IsEqualTo(4);
        await Assert.That(data.Moves[0]).IsNotNull();
        await Assert.That(data.Moves[0]!.MoveName).IsEqualTo("tackle");
        await Assert.That(data.Moves[0]!.LearnMethod).IsEqualTo(MoveLearnMethod.LevelUp);
        await Assert.That(data.Moves[0]!.CurrentPp).IsEqualTo((byte)35);
    }

    [Test]
    public async Task DeserializePokemon()
    {
        var library = LibraryHelpers.LoadLibrary();
        var data = new SerializedPokemon
        {
            Species = "bulbasaur",
            Form = "default",
            Ability = "overgrow",
            Level = 10,
            Experience = 560,
            PersonalityValue = 1000,
            Gender = Gender.Male,
            Coloring = 0,
            HeldItem = null,
            CurrentHealth = 29,
            Happiness = 70,
            IndividualValues = new SerializedStats(20, 20, 20, 20, 20, 20),
            EffortValues = new SerializedStats(0, 0, 0, 0, 0, 0),
            Nature = "hardy",
            Nickname = "foo",
            Moves = new[]
            {
                new SerializedLearnedMove
                {
                    MoveName = "tackle",
                    LearnMethod = MoveLearnMethod.LevelUp,
                    CurrentPp = 23,
                },
                null,
                null,
                null,
            },
        };

        var pokemon = new PokemonImpl(library, data);
        await Assert.That(pokemon.Species.Name.ToString()).IsEqualTo("bulbasaur");
        await Assert.That(pokemon.Form.Name.ToString()).IsEqualTo("default");
        await Assert.That(pokemon.AbilityIndex.Index).IsEqualTo((byte)0);
        await Assert.That(pokemon.Level).IsEqualTo((LevelInt)10);
        await Assert.That(pokemon.Experience).IsEqualTo((uint)560);
        await Assert.That(pokemon.PersonalityValue).IsEqualTo((uint)1000);
        await Assert.That(pokemon.Gender).IsEqualTo(Gender.Male);
        await Assert.That(pokemon.Coloring).IsEqualTo((byte)0);
        await Assert.That(pokemon.HeldItem).IsNull();
        await Assert.That(pokemon.CurrentHealth).IsEqualTo((uint)29);
        await Assert.That(pokemon.Happiness).IsEqualTo((byte)70);
        await Assert.That(pokemon.IndividualValues).IsEqualTo(new IndividualValueStatisticSet(20, 20, 20, 20, 20, 20));
        await Assert.That(pokemon.EffortValues).IsEqualTo(new EffortValueStatisticSet(0, 0, 0, 0, 0, 0));
        await Assert.That(pokemon.Nature.Name.ToString()).IsEqualTo("hardy");
        await Assert.That(pokemon.Nickname).IsEqualTo("foo");
        await Assert.That(pokemon.Moves).Count().IsEqualTo(4);
        await Assert.That(pokemon.Moves[0]).IsNotNull();
        await Assert.That(pokemon.Moves[0]!.MoveData.Name.ToString()).IsEqualTo("tackle");
        await Assert.That(pokemon.Moves[0]!.LearnMethod).IsEqualTo(MoveLearnMethod.LevelUp);
        await Assert.That(pokemon.Moves[0]!.CurrentPp).IsEqualTo((byte)23);
    }

    [Test]
    public async Task SerializedPokemonToJson()
    {
        var data = new SerializedPokemon
        {
            Species = "bulbasaur",
            Form = "default",
            Ability = "overgrow",
            Level = 10,
            Experience = 560,
            PersonalityValue = 1000,
            Gender = Gender.Male,
            Coloring = 0,
            HeldItem = null,
            CurrentHealth = 29,
            Happiness = 70,
            IndividualValues = new SerializedStats(20, 20, 20, 20, 20, 20),
            EffortValues = new SerializedStats(0, 0, 0, 0, 0, 0),
            Nature = "hardy",
            Nickname = "foo",
            Moves = new[]
            {
                new SerializedLearnedMove
                {
                    MoveName = "tackle",
                    LearnMethod = MoveLearnMethod.LevelUp,
                    CurrentPp = 23,
                },
                null,
                null,
                null,
            },
        };

        var json = JsonSerializer.Serialize(data);
        var deserialized = JsonSerializer.Deserialize<SerializedPokemon>(json);
        await Assert.That(deserialized).IsNotNull();
        await Assert.That(deserialized!.Species).IsEqualTo("bulbasaur");
        await Assert.That(deserialized.Form).IsEqualTo("default");
        await Assert.That(deserialized.Ability).IsEqualTo("overgrow");
        await Assert.That(deserialized.Level).IsEqualTo((byte)10);
        await Assert.That(deserialized.Experience).IsEqualTo((uint)560);
        await Assert.That(deserialized.PersonalityValue).IsEqualTo((uint)1000);
        await Assert.That(deserialized.Gender).IsEqualTo(Gender.Male);
        await Assert.That(deserialized.Coloring).IsEqualTo((byte)0);
        await Assert.That(deserialized.HeldItem).IsNull();
        await Assert.That(deserialized.CurrentHealth).IsEqualTo((uint)29);
        await Assert.That(deserialized.Happiness).IsEqualTo((byte)70);
        await Assert.That(deserialized.IndividualValues).IsEqualTo(new SerializedStats(20, 20, 20, 20, 20, 20));
        await Assert.That(deserialized.EffortValues).IsEqualTo(new SerializedStats(0, 0, 0, 0, 0, 0));
        await Assert.That(deserialized.Nature).IsEqualTo("hardy");
        await Assert.That(deserialized.Nickname).IsEqualTo("foo");

        await Assert.That(deserialized.Moves).Count().IsEqualTo(4);
        await Assert.That(deserialized.Moves[0]).IsNotNull();
        await Assert.That(deserialized.Moves[0]!.MoveName).IsEqualTo("tackle");
        await Assert.That(deserialized.Moves[0]!.LearnMethod).IsEqualTo(MoveLearnMethod.LevelUp);
        await Assert.That(deserialized.Moves[0]!.CurrentPp).IsEqualTo((byte)23);
    }
}