﻿using galaxy.core.communication.packets.outgoing.rooms.furni.wired;
using galaxy.core.habbolib.gameclients;
using galaxy.core.habbolib.items;
using galaxy.core.habbolib.items.wired;
using galaxy.core.habbolib.rooms;

namespace galaxy.core.communication.packets.incoming.rooms.furni.wired
{
    class SaveWiredConfigEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            if (Session == null || Session.GetHabbo() == null)
                return;

            if (!Session.GetHabbo().isInRoom)
                return;

            Room Room = Session.GetHabbo().currentRoom;
            if (Room == null || !Room.CheckRights(Session, false, true))
                return;

            int ItemId = Packet.PopInt();

            Session.SendPacket(new HideWiredConfigComposer());

            Item SelectedItem = Room.getRoomItemHandler().GetItem(ItemId);
            if (SelectedItem == null)
                return;

            IWiredItem Box = null;
            if (!Session.GetHabbo().currentRoom.GetWired().TryGet(ItemId, out Box))
                return;

            if (Box.Type == WiredBoxType.EffectGiveUserBadge && !Session.GetHabbo().GetPermissions().HasRight("room_item_wired_rewards"))
            {
                Session.SendNotification("You don't have the correct permissions to do this.");
                return;
            }

            Box.HandleSave(Packet);
            Session.GetHabbo().currentRoom.GetWired().SaveBox(Box);
        }
    }
}
