﻿using System;
using System.Data;
using galaxy.core.communication.packets.outgoing.marketplace;
using galaxy.core.database.interfaces;
using galaxy.core.habbolib.gameclients;

namespace galaxy.core.communication.packets.incoming.marketplace
{
    class GetMarketplaceItemStatsEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            int ItemId = Packet.PopInt();
            int spriteId = Packet.PopInt();

            DataRow Row = null;
            using (IQueryAdapter dbClient = GalaxyEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                dbClient.SetQuery("SELECT `avgprice` FROM `catalog_marketplace_data` WHERE `sprite` = @spriteId LIMIT 1");
                dbClient.AddParameter("spriteId", spriteId);
                Row = dbClient.GetRow();
            }

            Session.SendPacket(new MarketplaceItemStatsComposer(ItemId, spriteId, (Row != null ? Convert.ToInt32(Row["avgprice"]) : 0)));
        }
    }
}
