// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.

using System.Text;
using RaiNote.PacketMediator;

namespace Wonderking.Packets.Outgoing;

[WonderkingPacketId(OperationCode.CharacterSelectionSetGuildName)]
public class CharacterSelectionSetGuildNamePacket : IOutgoingPacket
{
    public required string[] GuildNames { get; set; }

    public byte[] Serialize()
    {
        Span<byte> data = stackalloc byte[1 + (1 + 16 + 1) * GuildNames.Length];
        data.Clear();
        data[0] = (byte)GuildNames.Length;
        for (var i = 0; i < GuildNames.Length; i++)
        {
            data[1 + i * (1 + 16 + 1)] = (byte)i;
            Encoding.ASCII.GetBytes(GuildNames[i], data.Slice(2 + i * (1 + 16 + 1), 16));
            // Null terminator
            data[18 + i * (1 + 16 + 1)] = 0;
        }

        return data.ToArray();
    }
}