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

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventRequest;
import net.cobem.grizzly.communication.GameEvent;
import net.cobem.grizzly.communication.composers.messenger.SendFriendListComposer;
import net.cobem.grizzly.communication.composers.messenger.SendPendingFriendRequestsComposer;
import net.cobem.grizzly.communication.composers.moderation.InitializeModToolsComposer;
import net.cobem.grizzly.communication.composers.user.*;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.UserDAO;

import java.text.SimpleDateFormat;
import java.util.Date;

public class LoadUserDataEvent implements GameEvent {
    @Override
    public void compose(Session Session, EventRequest Request) {
        if (!Session.getChannel().isWritable()) {
            Session.getChannel().disconnect();
            return;
        }

        if (UserDAO.isBanned(Session.getHabbo().getID())) {
            Session.getChannel().disconnect();
            return;
        }

        if (Session.getHabbo().getRank() >= 5) {
            Session.sendResponse(InitializeModToolsComposer.compose());
        }

        if (Session.getHabbo().getHome() > 0) {
            //Session.sendResponse(SendToHomeRoomEvent.compose(Session.getHabbo().getHome()));
        }

        Session.sendResponse(SendClubRankComposer.compose(Session.getHabbo().getRank()));
        Session.sendResponse(SendUserInformationComposer.compose(Session));
        Session.sendResponse(SendHotelViewWelcomeComposer.compose());
        Session.sendResponse(SendAllowancesComposer.compose(Session.getHabbo().hasBadge(Grizzly.getConfig().get("habbo.quiz.safetyquiz1.reward").trim())));

        Session.getHabbo().startMessenger();

        Session.sendResponse(SendCurrenciesComposer.compose(Session.getHabbo().getPixels(), Session.getHabbo().getCurrency()));
        Session.sendResponse(SendCreditsComposer.compose(Session.getHabbo().getCredits()));
        Session.sendResponse(SendClubComposer.compose(100));
        Session.sendResponse(SendFriendListComposer.compose(Session.getHabbo().getRank(), Session.getHabbo().getMessenger()));
        Session.sendResponse(SendPendingFriendRequestsComposer.compose(Session.getHabbo().getMessenger()));
        Session.sendResponse(SendBadgesComposer.compose(Session.getHabbo().getBadges(), Session.getHabbo().getEquippedBadges()));

        Session.getHabbo().refreshMessenger(true);

        if (!Session.getHabbo().hasBadge(Grizzly.getConfig().get("habbo.quiz.safetyquiz1.reward"))) //They haven't completed the safety quiz..
        {
            Session.sendResponse(SendCitizenshipComposer.compose());
        }

        Grizzly.getStorage().execute("UPDATE users SET online = '1', last_online = '" + new SimpleDateFormat("M-d-yyyy").format(new Date()) + "' WHERE id = '" + Session.getHabbo().getID() + "'");

    }
}

