using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Choices;
using PkmnLib.Plugin.Gen7.Scripts.Moves;

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

/// <summary>
/// Tests for the <see cref="AfterYou"/> move script.
/// Gen VII Bulbapedia behavior (Generations V to VII): "The target will move next on the current turn,
/// ignoring priority. [...] It fails if the target has already moved on the same turn. [...] After You fails
/// if the order remains the same after using After You."
/// </summary>
public class AfterYouTests
{
    private static IMoveChoice CreateChoice(IPokemon user, uint speed)
    {
        var choice = Substitute.For<IMoveChoice>();
        choice.User.Returns(user);
        choice.Speed.Returns(speed);
        return choice;
    }

    private static (AfterYou script, IExecutingMove move, IHitData hitData) CreateTestSetup(BattleChoiceQueue? queue)
    {
        var script = new AfterYou();
        var move = Substitute.For<IExecutingMove>();
        var hitData = Substitute.For<IHitData>();
        move.GetHitData(Arg.Any<IPokemon>(), Arg.Any<byte>()).Returns(hitData);

        var battle = Substitute.For<IBattle>();
        battle.ChoiceQueue.Returns(queue);
        var battleData = Substitute.For<IPokemonBattleData>();
        battleData.Battle.Returns(battle);

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

        return (script, move, hitData);
    }

    /// <summary>
    /// Bulbapedia: "The target will move next on the current turn, ignoring priority."
    /// The target's choice is moved in front of a faster Pokémon's remaining choice, and the move does not fail.
    /// </summary>
    [Test]
    public async Task OnSecondaryEffect_TargetLaterInQueue_TargetMovesNext()
    {
        // Arrange
        var user = Substitute.For<IPokemon>();
        var target = Substitute.For<IPokemon>();
        var other = Substitute.For<IPokemon>();
        // Sorted by speed: user (100), other (75), target (50)
        var queue = new BattleChoiceQueue([
            CreateChoice(user, 100), CreateChoice(other, 75), CreateChoice(target, 50),
        ]);
        // The user's own choice is executing, so it has been dequeued already.
        queue.Dequeue();

        var (script, move, hitData) = CreateTestSetup(queue);

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

        // Assert - the target is now the next Pokémon to move, ahead of the faster one
        await Assert.That(queue.Peek()!.User).IsEqualTo(target);
        hitData.DidNotReceive().Fail();
    }

    /// <summary>
    /// Bulbapedia: "It fails if the target has already moved on the same turn."
    /// The target's choice is no longer in the queue, so the move fails.
    /// </summary>
    [Test]
    public void OnSecondaryEffect_TargetAlreadyMoved_Fails()
    {
        // Arrange
        var user = Substitute.For<IPokemon>();
        var target = Substitute.For<IPokemon>();
        // Sorted by speed: target (100), user (50)
        var queue = new BattleChoiceQueue([
            CreateChoice(target, 100), CreateChoice(user, 50),
        ]);
        // The target already moved this turn, and the user's own choice is executing.
        queue.Dequeue();
        queue.Dequeue();

        var (script, move, hitData) = CreateTestSetup(queue);

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

        // Assert
        hitData.Received(1).Fail();
    }

    /// <summary>
    /// Bulbapedia: "After You fails if the order remains the same after using After You."
    /// The target was already the next Pokémon to move, so using After You changes nothing and the move
    /// must fail.
    /// </summary>
    [Test]
    public void OnSecondaryEffect_TargetAlreadyNext_Fails()
    {
        // Arrange
        var user = Substitute.For<IPokemon>();
        var target = Substitute.For<IPokemon>();
        // Sorted by speed: user (100), target (50)
        var queue = new BattleChoiceQueue([
            CreateChoice(user, 100), CreateChoice(target, 50),
        ]);
        // The user's own choice is executing; the target is already next.
        queue.Dequeue();

        var (script, move, hitData) = CreateTestSetup(queue);

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

        // Assert
        hitData.Received(1).Fail();
    }

    /// <summary>
    /// Technical test: when the battle has no active choice queue, the script returns without throwing
    /// and without failing the hit.
    /// </summary>
    [Test]
    public void OnSecondaryEffect_NoChoiceQueue_DoesNothing()
    {
        // Arrange
        var (script, move, hitData) = CreateTestSetup(null);

        // Act
        script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);

        // Assert
        hitData.DidNotReceive().Fail();
    }
}