using PSO2SERVER.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSO2SERVER.Protocol.Packets
{
    public class MailListPacket : Packet
    {
        // Unk1 to Unk6 fields - Assuming these are some form of metadata
        public ushort Unk1 { get; set; } = 0;
        public ushort Unk2 { get; set; } = 0;
        public ushort Unk3 { get; set; } = 0;
        public ushort Unk4 { get; set; } = 0;
        public byte[] Unk5 { get; set; } = new byte[4]; // byte[4] for unk5
        public uint Unk6 { get; set; } = 0;

        // Player name (?)
        public string Name { get; set; } = string.Empty;

        // Character name (?)
        public string Nickname { get; set; } = string.Empty;

        // Mail headers (List of MailHeader objects)
        public List<MailHeader> Headers { get; set; } = new List<MailHeader>();

        // Default constructor
        public MailListPacket()
        {
            // Initialize properties with default values
            Unk5 = new byte[4];  // assuming it's a byte array of size 4
            Headers = new List<MailHeader>();
        }

        // Custom constructor to initialize values
        public MailListPacket(ushort unk1, ushort unk2, ushort unk3, ushort unk4, byte[] unk5, uint unk6, string name, string nickname, List<MailHeader> headers)
        {
            Unk1 = unk1;
            Unk2 = unk2;
            Unk3 = unk3;
            Unk4 = unk4;
            Unk5 = unk5;
            Unk6 = unk6;
            Name = name;
            Nickname = nickname;
            Headers = headers;
        }

        #region implemented abstract members of Packet

        public override byte[] Build()
        {
            var pkt = new PacketWriter();
            pkt.Write(Unk1);
            pkt.Write(Unk2);
            pkt.Write(Unk3);
            pkt.Write(Unk4);
            pkt.Write(Unk5);
            pkt.WriteUtf16(Name, 0x36A1, 0xBF);
            pkt.WriteUtf16(Nickname, 0x36A1, 0xBF);
            pkt.WriteMagic(Headers.Count, 0x36A1, 0xBF);
            foreach (var header in Headers) { pkt.WriteStruct(header); }
            return pkt.ToArray();
        }

        public override PacketHeader GetHeader()
        {
            return new PacketHeader(0x1A, 0x01, PacketFlags.PACKED);
        }

        #endregion
    }
}