using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSO2SERVER.Models;
using System.Runtime.InteropServices;
using System.IO;
using PSO2SERVER.Json;
using PSO2SERVER.Database;

namespace PSO2SERVER.Protocol.Packets
{
    public class QuestAvailablePacket : Packet
    {
        public ushort Unk1 { get; set; }
        public ushort ExtremeCount { get; set; }
        public ushort Unk2 { get; set; }
        public ushort ArksCount { get; set; }
        public ushort LimitedTimeCount { get; set; }
        public ushort ExtremeDebugCount { get; set; }
        public ushort Blank1Count { get; set; }
        public ushort Unk3 { get; set; }
        public ushort NetCafeCount { get; set; }
        public ushort WarmingDebugCount { get; set; }
        public ushort Blank2Count { get; set; }
        public ushort AdvanceCount { get; set; }
        public ushort ExpeditionCount { get; set; }
        public ushort ExpeditionDebugCount { get; set; }
        public ushort ArksDebugCount { get; set; }
        public ushort Unk4Count { get; set; }
        public ushort ChallengeCount { get; set; }
        public ushort UrgentCount { get; set; }
        public ushort UrgentDebugCount { get; set; }
        public ushort TimeAttackCount { get; set; }
        public ushort TimeAttackDebugCount { get; set; }

        // Array of 9 ushort values (arks_debug2_count)
        public ushort[] ArksDebug2Count { get; set; } = new ushort[9];

        public ushort Blank3Count { get; set; }
        public ushort Unk5 { get; set; }
        public ushort RecommendedCount { get; set; }
        public ushort Unk6 { get; set; }
        public ushort UltimateDebugCount { get; set; }
        public ushort AgpCount { get; set; }
        public ushort BonusCount { get; set; }
        public ushort Unk7 { get; set; }

        // Array of 10 ushort values (training_count)
        public ushort[] TrainingCount { get; set; } = new ushort[10];

        public ushort TriggerCount { get; set; }
        public ushort RidroidCount { get; set; }
        public ushort NetCafeAgpCount { get; set; }
        public ushort BattleBrokenCount { get; set; }
        public ushort BusterDebugCount { get; set; }
        public ushort Poka12Count { get; set; }
        public ushort Unk8 { get; set; }
        public ushort Unk9 { get; set; }
        public ushort BusterCount { get; set; }
        public ushort HeroTrainingCount { get; set; }
        public ushort AmplifiedCount { get; set; }
        public ushort Unk10 { get; set; }
        public ushort Unk11 { get; set; }
        public ushort DarkBlastTrainingCount { get; set; }
        public ushort EndlessCount { get; set; }
        public ushort Unk12 { get; set; }
        public ushort Unk13 { get; set; }
        public ushort PhantomTrainingCount { get; set; }
        public ushort AisTrainingCount { get; set; }
        public ushort Unk14 { get; set; }
        public ushort DamageCalcCount { get; set; }
        public ushort EtoileTrainingCount { get; set; }
        public ushort DivideCount { get; set; }

        // Unsure fields (stars1_count, stars2_count, stars3_count)
        public ushort Stars1Count { get; set; }
        public ushort Stars2Count { get; set; }
        public ushort Stars3Count { get; set; }

        // Array of 2 ushort values (unk15)
        public ushort[] Unk15 { get; set; } = new ushort[2];

        // NotOn attribute is specific to certain frameworks (e.g., Vita platform)
        // You can use attributes like [Obsolete] in C# if needed
        public ushort[] Unk16 { get; set; } = new ushort[2];

        // AvailableQuestType values
        public AvailableQuestType AvailableTypes { get; set; }
        public AvailableQuestType Unk19 { get; set; }

        // Round boost active flag
        public uint RoundBoost { get; set; }
        public uint Unk21 { get; set; }

        // Constructor
        public QuestAvailablePacket()
        {
        }

