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

namespace PSO2SERVER.Protocol.Packets
{
    public class AddMemberPacket : Packet
    {
        // New player object.
        public ObjectHeader NewMember { get; set; }

        // Party color of the player.
        public PartyColor Color { get; set; }

        // Level of the main class.
        public uint Level { get; set; }

        // Level of the subclass.
        public uint Sublevel { get; set; }

        // Class of the player.
        public ClassType PlayerClass { get; set; }

        // Subclass of the player.
        public ClassType SubClass { get; set; }

        // Padding (3 bytes).
        public byte[] Padding { get; set; } = new byte[3];

        // Nickname of the player.
        public string Nickname { get; set; }

        // Name of the character.
        public string CharName { get; set; }

        // Unk5 (12 bytes).
        public byte[] Unk5 { get; set; } = new byte[0xC];

        // Unk6 (16-bit).
        public ushort Unk6 { get; set; }

        // Unk7 (2 bytes).
        public byte[] Unk7 { get; set; } = new byte[2];

        // HP of the player (3 values).
        public uint[] Hp { get; set; } = new uint[3];

        // Map ID where the player is located.
        public ushort MapId { get; set; }

        // Unk10 (4 bytes).
        public byte[] Unk10 { get; set; } = new byte[4];

        // Unk11 (16-bit).
        public ushort Unk11 { get; set; }

        // Unk12 (32-bit).
        public uint Unk12 { get; set; }

        // Unk13 (12 bytes).
        public byte[] Unk13 { get; set; } = new byte[0xC];

        // Unk14 (3 values of uint).
        public uint[] Unk14 { get; set; } = new uint[3];

        // Unk15 (string).
        public string Unk15 { get; set; }

        // Unk16 (AsciiString).
        public string Unk16 { get; set; }

        // NGS Packet Only (if applicable).
        public string Unk17 { get; set; } // Only if feature is enabled

        // Constructor
        public AddMemberPacket()
        {
            Padding = new byte[3];
            Unk5 = new byte[0xC];
            Unk7 = new byte[2];
            Hp = new uint[3];
            Unk10 = new byte[4];
            Unk13 = new byte[0xC];
            Unk14 = new uint[3];
        }

        public AddMemberPacket(Client client)
        {
            NewMember = new ObjectHeader((uint)client._account.AccountId, ObjectType.Player);
            Color = client.currentParty.AddColor(0);
            var character = client.Character;
            Level = character.Jobs.entries.hunter.level;
            Sublevel = character.Jobs.entries.ranger.level;
            PlayerClass = character.Jobs.mainClass;
            SubClass = character.Jobs.subClass;
            Nickname = client._account.Nickname;
            CharName = character.Name;
            MapId = (ushort)client.CurrentMap.MapID;
        }

        public AddMemberPacket(PartyInitPacket pkt)
        {
            NewMember = pkt.leader;
            Color = PartyColor.Red;
        }

        #region implemented abstract members of Packet

        public override byte[] Build()
        {
            var pkt = new PacketWriter();
            pkt.WriteObjectHeader(NewMember);
            pkt.Write((byte)Color);
            pkt.Write(Level);
            pkt.Write(Sublevel);
            pkt.WriteEnum(PlayerClass);
            pkt.WriteEnum(SubClass);
            pkt.Write(Padding);
            pkt.WriteUtf16(Nickname, 0xCCE7, 0x13);
            pkt.WriteUtf16(CharName, 0xCCE7, 0x13);
            pkt.Write(Unk5);
            pkt.Write(Unk6);
            pkt.Write(Unk7);
            pkt.WriteIntArray(Hp);
            pkt.Write(MapId);
            pkt.Write(Unk10);
            pkt.Write(Unk11);
            pkt.Write(Unk12);
            pkt.Write(Unk13);
            pkt.WriteIntArray(Unk14);
            pkt.WriteUtf16(Unk15, 0xCCE7, 0x13);
            pkt.WriteAscii(Unk16, 0xCCE7, 0x13);
            //NGS
            //pkt.WriteAscii(Unk17, 0xCCE7, 0x13);

            return pkt.ToArray();
        }

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

        #endregion
    }
}