using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Choices;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Plugin.Gen7.Scripts.Moves;
using PkmnLib.Plugin.Gen7.Scripts.Side;
using PkmnLib.Static.Moves;
using PkmnLib.Static.Utils;

namespace PkmnLib.Plugin.Gen7.Tests.Scripts.Moves;

/// <summary>
/// Tests for the <see cref="EchoedVoice"/> move script and its <see cref="EchoedVoiceData"/> side marker.
/// Gen VII Bulbapedia behavior: "The move's base power increases by 40 each consecutive turn when used
/// repeatedly, reaching a maximum of 200. Power increases only once per round, regardless of how many
/// Pokémon deploy it."
/// </summary>
public class EchoedVoiceTests
{
    /// <summary>
    /// Creates a mocked move choice whose chosen move has the given name.
    /// </summary>
    private static IMoveChoice CreateMoveChoice(string moveName)
    {
        var moveData = Substitute.For<IMoveData>();
        moveData.Name.Returns(new StringKey(moveName));
        var learnedMove = Substitute.For<ILearnedMove>();
        learnedMove.MoveData.Returns(moveData);
        var choice = Substitute.For<IMoveChoice>();
        choice.ChosenMove.Returns(learnedMove);
        return choice;
    }

    /// <summary>
    /// Creates a fully mocked test setup where the user's side has a real <see cref="ScriptSet"/> as its
    /// volatile scripts.
    /// </summary>
    private static (EchoedVoice script, IExecutingMove move, IPokemon target, IScriptSet sideScripts) CreateTestSetup()
    {
        var script = new EchoedVoice();
        var move = Substitute.For<IExecutingMove>();
        var target = Substitute.For<IPokemon>();

        var side = Substitute.For<IBattleSide>();
        side.GetScripts().Returns(_ => new ScriptIterator(Array.Empty<IEnumerable<ScriptContainer>>()));
        IScriptSet sideScripts = new ScriptSet(side);
        side.VolatileScripts.Returns(sideScripts);

        var battle = Substitute.For<IBattle>();
        battle.Sides.Returns(new[] { side });

        var battleData = Substitute.For<IPokemonBattleData>();
        battleData.Battle.Returns(battle);
        battleData.SideIndex.Returns((byte)0);

        var user = Substitute.For<IPokemon>();
        user.BattleData.Returns(battleData);
        move.User.Returns(user);

        return (script, move, target, sideScripts);
    }

    /// <summary>
    /// Bulbapedia: "The move's base power increases by 40 each consecutive turn" — on the first use there
    /// is no previous turn, so the base power is not modified.
    /// </summary>
    [Test]
    public async Task ChangeBasePower_FirstUse_BasePowerUnchanged()
    {
        // Arrange
        var (script, move, target, _) = CreateTestSetup();
        ushort basePower = 40;

        // Act
        script.ChangeBasePower(move, target, 0, ref basePower);

        // Assert
        await Assert.That(basePower).IsEqualTo((ushort)40);
    }

    /// <summary>
    /// Bulbapedia: "The move's base power increases by 40 each consecutive turn when used repeatedly,
    /// reaching a maximum of 200." Each previous consecutive use adds a stack worth 40 power: 80 on the
    /// second turn, 120 on the third, 160 on the fourth, and 200 on the fifth.
    /// </summary>
    [Test, Arguments(1, (ushort)80), Arguments(2, (ushort)120), Arguments(3, (ushort)160), Arguments(4, (ushort)200)]
    public async Task ChangeBasePower_ConsecutiveUses_PowerIncreasesByFortyPerTurn(int previousUses,
        ushort expectedPower)
    {
        // Arrange - the move hit on the given number of previous consecutive turns
        var (script, move, target, _) = CreateTestSetup();
        for (var i = 0; i < previousUses; i++)
            script.OnSecondaryEffect(move, target, 0);
        ushort basePower = 40;

        // Act
        script.ChangeBasePower(move, target, 0, ref basePower);

        // Assert
        await Assert.That(basePower).IsEqualTo(expectedPower);
    }