        public QuestAvailablePacket(List<QuestData> quests)
        {
            var amount = new QuestAvailablePacket();

            // 定义一个字典，映射 QuestType 到相应的更新方法
            var questActions = new Dictionary<QuestType, Action<QuestAvailablePacket>>
            {
            { QuestType.Unk0, packet => { Unk1++; AvailableTypes |= AvailableQuestType.UNK1; } },
            { QuestType.Extreme, packet => { ExtremeCount++;  AvailableTypes |= AvailableQuestType.EXTREME; } },
            { QuestType.ARKS, packet => { ArksCount++;  AvailableTypes |= AvailableQuestType.ARKS; } },
            { QuestType.LimitedTime, packet => { LimitedTimeCount++;  AvailableTypes |= AvailableQuestType.LIMITED_TIME; } },
            { QuestType.ExtremeDebug, packet => { ExtremeDebugCount++;  AvailableTypes |= AvailableQuestType.EXTREME_DEBUG; } },
            { QuestType.Blank1, packet => { Blank1Count++;  AvailableTypes |= AvailableQuestType.BLANK1; } },
            { QuestType.NetCafe, packet => { NetCafeCount++;  AvailableTypes |= AvailableQuestType.NET_CAFE; } },
            { QuestType.WarmingDebug, packet => { WarmingDebugCount++;  AvailableTypes |= AvailableQuestType.WARMING_DEBUG; } },
            { QuestType.Blank2, packet => { Blank2Count++;  AvailableTypes |= AvailableQuestType.BLANK2; } },
            { QuestType.Advance, packet => { AdvanceCount++;  AvailableTypes |= AvailableQuestType.ADVANCE; } },
            { QuestType.Expedition, packet => { ExpeditionCount++; AvailableTypes |= AvailableQuestType.EXPEDITION;  } },
            { QuestType.FreeDebug, packet => { ExpeditionDebugCount++; AvailableTypes |= AvailableQuestType.FREE_DEBUG; } },
            { QuestType.ArksDebug, packet => { ArksDebugCount++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG; } },
            { QuestType.Challenge, packet => { ChallengeCount++; AvailableTypes |= AvailableQuestType.CHALLENGE; } },
            { QuestType.Urgent, packet => { UrgentCount++; AvailableTypes |= AvailableQuestType.URGENT; } },
            { QuestType.UrgentDebug, packet => { UrgentDebugCount++; AvailableTypes |= AvailableQuestType.URGENT_DEBUG; } },
            { QuestType.TimeAttack, packet => { TimeAttackCount++; AvailableTypes |= AvailableQuestType.TIME_ATTACK; } },
            { QuestType.TimeDebug, packet => { TimeAttackDebugCount++; AvailableTypes |= AvailableQuestType.TIME_DEBUG; } },
            { QuestType.ArksDebug2, packet => { ArksDebug2Count[0]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG2; } },
            { QuestType.ArksDebug3, packet => { ArksDebug2Count[1]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG3; } },
            { QuestType.ArksDebug4, packet => { ArksDebug2Count[2]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG4; } },
            { QuestType.ArksDebug5, packet => { ArksDebug2Count[3]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG5; } },
            { QuestType.ArksDebug6, packet => { ArksDebug2Count[4]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG6; } },
            { QuestType.ArksDebug7, packet => { ArksDebug2Count[5]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG7; } },
            { QuestType.ArksDebug8, packet => { ArksDebug2Count[6]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG8; } },
            { QuestType.ArksDebug9, packet => { ArksDebug2Count[7]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG9; } },
            { QuestType.ArksDebug10, packet => { ArksDebug2Count[8]++; AvailableTypes |= AvailableQuestType.ARKS_DEBUG10; } },
            { QuestType.Blank3, packet => { Blank3Count++;  AvailableTypes |= AvailableQuestType.BLANK3; } },
            { QuestType.Ultimate, packet => { Unk6++;  AvailableTypes |= AvailableQuestType.ULTIMATE; } },
            { QuestType.UltimateDebug, packet => { UltimateDebugCount++;  AvailableTypes |= AvailableQuestType.ULTIMATE_DEBUG; } },
            { QuestType.AGP, packet => { AgpCount++;  AvailableTypes |= AvailableQuestType.AGP; } },
            { QuestType.Bonus, packet => { BonusCount++;  AvailableTypes |= AvailableQuestType.BONUS; } },
            { QuestType.StandardTraining, packet => { TrainingCount[0]++;  AvailableTypes |= AvailableQuestType.STANDARD_TRAINING; } },
            { QuestType.HunterTraining, packet => { TrainingCount[1]++;  AvailableTypes |= AvailableQuestType.HUNTER_TRAINING; } },
            { QuestType.RangerTraining, packet => { TrainingCount[2]++;  AvailableTypes |= AvailableQuestType.RANGER_TRAINING; } },
            { QuestType.ForceTraining, packet => { TrainingCount[3]++;  AvailableTypes |= AvailableQuestType.FORCE_TRAINING; } },
            { QuestType.FighterTraining, packet => { TrainingCount[4]++;  AvailableTypes |= AvailableQuestType.FIGHTER_TRAINING; } },
            { QuestType.GunnerTraining, packet => { TrainingCount[5]++;  AvailableTypes |= AvailableQuestType.GUNNER_TRAINING; } },
            { QuestType.TechterTraining, packet => { TrainingCount[6]++;  AvailableTypes |= AvailableQuestType.TECHTER_TRAINING; } },
            { QuestType.BraverTraining, packet => { TrainingCount[7]++;  AvailableTypes |= AvailableQuestType.BRAVER_TRAINING; } },
            { QuestType.BouncerTraining, packet => { TrainingCount[8]++;  AvailableTypes |= AvailableQuestType.BOUNCER_TRAINING; } },
            { QuestType.SummonerTraining, packet => { TrainingCount[9]++;  AvailableTypes |= AvailableQuestType.SUMMONER_TRAINING; } },
            { QuestType.AutoAccept, packet => { AvailableTypes |= AvailableQuestType.AUTO_ACCEPT; } },
            { QuestType.Ridroid, packet => { RidroidCount++;  AvailableTypes |= AvailableQuestType.RIDROID; } },
            { QuestType.CafeAGP, packet => { NetCafeAgpCount++;  AvailableTypes |= AvailableQuestType.NET_CAFE_AGP; } },
            { QuestType.BattleBroken, packet => { BattleBrokenCount++;  AvailableTypes |= AvailableQuestType.BATTLE_BROKEN; } },
            { QuestType.BusterDebug, packet => { BusterDebugCount++;  AvailableTypes |= AvailableQuestType.BUSTER_DEBUG; } },
            { QuestType.Poka12, packet => { Poka12Count++;  AvailableTypes |= AvailableQuestType.POKA12; } },
            { QuestType.StoryEP1, packet => { Unk9++;  AvailableTypes |= AvailableQuestType.STORY_EP1; } },
            { QuestType.Buster, packet => { BusterCount++;  AvailableTypes |= AvailableQuestType.BUSTER; } },
            { QuestType.HeroTraining, packet => { HeroTrainingCount++;  AvailableTypes |= AvailableQuestType.HERO_TRAINING; } },
            { QuestType.Amplified, packet => { AmplifiedCount++;  AvailableTypes |= AvailableQuestType.AMPLIFIED; } },
            { QuestType.DarkBlastTraining, packet => { DarkBlastTrainingCount++;  AvailableTypes |= AvailableQuestType.DARK_BLAST_TRAINING; } },
            { QuestType.Endless, packet => { EndlessCount++;  AvailableTypes |= AvailableQuestType.ENDLESS; } },
            { QuestType.Blank4, packet => { Unk13++;  AvailableTypes |= AvailableQuestType.BLANK4; } },
            { QuestType.PhantomTraining, packet => { PhantomTrainingCount++;  AvailableTypes |= AvailableQuestType.PHANTOM_TRAINING; } },
            { QuestType.AISTraining, packet => { AisTrainingCount++;  AvailableTypes |= AvailableQuestType.AIS_TRAINING; } },
            { QuestType.DamageCalculation, packet => { DamageCalcCount++;  AvailableTypes |= AvailableQuestType.DAMAGE_CALC; } },
            { QuestType.EtoileTraining, packet => { EtoileTrainingCount++;  AvailableTypes |= AvailableQuestType.ETOILE_TRAINING; } },
            { QuestType.Divide, packet => { DivideCount++;  AvailableTypes |= AvailableQuestType.DIVIDE; } },
            { QuestType.Stars1, packet => { Stars1Count++;  AvailableTypes |= AvailableQuestType.STARS1; } },
            { QuestType.Stars2, packet => { Stars2Count++;  AvailableTypes |= AvailableQuestType.STARS2; } },
            { QuestType.Stars3, packet => { Stars3Count++;  AvailableTypes |= AvailableQuestType.STARS3; } },
            { QuestType.Stars4, packet => { Unk15[0]++;  AvailableTypes |= AvailableQuestType.STARS4; } },
            { QuestType.Stars5, packet => { Unk15[1]++;  AvailableTypes |= AvailableQuestType.STARS5; } },
            { QuestType.Stars6, packet => { Unk16[0]++;  AvailableTypes |= AvailableQuestType.STARS6; } },
            };

            // 处理每个 QuestData
            foreach (var quest in quests)
            {
                if (quest?.QuestDefiniton == null)
                {
                    Logger.WriteError("QuestAvailablePacket Load: Invalid quest definition.");
                    continue;
                }

                var questType = quest.QuestDefiniton.QuestType;

                if (questActions.TryGetValue(questType, out var action))
                {
                    try
                    {
                        action(amount);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteError($"Error processing quest type {questType}: {ex.Message}");
                    }
                }
                else
                {
                    Logger.WriteWarning($"No action defined for quest type {questType}");
                }
            }
        }

