using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSO2SERVER.Models;
using PSO2SERVER.Party;
using PSO2SERVER.Protocol.Packets;
using PSO2SERVER.Zone;

namespace PSO2SERVER.Protocol.Handlers
{
    [PacketHandlerAttr(0x04, 0x75)]
    public class ActionEnd : PacketHandler
    {
        public ObjectHeader unk1 { get; set; } = new ObjectHeader();
        /// Object that was performing an action.
        public ObjectHeader performer { get; set; } = new ObjectHeader();
        public uint unk2 { get; set; } = 0;
        public ObjectHeader unk3 { get; set; } = new ObjectHeader();
        public ObjectHeader unk4 { get; set; } = new ObjectHeader();
        public byte[] unk5 { get; set; } = new byte[0x04];
        public string action { get; set; } = string.Empty;//Ascii

        public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
        {
            var info = string.Format("[<--] 接收到的数据 (hex): {0} 字节", data.Length);
            Logger.WriteHex(info, data);

            var reader = new PacketReader(data, position, size);
            unk1 = reader.ReadStruct<ObjectHeader>();
            performer = reader.ReadStruct<ObjectHeader>();
            unk2 = reader.ReadUInt32();
            unk3 = reader.ReadStruct<ObjectHeader>();
            unk4 = reader.ReadStruct<ObjectHeader>();
            unk5 = reader.ReadBytes(4);
            action = reader.ReadAscii(0x83EF, 0x40);
            // 打印各个字段的内容
            Logger.Write("unk1: " + unk1); // 假设 ObjectHeader 重载了 ToString() 方法
            Logger.Write("performer: " + performer); // 同上
            Logger.Write("unk2: " + unk2);
            Logger.Write("unk3: " + unk3);
            Logger.Write("unk4: " + unk4);
            Logger.Write("unk5: " + BitConverter.ToString(unk5)); // 打印 byte 数组
            Logger.Write("action: " + action); // 打印 ASCII 字符串

            foreach (Client cl in PartyManager.instance.GetCurrentPartyForClient(context).getMembers())
            {
                cl.SendPacket(new ActionEndPacket(unk1, performer, unk2, unk3, unk4, unk5, action));
            }
        }
    }
}
