package net.anomalyxii.bot.impl.handlers;

import net.anomalyxii.bot.api.Bot;
import net.anomalyxii.bot.api.handlers.Command;
import net.anomalyxii.bot.api.server.Channel;
import net.anomalyxii.bot.api.server.Server;
import net.anomalyxii.bot.api.server.events.MessageEvent;

import java.util.List;
import java.util.Optional;

/**
 * Responds to {@code !!leave} {@link MessageEvent}.
 * <p>
 * Created by Anomaly on 15/04/17.
 */
public class LeaveChannelHandler extends AbstractCommandHandler {

    private static final String COMMAND = "!!leave";

    // *********************************
    // CommandHandler Methods
    // *********************************

    @Override
    protected String getCommand() {
        return COMMAND;
    }

    @Override
    public void handleCommand(Command command) throws Exception {
        Server server = command.getServer();
        String channel = command.getArguments().get(0);

        // Todo: authentication??
        Optional<Channel> chan = server.asServerContext().findOrDiscoverChannel(channel);
        server.leaveChannel(chan.orElseThrow(() -> new IllegalArgumentException("Cannot leave channel: not found")));
    }

}
