using Org.BouncyCastle.Bcpg;
using PSO2SERVER.Models;
using PSO2SERVER.Protocol.Packets;
using System.Collections.Generic;

namespace PSO2SERVER.Protocol.Handlers
{
    [PacketHandlerAttr(0x0B, 0x19)]
    public class QuestDifficultyRequestHandler : PacketHandler
    {
        /// List of objects of requested quests.
        public List<ObjectHeader> Quests { get; set; } = new List<ObjectHeader>();

        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);

            var questCount = reader.ReadMagic(0xA36E, 0x10);
            for (uint i = 0; i < questCount; i++)
            {
                var qobj = new ObjectHeader();
                qobj.ReadObjectHeaderFromStream(reader);
                Quests.Add(qobj);

                // 打印每个任务的 ID 和类型，方便调试
                Logger.Write($"任务 ID: {qobj.ID}, 任务类型: {qobj.ObjectType}");
            }

            QuestDifficulty[] diffs = new QuestDifficulty[1];
            for (int i = 0; i < diffs.Length; i++)
            {
                diffs[i].dateOrSomething = "2012/01/05";
                diffs[i].quest_obj = new ObjectHeader(0x20, ObjectType.Quest);
                diffs[i].name_id = 30010;
                diffs[i].area = 0x01;
                diffs[i].planet = 0x03;
                diffs[i].unk1 = 0x03;
                diffs[i].unk2 = 0x00;
                diffs[i].difficulty1 = new QuestDifficultyEntry();
                diffs[i].difficulty2 = new QuestDifficultyEntry();
                diffs[i].difficulty3 = new QuestDifficultyEntry();
                diffs[i].difficulty4 = new QuestDifficultyEntry();
                diffs[i].difficulty5 = new QuestDifficultyEntry();
                diffs[i].difficulty6 = new QuestDifficultyEntry();
                diffs[i].difficulty7 = new QuestDifficultyEntry();
                diffs[i].difficulty8 = new QuestDifficultyEntry();
            }
            context.SendPacket(new QuestDifficultyPacket(diffs));
            // [K873] I believe this is the correct packet, but it causes an infinite send/recieve loop, we're probably just missing something else
            context.SendPacket(new QuestDifficultySendFinishedPacket());


            //// 现在可以对任务列表（Quests）进行处理
            //// 例如，可以记录任务的信息或进行其他处理
            //foreach (var quest in Quests)
            //{
            //    if(quest.ObjectType == ObjectType.Quest)
            //    {
            //    }
            //}

        }
    }

}
