﻿using galaxy.core.communication.packets.outgoing.quests;
using galaxy.core.database.interfaces;
using galaxy.core.habbolib.gameclients;
using galaxy.core.habbolib.quests;

namespace galaxy.core.communication.packets.incoming.quests
{
    class StartQuestEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            int QuestId = Packet.PopInt();

            Quest Quest = GalaxyEnvironment.GetGame().GetQuestManager().GetQuest(QuestId);
            if (Quest == null)
                return;

            using (IQueryAdapter dbClient = GalaxyEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                dbClient.RunQuery("REPLACE INTO `user_quests` (`user_id`,`quest_id`) VALUES ('" + Session.GetHabbo().Id + "', '" + Quest.Id + "')");
                dbClient.RunQuery("UPDATE `user_stats` SET `quest_id` = '" + Quest.Id + "' WHERE `id` = '" + Session.GetHabbo().Id + "' LIMIT 1");
            }

            Session.GetHabbo().getStats().QuestID = Quest.Id;
            GalaxyEnvironment.GetGame().GetQuestManager().GetList(Session, null);
            Session.SendPacket(new QuestStartedComposer(Session, Quest));
        }
    }
}
