using System;
using PSO2SERVER.Models;
using PSO2SERVER.Protocol.Packets;
using PSO2SERVER.Party;
using PSO2SERVER.Json;
using System.Collections.Generic;

namespace PSO2SERVER.Protocol.Handlers
{
    [PacketHandlerAttr(0x0E, 0x0C)]
    public class NewPartySettings : PacketHandler
    {
        public string name { get; set; } = string.Empty;
        public string password { get; set; } = string.Empty;
        public string comments { get; set; } = string.Empty;
        public string questname { get; set; } = string.Empty;
        public byte min_level { get; set; } = 0;
        public byte max_level { get; set; } = 0;
        public byte playstyle { get; set; } = 0;
        public PartyFlags partyFlags { get; set; } = 0;
        public ulong unk { get; set; } = 0;

        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);
            name = reader.ReadUtf16(0x11CB, 0x98);
            password = reader.ReadUtf16(0x11CB, 0x98);
            comments = reader.ReadUtf16(0x11CB, 0x98);
            questname = reader.ReadUtf16(0x11CB, 0x98);
            min_level = reader.ReadByte();
            max_level = reader.ReadByte();
            playstyle = reader.ReadByte();
            partyFlags = (PartyFlags)reader.ReadByte();
            unk = reader.ReadUInt64();
            // 打印输出
            Logger.Write($"name: {name}");
            Logger.Write($"Password: {password}");
            Logger.Write($"Comments: {comments}");
            Logger.Write($"PartyQuest name: {questname}");
            Logger.Write($"Min Level: {min_level}");
            Logger.Write($"Max Level: {max_level}");
            Logger.Write($"Playstyle: {playstyle}");
            Logger.Write($"Party Flags: {partyFlags}");  // 如果 PartyFlags 是枚举类型
            Logger.Write($"Unknown Value: {unk}");

            context.currentParty.partySetting = new PartySettingsPacket
            {
                name = name,
                password = password,
                comments = comments,
                min_level = min_level,
                max_level = max_level,
                playstyle = playstyle,
                flags = partyFlags,
                unk = unk,
            };

            context.SendPacket(new PartySettingsPacket(context.currentParty.partySetting));

            if (questname != "")
            {

                context.currentParty.questname = questname;

                //TODO questname
                string jsonFilePath4 = "data\\quests\\Story Quests\\EP1\\700000 - An Encounter with Xion\\data.json";

                var quest = JsonRead.DeserializeJson<QuestData>(jsonFilePath4);

                var quests = new List<QuestData>();

                quests.Add(quest);

                context.currentParty.currentQuest.Quest = quest;
                context.SendPacket(new SetQuestInfoPacket(quest.QuestDefiniton, 0, (uint)context._account.AccountId));
                context.SendPacket(new PartySetQuestPacket(0x753A, 0, quest, (uint)context._account.AccountId));
            }

        }
    }
}
