package org.royaldev.thehumanity.commands.impl.ping.subcommands;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.kitteh.irc.client.library.element.User;
import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent;
import org.kitteh.irc.client.library.util.Format;
import org.royaldev.thehumanity.TheHumanity;
import org.royaldev.thehumanity.commands.impl.ping.PingListSubcommand;
import org.royaldev.thehumanity.ping.PingRegistration;
import org.royaldev.thehumanity.ping.PingRegistrationOption;
import org.royaldev.thehumanity.ping.PingRegistry;
import xyz.cardstock.cardstock.commands.CallInfo;
import xyz.cardstock.cardstock.commands.Command;
import java.util.List;
import java.util.Optional;
@Command(
name = "options",
description = "Sets options for your registration.",
aliases = {"o", "option", "config", "configuration"}
)
public class OptionsSubcommand extends PingListSubcommand {
private final TheHumanity humanity;
public OptionsSubcommand(final TheHumanity humanity) {
this.humanity = humanity;
}
private void add(@NotNull final PingRegistry pr, @NotNull final User u, @NotNull final String accountName, @NotNull final List<String> args) {
Preconditions.checkNotNull(pr, "pr was null");
Preconditions.checkNotNull(u, "u was null");
Preconditions.checkNotNull(accountName, "accountName was null");
Preconditions.checkNotNull(args, "args was null");
if (args.size() < 1) {
u.sendNotice("You must provide an option to add.");
return;
}
final PingRegistrationOption pro;
try {
pro = PingRegistrationOption.valueOf(args.get(0).toUpperCase());
} catch (final IllegalArgumentException ex) {
u.sendNotice("Invalid option.");
return;
}
final PingRegistration registration = pr.getRegistration(accountName);
if (registration == null) {
u.sendNotice("You don't have a registration.");
return;
}
if (registration.hasOption(pro)) {
u.sendNotice("That option is already enabled.");
return;
}
u.sendNotice("Option " + (registration.addOption(pro) ? "" : "could not be ") + "added.");
}
private void list(@NotNull final PingRegistry pr, @NotNull final User u, @NotNull final String accountName) {
Preconditions.checkNotNull(pr, "pr was null");
Preconditions.checkNotNull(u, "u was null");
Preconditions.checkNotNull(accountName, "accountName was null");
final List<String> messages = Lists.newArrayList();
final PingRegistration registration = pr.getRegistration(accountName);
if (registration == null) {
u.sendNotice("You don't have a registration.");
return;
}
for (final PingRegistrationOption pro : PingRegistrationOption.values()) {
messages.add(
String.format(
"%s%s%s (%s)",
registration.hasOption(pro) ? Format.GREEN : Format.RED,
pro.name(),
Format.RESET,
pro.getDescription()
)
);
}
messages.forEach(u::sendNotice);
}
private void remove(@NotNull final PingRegistry pr, @NotNull final User u, @NotNull final String accountName, @NotNull final List<String> args) {
Preconditions.checkNotNull(pr, "pr was null");
Preconditions.checkNotNull(u, "u was null");
Preconditions.checkNotNull(accountName, "accountName was null");
Preconditions.checkNotNull(args, "args was null");
if (args.size() < 1) {
u.sendNotice("You must provide an option to remove.");
return;
}
final PingRegistrationOption pro;
try {
pro = PingRegistrationOption.valueOf(args.get(0).toUpperCase());
} catch (final IllegalArgumentException ex) {
u.sendNotice("Invalid option.");
return;
}
final PingRegistration registration = pr.getRegistration(accountName);
if (registration == null) {
u.sendNotice("You don't have a registration.");
return;
}
if (!registration.hasOption(pro)) {
u.sendNotice("That option is not enabled.");
return;
}
u.sendNotice("Option " + (registration.removeOption(pro) ? "" : "could not be ") + "removed.");
}
@Override
public void onSubcommand(@NotNull final ChannelMessageEvent event, @NotNull final CallInfo ci, @NotNull final List<String> args) {
final User u = event.getActor();
final Optional<String> account = u.getAccount();
if (!account.isPresent()) {
u.sendNotice("You must be registered with services to use this command.");
return;
}
final String accountName = account.get();
final PingRegistry pr = this.humanity.getPingRegistry();
if (!pr.hasRegistration(accountName)) {
u.sendNotice("You don't have a registration.");
return;
}
if (args.size() < 1) {
u.sendNotice("You must provide a subcommand.");
u.sendNotice("Subcommands: (list, l), (add, a), (remove, r)");
return;
}
final String subcommand = args.get(0).toLowerCase();
final List<String> newArgs = args.subList(1, args.size());
switch (subcommand) {
case "list":
case "l":
this.list(pr, u, accountName);
break;
case "add":
case "a":
this.add(pr, u, accountName, newArgs);
break;
case "remove":
case "r":
this.remove(pr, u, accountName, newArgs);
break;
default:
u.sendNotice("Unknown subcommand.");
break;
}
}
}