using System.Collections.Generic;
using System.Linq;
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Choices;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Plugin.Gen7.Scripts.Utils;
using PkmnLib.Static.Moves;
using PkmnLib.Static.Utils;

namespace PkmnLib.Plugin.Gen7.Moves;

/// <summary>
/// The user hurriedly and randomly uses a move among those known by ally Pokémon. 
/// </summary>
/// <remarks>
/// The Assist user executes a move that is randomly selected from all the eligible moves known by other Pokémon in the
/// same party. If the same move is known by multiple Pokémon, each of them counts and therefore the move is more likely
/// to be called. Assist can call a move that currently has no PP or is known by a fainted Pokémon. It is also able to
/// call moves that are currently inaccessible to their original users due to Disable, Heal Block, Taunt, or Torment.
/// If the other Pokémon in the user's party do not know any eligible moves, Assist fails. 
/// </remarks>
[Script(ScriptCategory.Move, "assist")]
public class Assist : Script
{
    /// <inheritdoc />
    public override void ChangeMove(IMoveChoice choice, ref StringKey moveName)
    {
        var user = choice.User;
        if (user.BattleData == null)
            return;
        var battle = user.BattleData.Battle;
        var party = battle.Parties.FirstOrDefault(p => p.Party.Contains(user));
        if (party == null)
        {
            choice.Fail();
            return;
        }
        var moves = GetPartyMoves(party.Party, user).ToList();
        if (moves.Count == 0)
        {
            choice.Fail();
            return;
        }
        var random = battle.Random.GetInt(moves.Count);
        moveName = moves[random].Name;
    }

    private static IEnumerable<IMoveData> GetPartyMoves(IPokemonParty party, IPokemon user)
    {
        foreach (var pokemon in party.WhereNotNull())
        {
            if (pokemon == user)
                continue;
            var moves = pokemon.Moves.WhereNotNull().Where(m => m.MoveData.CanCopyMove());
            foreach (var move in moves)
            {
                yield return move.MoveData;
            }
        }
    }
}