package net.cobem.grizzly.communication.events.rooms;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventRequest;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.GameEvent;
import net.cobem.grizzly.communication.composers.inventory.UpdateUserInventoryComposer;
import net.cobem.grizzly.communication.composers.rooms.RemoveFloorItemComposer;
import net.cobem.grizzly.communication.composers.rooms.RemoveWallItemComposer;
import net.cobem.grizzly.communication.events.user.InitializeInventoryEvent;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.rooms.items.WallItem;
import net.cobem.grizzly.habbohotel.sessions.Session;

public class RemoveItemEvent implements GameEvent {

    @Override
    public void compose(Session Session, EventRequest Request) {
        if (Session.getActor().CurrentRoom.Owner != Session.getHabbo().getID()) {
            return;
        }

        Request.readInt(); //Clear out the first integer
        int ID = Request.readInt();

        EventResponse Message = new EventResponse();

        if (Session.getActor().CurrentRoom.getFloors().containsKey(ID)) {
            FloorItem Item = Session.getActor().CurrentRoom.getFloors().get(ID);

            Session.getActor().CurrentRoom.sendMessage(RemoveFloorItemComposer.compose(ID, Session.getHabbo().getID()));

            Session.getHabbo().getItems().add(Item.getBase(), false, Item.ExtraData);

            Grizzly.getStorage().execute("UPDATE items SET room_id = '0' WHERE id = '" + ID + "'");

            Session.getActor().CurrentRoom.getFloors().remove(ID);

            Session.getActor().CurrentRoom.getWiredUtility().removeFurni(Item);

        } else if (Session.getActor().CurrentRoom.getWalls().containsKey(ID)) {
            WallItem Item = Session.getActor().CurrentRoom.getWalls().get(ID);

            Session.getActor().CurrentRoom.sendMessage(RemoveWallItemComposer.compose(Item.ID, Integer.parseInt(Item.ExtraData)));

            Session.getHabbo().getItems().add(Item.getBase(), false, Item.ExtraData);

            Grizzly.getStorage().execute("UPDATE items SET room_id = '0' WHERE id = '" + ID + "'");

            Session.getActor().CurrentRoom.getWalls().remove(ID);
        }

        Session.sendResponse(UpdateUserInventoryComposer.compose());

        (new InitializeInventoryEvent()).compose(Session, Request);

        Session.getActor().CurrentRoom.getMap().ReGenerateCollisionMap();

        //Session.getActor().CurrentRoom.sendMessage(RemoveRoomItemComposer.compose(ID));
    }

}