        public static QuestAvailablePacket Load(List<QuestData> quests)
        {
            var amount = new QuestAvailablePacket();

            // 定义一个字典，映射 QuestType 到相应的更新方法
            var questActions = new Dictionary<QuestType, Action<QuestAvailablePacket>>
            {
            { QuestType.Unk0, packet => { packet.Unk1++; packet.AvailableTypes |= AvailableQuestType.UNK1; } },
            { QuestType.Extreme, packet => { packet.ExtremeCount++;  packet.AvailableTypes |= AvailableQuestType.EXTREME; } },
            { QuestType.ARKS, packet => { packet.ArksCount++;  packet.AvailableTypes |= AvailableQuestType.ARKS; } },
            { QuestType.LimitedTime, packet => { packet.LimitedTimeCount++;  packet.AvailableTypes |= AvailableQuestType.LIMITED_TIME; } },
            { QuestType.ExtremeDebug, packet => { packet.ExtremeDebugCount++;  packet.AvailableTypes |= AvailableQuestType.EXTREME_DEBUG; } },
            { QuestType.Blank1, packet => { packet.Blank1Count++;  packet.AvailableTypes |= AvailableQuestType.BLANK1; } },
            { QuestType.NetCafe, packet => { packet.NetCafeCount++;  packet.AvailableTypes |= AvailableQuestType.NET_CAFE; } },
            { QuestType.WarmingDebug, packet => { packet.WarmingDebugCount++;  packet.AvailableTypes |= AvailableQuestType.WARMING_DEBUG; } },
            { QuestType.Blank2, packet => { packet.Blank2Count++;  packet.AvailableTypes |= AvailableQuestType.BLANK2; } },
            { QuestType.Advance, packet => { packet.AdvanceCount++;  packet.AvailableTypes |= AvailableQuestType.ADVANCE; } },
            { QuestType.Expedition, packet => { packet.ExpeditionCount++; packet.AvailableTypes |= AvailableQuestType.EXPEDITION;  } },
            { QuestType.FreeDebug, packet => { packet.ExpeditionDebugCount++; packet.AvailableTypes |= AvailableQuestType.FREE_DEBUG; } },
            { QuestType.ArksDebug, packet => { packet.ArksDebugCount++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG; } },
            { QuestType.Challenge, packet => { packet.ChallengeCount++; packet.AvailableTypes |= AvailableQuestType.CHALLENGE; } },
            { QuestType.Urgent, packet => { packet.UrgentCount++; packet.AvailableTypes |= AvailableQuestType.URGENT; } },
            { QuestType.UrgentDebug, packet => { packet.UrgentDebugCount++; packet.AvailableTypes |= AvailableQuestType.URGENT_DEBUG; } },
            { QuestType.TimeAttack, packet => { packet.TimeAttackCount++; packet.AvailableTypes |= AvailableQuestType.TIME_ATTACK; } },
            { QuestType.TimeDebug, packet => { packet.TimeAttackDebugCount++; packet.AvailableTypes |= AvailableQuestType.TIME_DEBUG; } },
            { QuestType.ArksDebug2, packet => { packet.ArksDebug2Count[0]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG2; } },
            { QuestType.ArksDebug3, packet => { packet.ArksDebug2Count[1]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG3; } },
            { QuestType.ArksDebug4, packet => { packet.ArksDebug2Count[2]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG4; } },
            { QuestType.ArksDebug5, packet => { packet.ArksDebug2Count[3]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG5; } },
            { QuestType.ArksDebug6, packet => { packet.ArksDebug2Count[4]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG6; } },
            { QuestType.ArksDebug7, packet => { packet.ArksDebug2Count[5]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG7; } },
            { QuestType.ArksDebug8, packet => { packet.ArksDebug2Count[6]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG8; } },
            { QuestType.ArksDebug9, packet => { packet.ArksDebug2Count[7]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG9; } },
            { QuestType.ArksDebug10, packet => { packet.ArksDebug2Count[8]++; packet.AvailableTypes |= AvailableQuestType.ARKS_DEBUG10; } },
            { QuestType.Blank3, packet => { packet.Blank3Count++;  packet.AvailableTypes |= AvailableQuestType.BLANK3; } },
            { QuestType.Ultimate, packet => { packet.Unk6++;  packet.AvailableTypes |= AvailableQuestType.ULTIMATE; } },
            { QuestType.UltimateDebug, packet => { packet.UltimateDebugCount++;  packet.AvailableTypes |= AvailableQuestType.ULTIMATE_DEBUG; } },
            { QuestType.AGP, packet => { packet.AgpCount++;  packet.AvailableTypes |= AvailableQuestType.AGP; } },
            { QuestType.Bonus, packet => { packet.BonusCount++;  packet.AvailableTypes |= AvailableQuestType.BONUS; } },
            { QuestType.StandardTraining, packet => { packet.TrainingCount[0]++;  packet.AvailableTypes |= AvailableQuestType.STANDARD_TRAINING; } },
            { QuestType.HunterTraining, packet => { packet.TrainingCount[1]++;  packet.AvailableTypes |= AvailableQuestType.HUNTER_TRAINING; } },
            { QuestType.RangerTraining, packet => { packet.TrainingCount[2]++;  packet.AvailableTypes |= AvailableQuestType.RANGER_TRAINING; } },
            { QuestType.ForceTraining, packet => { packet.TrainingCount[3]++;  packet.AvailableTypes |= AvailableQuestType.FORCE_TRAINING; } },
            { QuestType.FighterTraining, packet => { packet.TrainingCount[4]++;  packet.AvailableTypes |= AvailableQuestType.FIGHTER_TRAINING; } },
            { QuestType.GunnerTraining, packet => { packet.TrainingCount[5]++;  packet.AvailableTypes |= AvailableQuestType.GUNNER_TRAINING; } },
            { QuestType.TechterTraining, packet => { packet.TrainingCount[6]++;  packet.AvailableTypes |= AvailableQuestType.TECHTER_TRAINING; } },
            { QuestType.BraverTraining, packet => { packet.TrainingCount[7]++;  packet.AvailableTypes |= AvailableQuestType.BRAVER_TRAINING; } },
            { QuestType.BouncerTraining, packet => { packet.TrainingCount[8]++;  packet.AvailableTypes |= AvailableQuestType.BOUNCER_TRAINING; } },
            { QuestType.SummonerTraining, packet => { packet.TrainingCount[9]++;  packet.AvailableTypes |= AvailableQuestType.SUMMONER_TRAINING; } },
            { QuestType.AutoAccept, packet => { packet.AvailableTypes |= AvailableQuestType.AUTO_ACCEPT; } },
            { QuestType.Ridroid, packet => { packet.RidroidCount++;  packet.AvailableTypes |= AvailableQuestType.RIDROID; } },
            { QuestType.CafeAGP, packet => { packet.NetCafeAgpCount++;  packet.AvailableTypes |= AvailableQuestType.NET_CAFE_AGP; } },
            { QuestType.BattleBroken, packet => { packet.BattleBrokenCount++;  packet.AvailableTypes |= AvailableQuestType.BATTLE_BROKEN; } },
            { QuestType.BusterDebug, packet => { packet.BusterDebugCount++;  packet.AvailableTypes |= AvailableQuestType.BUSTER_DEBUG; } },
            { QuestType.Poka12, packet => { packet.Poka12Count++;  packet.AvailableTypes |= AvailableQuestType.POKA12; } },
            { QuestType.StoryEP1, packet => { packet.Unk9++;  packet.AvailableTypes |= AvailableQuestType.STORY_EP1; } },
            { QuestType.Buster, packet => { packet.BusterCount++;  packet.AvailableTypes |= AvailableQuestType.BUSTER; } },
            { QuestType.HeroTraining, packet => { packet.HeroTrainingCount++;  packet.AvailableTypes |= AvailableQuestType.HERO_TRAINING; } },
            { QuestType.Amplified, packet => { packet.AmplifiedCount++;  packet.AvailableTypes |= AvailableQuestType.AMPLIFIED; } },
            { QuestType.DarkBlastTraining, packet => { packet.DarkBlastTrainingCount++;  packet.AvailableTypes |= AvailableQuestType.DARK_BLAST_TRAINING; } },
            { QuestType.Endless, packet => { packet.EndlessCount++;  packet.AvailableTypes |= AvailableQuestType.ENDLESS; } },
            { QuestType.Blank4, packet => { packet.Unk13++;  packet.AvailableTypes |= AvailableQuestType.BLANK4; } },
            { QuestType.PhantomTraining, packet => { packet.PhantomTrainingCount++;  packet.AvailableTypes |= AvailableQuestType.PHANTOM_TRAINING; } },
            { QuestType.AISTraining, packet => { packet.AisTrainingCount++;  packet.AvailableTypes |= AvailableQuestType.AIS_TRAINING; } },
            { QuestType.DamageCalculation, packet => { packet.DamageCalcCount++;  packet.AvailableTypes |= AvailableQuestType.DAMAGE_CALC; } },
            { QuestType.EtoileTraining, packet => { packet.EtoileTrainingCount++;  packet.AvailableTypes |= AvailableQuestType.ETOILE_TRAINING; } },
            { QuestType.Divide, packet => { packet.DivideCount++;  packet.AvailableTypes |= AvailableQuestType.DIVIDE; } },
            { QuestType.Stars1, packet => { packet.Stars1Count++;  packet.AvailableTypes |= AvailableQuestType.STARS1; } },
            { QuestType.Stars2, packet => { packet.Stars2Count++;  packet.AvailableTypes |= AvailableQuestType.STARS2; } },
            { QuestType.Stars3, packet => { packet.Stars3Count++;  packet.AvailableTypes |= AvailableQuestType.STARS3; } },
            { QuestType.Stars4, packet => { packet.Unk15[0]++;  packet.AvailableTypes |= AvailableQuestType.STARS4; } },
            { QuestType.Stars5, packet => { packet.Unk15[1]++;  packet.AvailableTypes |= AvailableQuestType.STARS5; } },
            { QuestType.Stars6, packet => { packet.Unk16[0]++;  packet.AvailableTypes |= AvailableQuestType.STARS6; } },
            };

            // 遍历 quests 列表并执行相应的更新操作
            for (int i = 0; i < quests.Count; i++)
            {
                var quest = quests[i];

                // 确保 quest 和 QuestDefiniton 不为 null
                if (quest?.QuestDefiniton == null)
                {
                    Logger.WriteError($"队伍任务索引 {i} 无效 (QuestDefiniton 为空).");
                    continue;  // 跳过当前无效的 quest
                }

                // 获取 quest type
                var questType = quest.QuestDefiniton.QuestType;

                // 如果字典中包含该 quest type，则执行对应的操作
                if (questActions.TryGetValue(questType, out var action))
                {
                    try
                    {
                        action(amount);  // 执行更新操作
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteError($"Error executing action for QuestType {questType} at index {i}: {ex.Message}");
                    }
                }
                else
                {
                    // 可以在此处处理未定义的 QuestType（例如记录警告或错误）
                    Logger.WriteWarning($"未知 QuestType {questType} 索引 {i}. 跳过.");
                }
            }

            return amount;
        }

