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

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventRequest;
import net.cobem.grizzly.communication.GameEvent;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.User;
import net.cobem.grizzly.habbohotel.users.UserDAO;

public class AcceptFriendRequestEvent implements GameEvent {
    @Override
    public void compose(Session Session, EventRequest Request) {
        int Count = Request.readInt();

        for (int i = 0; i < Count; i++) {
            User mUser = UserDAO.generateUser(Request.readInt());

            Grizzly.getStorage().execute(
                    "INSERT INTO messenger_friendships (user_one_id, user_two_id) VALUES ('" + Session.getHabbo().getID() +
                            "', '" + mUser.getID() + "')");

            Grizzly.getStorage().execute("DELETE FROM messenger_requests WHERE from_id = '" + mUser.getID() +
                    "' AND to_id = '" + Session.getHabbo().getID() + "'");

            Grizzly.getHabboHotel().getStream().addFriend(Session, mUser.getID());

            if ((Grizzly.getHabboHotel().getSessions().getByID(mUser.getID()) != null)) {
                try {
                    mUser.refreshMessenger(true);

                    Session.getHabbo().refreshMessenger(true);
                } catch (Exception ex) {
                }
            }
        }
    }
}
