package net.cobem.grizzly.habbohotel.misc.commands;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.composers.misc.SendNotificationComposer;
import net.cobem.grizzly.habbohotel.misc.ChatCommand;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.User;
import net.cobem.grizzly.habbohotel.users.UserDAO;

import java.util.Date;

public class Ban implements ChatCommand {
    @Override
    public int rank() {
        return 5;
    }

    @Override
    public void execute(Session session, String[] args) {
        if (args.length < 2) {
            session.sendResponse(SendNotificationComposer.compose("Invalid number of arguments!"));
            return;
        }

        User target = UserDAO.generateUser(args[0]);

        if (target == null) {
            session.sendResponse(SendNotificationComposer.compose("Cannot find that user!"));
            return;
        }

        int minutes = new Integer(args[1]);

        if (minutes <= 0) {
            session.sendResponse(SendNotificationComposer.compose("Invalid number of minutes for ban!"));
            return;
        }

        if (target.getRank() >= 4) {
            session.sendResponse(SendNotificationComposer.compose("You cannot ban a staff member, de-rank them first!"));
            return;
        }

        Date whatever = new Date();

        whatever.setTime(whatever.getTime() + (minutes * 60));

        UserDAO.banUser(target.getID(), whatever);

        if (Grizzly.getHabboHotel().getSessions().getByID(target.getID()) != null) {
            Grizzly.getHabboHotel().getSessions().getByID(target.getID()).getChannel().disconnect();
        }
    }
}
