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.rooms.*;
import net.cobem.grizzly.habbohotel.rooms.Room;
import net.cobem.grizzly.habbohotel.sessions.Session;

public class FinishRoomLoadEvent implements GameEvent {
    @Override
    public void compose(Session Session, EventRequest Request) {
        Room mRoom = Session.getActor().CurrentRoom;

        Session.sendResponse(SendFloorItemsComposer.compose(mRoom.Owner, mRoom.OwnerByName, mRoom.getFloors()));
        Session.sendResponse(SendWallItemsComposer.compose(mRoom.Owner, mRoom.OwnerByName, mRoom.getWalls()));

        Session.sendResponse(SendNewRoomUserInformationComposer.compose(Session.getHabbo().getID(), Session.getHabbo().getUsername()));

        mRoom.sendMessage(mRoom.getActorStatus());
        mRoom.sendMessage(mRoom.getRoomStatus());

        mRoom.sendStatuses(Session);

        for (Integer ID : Session.getHabbo().getIgnored()) //Load current users in room that we're ignoring.
        {
            if (!mRoom.getParty().containsKey(ID)) {
                continue;
            }

            Session IgnoredUser = mRoom.getParty().get(ID);

            Session.getResponse().Initialize(2600);
            Session.getResponse().addInt(1);
            Session.getResponse().addString(IgnoredUser.getHabbo().getUsername());

            Session.sendResponse();
        }

        for (Session Ignore : mRoom.getParty().values()) {
            if (Session.getHabbo().getIgnorers().contains(Ignore.getHabbo().getID())) {
                EventResponse Message = new EventResponse();

                Message.Initialize(2600);
                Message.addInt(1);
                Message.addString(Session.getHabbo().getUsername());

                Ignore.sendResponse(Message);
            }
        }

        Session.sendResponse(SendRoomSettingsInterfaceComposer.compose(mRoom.ID));

        Session.sendResponse(SendMoreRoomInformationComposer.compose(mRoom));

        Session.sendResponse(SendRoomUserRightsComposer.compose(mRoom));

        for (int UserID : mRoom.getRightHolders()) {
            //Session.sendResponse(SendRoomPowerEvent.compose(mRoom.ID, UserHandler.generateUser(UserID)));
        }

        if (Grizzly.getConfig().get("grizzly.rooms.log").equals("1")) {
            Grizzly.write(Session.getHabbo().getUsername() + " entered room " + mRoom.ID);
        }

        Session.getActor().GoalPosition = null;

        mRoom.refreshRobots();

        mRoom.getMap().ReGenerateCollisionMap();

        mRoom.getWiredUtility().onUserEnter(Session);
    }
}
