using System;

namespace PKHeX.Core;

/// <summary>
/// Level Up Learn Movepool Information
/// </summary>
public sealed class Learnset(ushort[] Moves, byte[] Levels)
{
    /// <summary>
    /// Moves that can be learned.
    /// </summary>
    private readonly ushort[] Moves = Moves;

    /// <summary>
    /// Levels at which a move at a given index can be learned.
    /// </summary>
    private readonly byte[] Levels = Levels;

    private const byte MagicEvolutionMoveLevel = 0;

    public ReadOnlySpan<ushort> GetAllMoves() => Moves;
    public ReadOnlySpan<byte> GetAllLevels() => Levels;

    public ReadOnlySpan<ushort> GetMoveRange(byte maxLevel, byte minLevel = 0)
    {
        if (minLevel <= Experience.MinLevel && maxLevel >= Experience.MaxLevel) // out of range, return all
            return Moves;
        if (minLevel > maxLevel)
            return default;
        int start = FindGrq(minLevel);
        if (start < 0)
            return default;
        int end = FindLastLeq(maxLevel);
        if (end < 0)
            return default;

        var length = end - start + 1;
        return Moves.AsSpan(start, length);
    }

    private int FindGrq(byte level, int start = 0)
    {
        var levels = Levels;
        for (int i = start; i < levels.Length; i++)
        {
            if (levels[i] >= level)
                return i;
        }
        return -1;
    }

    private int FindGr(byte level, int start)
    {
        var levels = Levels;
        for (int i = start; i < levels.Length; i++)
        {
            if (levels[i] >= level)
                return i;
        }
        return -1;
    }

    private int FindLastLeq(byte level, int end = 0)
    {
        var levels = Levels;
        for (int i = levels.Length - 1; i >= end; i--)
        {
            if (levels[i] <= level)
                return i;
        }
        return -1;
    }

    /// <summary>Returns the moves a Pokémon would have if it were encountered at the specified level.</summary>
    /// <remarks>In Generation 1, it is not possible to learn any moves lower than these encounter moves.</remarks>
    /// <param name="level">The level the Pokémon was encountered at.</param>
    /// <param name="moves">Move array to write to</param>
    /// <param name="ctr">Starting index to begin overwriting at</param>
    /// <returns>Array of Move IDs</returns>
    public void SetEncounterMoves(byte level, Span<ushort> moves, int ctr = 0)
    {
        for (int i = 0; i < Moves.Length; i++)
        {
            var req = Levels[i];
            if (req < 1) // Evolution or Relearn-menu-only moves
                continue;
            if (req > level)
                break;

            AddMoveShiftLater(moves, ref ctr, Moves[i]);
        }
        RectifyOrderShift(moves, ctr);
    }

    private static void AddMoveShiftLater(Span<ushort> moves, ref int ctr, ushort move)
    {
        if (!moves.Contains(move))
            moves[(ctr++) & 3] = move;
    }

    private static void RectifyOrderShift(Span<ushort> moves, int ctr)
    {
        // Perform (n & 3) rotations as if we were inserting moves, but a minimal amount of times.
        // This skips the rotation for when moves are inserted and then overwritten by later inserted moves.
        if (ctr <= moves.Length)
            return;
        var rotation = ctr & 3;
        if (rotation == 0)
            return;

        // rotate n times in-place
        for (int i = 0; i < rotation; i++)
        {
            var move = moves[0];
            for (int j = 0; j < 3; j++)
                moves[j] = moves[j + 1];
            moves[3] = move;
        }
    }

    public void SetEncounterMovesBackwards(byte level, Span<ushort> moves, int ctr = 0, bool sameDescend = true)
    {
        // sameDescend makes it work like a push-queue in reverse
        int index = FindLastLeq(level);

        while (true)
        {
            if (index == -1)
                return; // no moves to add?

            // In the event we have multiple moves at the same level, insert them in regular descending order.
            int start = index;

            if (sameDescend)
            {
                while (start != 0 && Levels[start] == Levels[start - 1])
                    start--;
            }

            for (int i = start; i <= index; i++)
            {
                var move = Moves[i];
                if (moves.Contains(move))
                    continue;

                if (Levels[i] == 0)
                    break; // not a Level Up move

                moves[ctr++] = move;
                if (ctr == 4)
                    return;
            }

            index = start - 1;
        }
    }