        //public short[] amount = new short[Enum.GetValues(typeof(QuestType)).Length];

        public override byte[] Build()
        {
            PacketWriter pkt = new PacketWriter();
            //// 使用反射获取所有枚举值，并合并它们
            //ulong combinedFlags = 0;

            //foreach (var value in Enum.GetValues(typeof(AvailableQuestType)))
            //{
            //    combinedFlags |= (ulong)value;
            //}

            //// Filler/Padding?
            //pkt.Write((UInt16)0);

            //// Amounts
            //for (int i = 0; i < amount.Length; i++)
            //{
            //    amount[i] = 1; // Just for testing
            //    pkt.Write(amount[i]);
            //}

            //// Padding/Blank entries?
            //for (int i = 0; i < 2; i++)
            //    pkt.Write((int)0);

            //// Available Bitfield
            //pkt.Write((UInt64)combinedFlags);

            //// Filler/Padding?
            //for (int i = 0; i < 2; i++)
            //    pkt.Write((int)0);

            //return pkt.ToArray();
            pkt.Write(Unk1);
            pkt.Write(ExtremeCount);
            pkt.Write(Unk2);
            pkt.Write(ArksCount);
            pkt.Write(LimitedTimeCount);
            pkt.Write(ExtremeDebugCount);
            pkt.Write(Blank1Count);
            pkt.Write(Unk3);
            pkt.Write(NetCafeCount);
            pkt.Write(WarmingDebugCount);
            pkt.Write(Blank2Count);
            pkt.Write(AdvanceCount);
            pkt.Write(ExpeditionCount);
            pkt.Write(ExpeditionDebugCount);
            pkt.Write(ArksDebugCount);
            pkt.Write(Unk4Count);
            pkt.Write(ChallengeCount);
            pkt.Write(UrgentCount);
            pkt.Write(UrgentDebugCount);
            pkt.Write(TimeAttackCount);
            pkt.Write(TimeAttackDebugCount);

            // 写入 ArksDebug2Count 数组
            foreach (var count in ArksDebug2Count)
            {
                pkt.Write(count);
            }

            pkt.Write(Blank3Count);
            pkt.Write(Unk5);
            pkt.Write(RecommendedCount);
            pkt.Write(Unk6);
            pkt.Write(UltimateDebugCount);
            pkt.Write(AgpCount);
            pkt.Write(BonusCount);
            pkt.Write(Unk7);

            // 写入 TrainingCount 数组
            foreach (var count in TrainingCount)
            {
                pkt.Write(count);
            }

            pkt.Write(TriggerCount);
            pkt.Write(RidroidCount);
            pkt.Write(NetCafeAgpCount);
            pkt.Write(BattleBrokenCount);
            pkt.Write(BusterDebugCount);
            pkt.Write(Poka12Count);
            pkt.Write(Unk8);
            pkt.Write(Unk9);
            pkt.Write(BusterCount);
            pkt.Write(HeroTrainingCount);
            pkt.Write(AmplifiedCount);
            pkt.Write(Unk10);
            pkt.Write(Unk11);
            pkt.Write(DarkBlastTrainingCount);
            pkt.Write(EndlessCount);
            pkt.Write(Unk12);
            pkt.Write(Unk13);
            pkt.Write(PhantomTrainingCount);
            pkt.Write(AisTrainingCount);
            pkt.Write(Unk14);
            pkt.Write(DamageCalcCount);
            pkt.Write(EtoileTrainingCount);
            pkt.Write(DivideCount);
            pkt.Write(Stars1Count);
            pkt.Write(Stars2Count);
            pkt.Write(Stars3Count);

            // 写入 Unk15 数组
            foreach (var count in Unk15)
            {
                pkt.Write(count);
            }

            // 写入 Unk16 数组
            foreach (var count in Unk16)
            {
                pkt.Write(count);
            }

            // 写入 AvailableTypes 和 Unk19 (作为枚举类型，转换为整数)
            pkt.Write((ulong)AvailableTypes);
            pkt.Write((ulong)Unk19);

            pkt.Write(RoundBoost);
            pkt.Write(Unk21);
            return pkt.ToArray();
        }

        public override PacketHeader GetHeader()
        {
            return new PacketHeader(0x0B, 0x16, PacketFlags.None);
        }

    }
}
