/*
 * Skybot, a multipurpose discord bot
 *      Copyright (C) 2017 - 2019  Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ml.duncte123.skybot.commands.lgbtq;

import ml.duncte123.skybot.Author;
import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.objects.command.CommandCategory;
import ml.duncte123.skybot.objects.command.CommandContext;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg;

@Author(nickname = "duncte123", author = "Duncan Sterken")
public class SetPronounsCommand extends Command {

    private static final String PRONOUNS_REGEX = "([a-z]+\\/[a-z]+\\/[a-z]+\\/[a-z]+)(?:.*)";

    private final String[] messages = {
        "%6$s went into the kitchen.\n" +
            "**%1$s** brought **%3$s** groceries with **%2$s**.\n" +
            "At least I think the groceries were **%4$s**.\n" +
            "**%1$s %5$s** making dinner tonight, after all.",

        "**%1$s %5$s** a writer and wrote that book **%2$sself**. \n" +
            "Those ideas are **%4$s**. \n" +
            "I like both **%2$s** and **%3$s** ideas."
    };

    @Override
    public void executeCommand(@Nonnull CommandContext ctx) {
        final GuildMessageReceivedEvent event = ctx.getEvent();

        if (ctx.getArgs().isEmpty()) {
            sendMsg(event, "Correct usage: `" + ctx.getPrefix() + getName() + " <pronouns> [--plural]`");
            return;
        }

        final Pattern pattern = Pattern.compile(PRONOUNS_REGEX);
        final Matcher matcher = pattern.matcher(ctx.getArgsRaw());

        if (!matcher.matches()) {
            sendMsg(event, "Incorrect format, check `" + ctx.getPrefix() + "help " + getName() + '`');
            return;
        }

        final String pronouns = matcher.group(1);

        if (pronouns.length() > 30) {
            sendMsg(event, "Maximum length for pronouns is 30 characters, " +
                "please contact duncte123#1245 to have this changed");
            return;
        }

        final String[] pronounsSplit = pronouns.split("/");
        final boolean singular = !ctx.getArgsRaw().contains("--plural");
        final String format = this.messages[ctx.getRandom().nextInt(messages.length)];
        final List<String> items = new ArrayList<>(Arrays.asList(pronounsSplit));
        items.add(singular ? "is" : "are");
        items.add(ctx.getAuthor().getName());

        final String message = String.format("Your pronouns have been set to **%s**\n", pronouns) +
            "Here is a preview:\n" +
            String.format(format, items.toArray());

        sendMsg(event, message);

        ctx.getApis().setPronouns(ctx.getAuthor().getIdLong(), pronouns, singular);
    }

    @Override
    public String getName() {
        return "setpronouns";
    }

    @Override
    public CommandCategory getCategory() {
        return CommandCategory.LGBTQ;
    }

    @Override
    public String help(String prefix) {
        return "Set your pronouns to people can check them with `" + prefix + "pronounscheck`\n" +
            "Usage: `" + prefix + getName() + " <pronouns> [--plural]`\n" +
            "Examples of pronouns are:\n" +
            "```they/them/their/theirs\n" +
            "she/her/her/hers\n" +
            "he/him/his/his\n" +
            "ze/zir/zir/zirs\n" +
            "xe/xir/xir/xirs```\n" +
            "Use the `--plural` flag to mark your pronouns as plural (is vs are)";
    }
}

@@ -1235,72 +1235,8 @@
 ist;
-%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aim
@@ -1418,103 +1418,8 @@
  %7B%0A%0A
-    private static final String PRONOUNS_REGEX = %22(%5Ba-z%5D+%5C%5C/%5Ba-z%5D+%5C%5C/%5Ba-z%5D+%5C%5C/%5Ba-z%5D+)(?:.*)%22;%0A%0A
     
@@ -2208,157 +2208,142 @@
 nal 
-Pattern pattern = Pattern.compile(PRONOUNS_REGEX);%0A        final Matcher matcher = pattern.matcher(ctx.getArgsRaw());%0A%0A        if (!matcher.matches()
+String pronouns = ctx.getArgsRaw();%0A        final String%5B%5D pronounsSplit = pronouns.split(%22/%22);%0A%0A        if (pronounsSplit.length != 4
 ) %7B%0A
@@ -2480,59 +2480,8 @@
  %7D%0A%0A
-        final String pronouns = matcher.group(1);%0A%0A
     
@@ -2698,68 +2698,8 @@
  %7D%0A%0A
-        final String%5B%5D pronounsSplit = pronouns.split(%22/%22);%0A
     

Allow for unicode in pronouns