    /// <summary>Adds the learned moves by level up to the specified level.</summary>
    public void SetLevelUpMoves(byte startLevel, byte endLevel, Span<ushort> moves, int ctr = 0)
    {
        int startIndex = FindGrq(startLevel);
        if (startIndex == -1)
            return;
        int endIndex = FindGr(endLevel, startIndex);
        if (endIndex == -1)
            endIndex = Levels.Length;

        for (int i = startIndex; i < endIndex; i++)
            AddMoveShiftLater(moves, ref ctr, Moves[i]);
        RectifyOrderShift(moves, ctr);
    }

    /// <summary>Adds the moves that are gained upon evolving.</summary>
    /// <param name="moves">Move array to write to</param>
    /// <param name="ctr">Starting index to begin overwriting at</param>
    public void SetEvolutionMoves(Span<ushort> moves, int ctr = 0)
    {
        // Evolution moves are always at the lowest indexes of the learnset.
        for (int i = 0; i < Moves.Length; i++)
        {
            if (Levels[i] != MagicEvolutionMoveLevel)
                break;

            AddMoveShiftLater(moves, ref ctr, Moves[i]);
        }
        RectifyOrderShift(moves, ctr);
    }

    /// <summary>Adds the learned moves by level up to the specified level.</summary>
    public void SetLevelUpMoves(byte startLevel, byte endLevel, Span<ushort> moves, ReadOnlySpan<ushort> ignore, int ctr = 0)
    {
        int startIndex = FindGrq(startLevel);
        if (startIndex == -1)
            return; // No more remain
        int endIndex = FindGr(endLevel, startIndex);
        if (endIndex == -1)
            endIndex = Levels.Length;
        for (int i = startIndex; i < endIndex; i++)
        {
            var move = Moves[i];
            if (ignore.Contains(move))
                continue;

            AddMoveShiftLater(moves, ref ctr, move);
        }
        RectifyOrderShift(moves, ctr);
    }

    /// <summary>Adds the moves that are gained upon evolving.</summary>
    /// <param name="moves">Move array to write to</param>
    /// <param name="ignore">Ignored moves</param>
    /// <param name="ctr">Starting index to begin overwriting at</param>
    public void SetEvolutionMoves(Span<ushort> moves, ReadOnlySpan<ushort> ignore, int ctr = 0)
    {
        for (int i = 0; i < Moves.Length; i++)
        {
            if (Levels[i] != MagicEvolutionMoveLevel)
                break;

            var move = Moves[i];
            if (ignore.Contains(move))
                continue;

            AddMoveShiftLater(moves, ref ctr, move);
        }
        RectifyOrderShift(moves, ctr);
    }

    /// <summary>
    /// Checks if the specified move is learned by level up.
    /// </summary>
    /// <param name="move">Move ID</param>
    public bool GetIsLearn(ushort move) => Moves.AsSpan().Contains(move);

    /// <summary>
    /// Checks if the specified move is learned by level up.
    /// </summary>
    /// <param name="move">Move ID</param>
    /// <param name="level">Level at which the move is learned</param>
    /// <returns>True if the move is learned by level up, false otherwise.</returns>
    public bool TryGetLevelLearnMove(ushort move, out byte level)
    {
        var index = Array.IndexOf(Moves, move);
        if (index == -1)
        {
            level = 0;
            return false;
        }
        level = Levels[index];
        return true;
    }

    public ReadOnlySpan<ushort> GetBaseEggMoves(byte level)
    {
        // Count moves <= level
        var count = 0;
        foreach (ref readonly var x in Levels.AsSpan())
        {
            if (x > level)
                break;
            count++;
        }

        // Return a slice containing the moves <= level.
        if (count == 0)
            return [];

        int start = 0;
        if (count > 4)
        {
            start = count - 4;
            count = 4;
        }
        return Moves.AsSpan(start, count);
    }
}
