using PSO2SERVER.Database;
using PSO2SERVER.Models;
using PSO2SERVER.Party;
using System.Collections.Generic;

namespace PSO2SERVER.Protocol.Packets
{
    public class PartyInitPacket : Packet
    {
        /// 队伍实例.
        public ObjectHeader party_object;
        /// 队伍队长实例.
        public ObjectHeader leader;
        /// 队伍人数.
        public uint people_amount;
        /// 队伍成员.
        public PartyEntry[] entries = new PartyEntry[4]; // 这个是一个包含 4 个 `PartyEntry` 结构的数组，表示队伍中的4个成员。
        /// <summary>
        /// 未知
        /// </summary>
        public string unk2 = string.Empty;

        private List<Client> Clients = new List<Client>();
        public PartyInitPacket(List<Client> clients)
        {
            this.Clients = clients;

            party_object = new ObjectHeader((uint)clients[0]._account.AccountId, ObjectType.Party);
            leader = new ObjectHeader((uint)clients[0]._account.AccountId, ObjectType.Player);
            people_amount = (uint)Clients.Count;

            // 队伍结构数据
            for (int i = 0; i < Clients.Count; i++)
            {
                entries[i].id = new ObjectHeader((uint)Clients[i]._account.AccountId, ObjectType.Player); // player of player
                entries[i].nickname = Clients[i]._account.Nickname;
                var character = Clients[i].Character;
                entries[i].char_name = character.Name;
                entries[i].level = (byte)character.Jobs.entries.hunter.level;
                entries[i].sublevel = (byte)character.Jobs.entries.hunter.level2;
                entries[i].mainClass = character.Jobs.mainClass;
                entries[i].subClass = character.Jobs.subClass;
                entries[i].color = (PartyColor)i;
                entries[i].unk2 = 0;
                entries[i].mapid = 0;
                entries[i].unk3 = 0;
                entries[i].unk6 = character.Name;
            }
        }

        public override byte[] Build()
        {
            if (Clients.Count < 1)
                return new byte[0];

            // xor: 0xD863, sub: 0xA9
            PacketWriter pkt = new PacketWriter();
            pkt.WriteObjectHeader(party_object);
            pkt.WriteObjectHeader(leader); // Accounts receiving the thing
            pkt.Write(people_amount); // Likely partymembercount
            for (int i = 0; i < 4; i++)
            {
                pkt.WriteObjectHeader(entries[i].id);
                pkt.WriteUtf16(entries[i].nickname, 0xD863, 0xA9);
                pkt.WriteUtf16(entries[i].char_name, 0xD863, 0xA9);
                pkt.Write(entries[i].level); // Active class level
                pkt.Write(entries[i].sublevel); // idk
                pkt.Write((byte)entries[i].mainClass); // idk
                pkt.Write((byte)entries[i].subClass); // idk
                pkt.Write((byte)entries[i].color); // idk
                pkt.WriteBytes(0, 7);
                pkt.Write(entries[i].unk2); // idk
                ///hp
                pkt.Write((uint)0);
                pkt.Write((uint)0);
                pkt.Write((uint)0);
                /// 玩家所在的地图ID。
                pkt.Write(entries[i].mapid);
                pkt.Write(entries[i].unk3);
                pkt.WriteBytes(0, 12);
                pkt.Write((uint)0);
                pkt.Write((uint)0);
                pkt.WriteUtf16(entries[i].unk6, 0xD863, 0xA9);
            }

            pkt.WriteAscii(unk2, 0xD863, 0xA9);
            return pkt.ToArray();
        }

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