    /// <summary>
    /// Bulbapedia: the base power increases "reaching a maximum of 200" — further consecutive uses beyond
    /// the fifth turn do not push the power past the cap.
    /// </summary>
    [Test]
    public async Task ChangeBasePower_MoreThanFourPreviousUses_PowerCappedAtTwoHundred()
    {
        // Arrange
        var (script, move, target, _) = CreateTestSetup();
        for (var i = 0; i < 6; i++)
            script.OnSecondaryEffect(move, target, 0);
        ushort basePower = 40;

        // Act
        script.ChangeBasePower(move, target, 0, ref basePower);

        // Assert
        await Assert.That(basePower).IsEqualTo((ushort)200);
    }

    /// <summary>
    /// Bulbapedia: the power increase applies "when used repeatedly" — using the move marks the user's side
    /// with <see cref="EchoedVoiceData"/> so the next turn's use is recognized as consecutive.
    /// </summary>
    [Test]
    public async Task OnSecondaryEffect_AddsEchoedVoiceDataToUserSide()
    {
        // Arrange
        var (script, move, target, sideScripts) = CreateTestSetup();

        // Act
        script.OnSecondaryEffect(move, target, 0);

        // Assert
        await Assert.That(sideScripts.Contains(ScriptUtils.ResolveName<EchoedVoiceData>())).IsTrue();
    }

    /// <summary>
    /// Bulbapedia: "The move's base power increases by 40 each consecutive turn" — each additional use
    /// stacks the side marker, increasing <see cref="EchoedVoiceData.Stacks"/>.
    /// </summary>
    [Test]
    public async Task OnSecondaryEffect_UsedAgain_StacksEchoedVoiceData()
    {
        // Arrange
        var (script, move, target, sideScripts) = CreateTestSetup();

        // Act
        script.OnSecondaryEffect(move, target, 0);
        script.OnSecondaryEffect(move, target, 0);

        // Assert
        await Assert.That(sideScripts.Get<EchoedVoiceData>()!.Stacks).IsEqualTo(2);
    }

    /// <summary>
    /// Technical test: without battle data on the user (outside of battle) the base power hook does
    /// nothing and does not throw.
    /// </summary>
    [Test]
    public async Task ChangeBasePower_UserHasNoBattleData_BasePowerUnchanged()
    {
        // Arrange
        var (script, move, target, _) = CreateTestSetup();
        move.User.BattleData.Returns((IPokemonBattleData?)null);
        ushort basePower = 40;

        // Act
        script.ChangeBasePower(move, target, 0, ref basePower);

        // Assert
        await Assert.That(basePower).IsEqualTo((ushort)40);
    }

    /// <summary>
    /// Bulbapedia: the boost only applies on "consecutive" turns — when a Pokémon on the side chooses a
    /// move other than Echoed Voice, the <see cref="EchoedVoiceData"/> marker removes itself, resetting
    /// the power.
    /// </summary>
    [Test]
    public async Task OnBeforeMoveChoice_DifferentMoveChosen_MarkerRemovesItself()
    {
        // Arrange
        var side = Substitute.For<IBattleSide>();
        side.GetScripts().Returns(_ => new ScriptIterator(Array.Empty<IEnumerable<ScriptContainer>>()));
        IScriptSet sideScripts = new ScriptSet(side);
        var data = new EchoedVoiceData();
        sideScripts.Add(data);

        // Act
        data.OnBeforeMoveChoice(CreateMoveChoice("tackle"));

        // Assert
        await Assert.That(sideScripts.Contains(ScriptUtils.ResolveName<EchoedVoiceData>())).IsFalse();
    }

    /// <summary>
    /// Bulbapedia: "The move's base power increases by 40 each consecutive turn when used repeatedly" —
    /// choosing Echoed Voice again keeps the marker (and its stacks) alive.
    /// </summary>
    [Test]
    public async Task OnBeforeMoveChoice_EchoedVoiceChosen_MarkerStaysActive()
    {
        // Arrange
        var side = Substitute.For<IBattleSide>();
        side.GetScripts().Returns(_ => new ScriptIterator(Array.Empty<IEnumerable<ScriptContainer>>()));
        IScriptSet sideScripts = new ScriptSet(side);
        var data = new EchoedVoiceData();
        sideScripts.Add(data);

        // Act
        data.OnBeforeMoveChoice(CreateMoveChoice("echoed_voice"));

        // Assert
        await Assert.That(sideScripts.Contains(ScriptUtils.ResolveName<EchoedVoiceData>())).IsTrue();
    }
}