﻿using System.Collections.Generic;
using galaxy.core.habbolib.achievements;
using galaxy.core.habbolib.gameclients;
using galaxy.core.util;

namespace galaxy.core.communication.packets.outgoing.inventory.achievements
{
    class AchievementsComposer : ServerPacket
    {
        public AchievementsComposer(GameClient Session, List<Achievement> Achievements)
            : base(ServerPacketHeader.AchievementsMessageComposer)
        {
            base.WriteInteger(Achievements.Count);
            foreach (Achievement Achievement in Achievements)
            {
                UserAchievement UserData = Session.GetHabbo().getAchievementData(Achievement.groupName);
                int TargetLevel = UserData?.level + 1 ?? 1;
                int TotalLevels = Achievement.levels.Count;

                TargetLevel = (TargetLevel > TotalLevels ? TotalLevels : TargetLevel);

                AchievementLevel TargetLevelData = Achievement.levels[TargetLevel];
                base.WriteInteger(Achievement.id); // Unknown (ID?)
                base.WriteInteger(TargetLevel); // Target level
               base.WriteString(Achievement.groupName.getAttributeString() + TargetLevel); // Target name/desc/badge

                base.WriteInteger(1);
                base.WriteInteger(TargetLevelData.requirement); // Progress req/target          
                base.WriteInteger(TargetLevelData.rewardDuckets);

                base.WriteInteger(0); // Type of reward
                base.WriteInteger(UserData?.progress ?? 0); // Current progress
                
                base.WriteBoolean(UserData != null && (UserData.level >= TotalLevels));// Set 100% completed(??)
               base.WriteString(Achievement.category.getAttributeString()); // Category
               base.WriteString(string.Empty);
                base.WriteInteger(TotalLevels); // Total amount of levels 
                base.WriteInteger(0);
            }
           base.WriteString("");
        }
    }
}