﻿using galaxy.core.communication.packets.outgoing.catalog;
using galaxy.core.habbolib.catalog;
using galaxy.core.habbolib.gameclients;

namespace galaxy.core.communication.packets.incoming.catalog
{
    class GetCatalogOfferEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            int offerID = Packet.PopInt();
            if (!GalaxyEnvironment.GetGame().getCatalogManager().ItemOffers.ContainsKey(offerID))
                return;

            int PageId = GalaxyEnvironment.GetGame().getCatalogManager().ItemOffers[offerID];

            CatalogPage Page;
            if (!GalaxyEnvironment.GetGame().getCatalogManager().TryGetPage(PageId, out Page))
                return;

            if (!Page.Enabled || !Page.Visible || Page.MinimumRank > Session.GetHabbo().Rank || (Page.MinimumVIP > Session.GetHabbo().VIPRank && Session.GetHabbo().Rank == 1))
                return;

            CatalogItem Item = null;
            if (!Page.ItemOffers.ContainsKey(offerID))
                return;

            Item = (CatalogItem)Page.ItemOffers[offerID];
            if (Item != null)
                Session.SendPacket(new CatalogOfferComposer(Item));
        }
    }
}
