<commit_before>package de.codemakers.bot.supreme.commands.impl.moderation.util;

import de.codemakers.bot.supreme.commands.Command;
import de.codemakers.bot.supreme.commands.CommandCategory;
import de.codemakers.bot.supreme.commands.arguments.ArgumentConsumeType;
import de.codemakers.bot.supreme.commands.arguments.ArgumentList;
import de.codemakers.bot.supreme.commands.invoking.Invoker;
import de.codemakers.bot.supreme.entities.DeleteMessageManager;
import de.codemakers.bot.supreme.entities.MessageEvent;
import de.codemakers.bot.supreme.permission.PermissionRoleFilter;
import de.codemakers.bot.supreme.util.AdvancedFile;
import de.codemakers.bot.supreme.util.Standard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.dv8tion.jda.core.EmbedBuilder;

/**
 * GetLogCommand
 *
 * @author Panzer1119
 */
public class GetLogCommand extends Command {

    public static final String LOG_PREFIX = "log_";
    public static final String LOG_SUFFIX = ".txt";

    @Override
    public void initInvokers() {
        addInvokers(Invoker.createInvoker("getLog", this), Invoker.createInvoker("gLog", this));
    }

    @Override
    public boolean called(Invoker invoker, ArgumentList arguments, MessageEvent event) {
        if (arguments == null) {
            return false;
        }
        if (arguments.isEmpty()) {
            return true;
        }
        final boolean list = arguments.isConsumed(Standard.ARGUMENT_LIST, ArgumentConsumeType.FIRST_IGNORE_CASE);
        //final boolean download = arguments.isConsumed(Standard.ARGUMENT_DOWNLOAD, ArgumentConsumeType.FIRST_IGNORE_CASE);
        if (list/* || download*/) {
            /*
            if (!list) {
                return false;
            }
            if (download) {
                return arguments.isSize(2);
            }*/
            return arguments.isSize(1); //-list
        }
        return arguments.isSize(1); //Log
    }

    @Override
    public void action(Invoker invoker, ArgumentList arguments, MessageEvent event) {
        final boolean list = arguments.isConsumed(Standard.ARGUMENT_LIST, ArgumentConsumeType.CONSUME_FIRST_IGNORE_CASE);
        //final boolean download = arguments.isConsumed(Standard.ARGUMENT_DOWNLOAD, ArgumentConsumeType.CONSUME_FIRST_IGNORE_CASE);
        if (list) { //-list
            //final String list_logs = Standard.STANDARD_LOG_FOLDER.listAdvancedFiles((parent, name) -> (name != null && name.startsWith(LOG_PREFIX) && name.endsWith(LOG_SUFFIX))).stream().map((advancedFile) -> advancedFile.toFile()).map((advancedFile) -> (/*download ? Standard.embedLink(advancedFile.getName(), event.getTextChannel().sendFile(advancedFile.toByteArray(), advancedFile.getName(), null).complete().getAttachments().get(0).getUrl()) : */advancedFile.getName())).collect(Collectors.joining("\n"));
            final int log_file_show_count = Standard.STANDARD_SETTINGS.asAutoAdd().getProperty("log_file_show_count", 25);
            List<AdvancedFile> files = Standard.STANDARD_LOG_FOLDER.listAdvancedFiles((parent, name) -> (name != null && name.startsWith(LOG_PREFIX) && name.endsWith(".txt")));
            files = files.stream().skip(files.size() - log_file_show_count).collect(Collectors.toList());
            final Map<String, Map<String, Map<String, List<AdvancedFile>>>> years = new HashMap<>();
            files.stream().forEach((advancedFile) -> {
                final String name = advancedFile.getName();
                final String year = name.substring(LOG_PREFIX.length(), LOG_PREFIX.length() + 4);
                final String month = name.substring(LOG_PREFIX.length() + 5, LOG_PREFIX.length() + 7);
                final String day = name.substring(LOG_PREFIX.length() + 8, LOG_PREFIX.length() + 10);
                years.computeIfAbsent(year, (key) -> new HashMap<>());
                years.get(year).computeIfAbsent(month, (key) -> new HashMap<>());
                years.get(year).get(month).computeIfAbsent(day, (key) -> new ArrayList<>());
                years.get(year).get(month).get(day).add(advancedFile);
            });
            final StringBuilder out = new StringBuilder();
            out.append(Standard.toUnderlineBold(String.format("Logs (last %d):", log_file_show_count))).append("\n").append("\n");
            for (String year : years.keySet()) {
                out.append(Standard.toBold(Standard.toUnderline("YEAR:") + " " + year)).append("\n");
                final Map<String, Map<String, List<AdvancedFile>>> months = years.get(year);
                for (String month : months.keySet()) {
                    out.append(Standard.TAB).append(Standard.TAB).append(Standard.toBold(Standard.toUnderline("MONTH:") + " " + month)).append("\n");
                    final Map<String, List<AdvancedFile>> days = months.get(month);
                    days.keySet().stream().map((day) -> {
                        out.append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.toBold(Standard.toUnderline("DAY:") + " " + day)).append("\n");
                        return day;
                    }).map((day) -> days.get(day)).forEach((logs) -> {
                        logs.stream().forEach((advancedFile) -> out.append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(advancedFile.getName()).append("\n"));
                    });
                }
            }
            /*
            final Map<String, List<AdvancedFile>> years = Standard.STANDARD_LOG_FOLDER.listAdvancedFiles((parent, name) -> (name != null && name.startsWith(LOG_PREFIX) && name.endsWith(LOG_SUFFIX))).stream().collect(Collectors.groupingBy((advancedFile) -> advancedFile.getName().substring(LOG_PREFIX.length(), LOG_PREFIX.length() + 4)));
            System.err.println(years.toString());
            final Map<String, Map<String, List<AdvancedFile>>> years_months = null;
            System.err.println(years.keySet().stream().map((year) -> years.get(year)).map((advancedFiles) -> advancedFiles.stream().collect(Collectors.groupingBy((advancedFile) -> advancedFile.getName().substring(LOG_PREFIX.length() + 5, LOG_PREFIX.length() + 7)))).collect(Collectors.groupingBy((year) -> year)));
             */<commit_msg>Improved log file list with streams<commit_after>package de.codemakers.bot.supreme.commands.impl.moderation.util;

import de.codemakers.bot.supreme.commands.Command;
import de.codemakers.bot.supreme.commands.CommandCategory;
import de.codemakers.bot.supreme.commands.arguments.ArgumentConsumeType;
import de.codemakers.bot.supreme.commands.arguments.ArgumentList;
import de.codemakers.bot.supreme.commands.invoking.Invoker;
import de.codemakers.bot.supreme.entities.DeleteMessageManager;
import de.codemakers.bot.supreme.entities.MessageEvent;
import de.codemakers.bot.supreme.permission.PermissionRoleFilter;
import de.codemakers.bot.supreme.util.AdvancedFile;
import de.codemakers.bot.supreme.util.Standard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.dv8tion.jda.core.EmbedBuilder;

/**
 * GetLogCommand
 *
 * @author Panzer1119
 */
public class GetLogCommand extends Command {

    public static final String LOG_PREFIX = "log_";
    public static final String LOG_SUFFIX = ".txt";

    @Override
    public void initInvokers() {
        addInvokers(Invoker.createInvoker("getLog", this), Invoker.createInvoker("gLog", this));
    }

    @Override
    public boolean called(Invoker invoker, ArgumentList arguments, MessageEvent event) {
        if (arguments == null) {
            return false;
        }
        if (arguments.isEmpty()) {
            return true;
        }
        final boolean list = arguments.isConsumed(Standard.ARGUMENT_LIST, ArgumentConsumeType.FIRST_IGNORE_CASE);
        //final boolean download = arguments.isConsumed(Standard.ARGUMENT_DOWNLOAD, ArgumentConsumeType.FIRST_IGNORE_CASE);
        if (list/* || download*/) {
            /*
            if (!list) {
                return false;
            }
            if (download) {
                return arguments.isSize(2);
            }*/
            return arguments.isSize(1); //-list
        }
        return arguments.isSize(1); //Log
    }

    @Override
    public void action(Invoker invoker, ArgumentList arguments, MessageEvent event) {
        final boolean list = arguments.isConsumed(Standard.ARGUMENT_LIST, ArgumentConsumeType.CONSUME_FIRST_IGNORE_CASE);
        //final boolean download = arguments.isConsumed(Standard.ARGUMENT_DOWNLOAD, ArgumentConsumeType.CONSUME_FIRST_IGNORE_CASE);
        if (list) { //-list
            //final String list_logs = Standard.STANDARD_LOG_FOLDER.listAdvancedFiles((parent, name) -> (name != null && name.startsWith(LOG_PREFIX) && name.endsWith(LOG_SUFFIX))).stream().map((advancedFile) -> advancedFile.toFile()).map((advancedFile) -> (/*download ? Standard.embedLink(advancedFile.getName(), event.getTextChannel().sendFile(advancedFile.toByteArray(), advancedFile.getName(), null).complete().getAttachments().get(0).getUrl()) : */advancedFile.getName())).collect(Collectors.joining("\n"));
            final int log_file_show_count = Standard.STANDARD_SETTINGS.asAutoAdd().getProperty("log_file_show_count", 25);
            List<AdvancedFile> files = Standard.STANDARD_LOG_FOLDER.listAdvancedFiles((parent, name) -> (name != null && name.startsWith(LOG_PREFIX) && name.endsWith(".txt")));
            files = files.stream().skip(files.size() - log_file_show_count).collect(Collectors.toList());
            final Map<String, Map<String, Map<String, List<AdvancedFile>>>> years = new HashMap<>();
            files.stream().forEach((advancedFile) -> {
                final String name = advancedFile.getName();
                final String year = name.substring(LOG_PREFIX.length(), LOG_PREFIX.length() + 4);
                final String month = name.substring(LOG_PREFIX.length() + 5, LOG_PREFIX.length() + 7);
                final String day = name.substring(LOG_PREFIX.length() + 8, LOG_PREFIX.length() + 10);
                years.computeIfAbsent(year, (key) -> new HashMap<>());
                years.get(year).computeIfAbsent(month, (key) -> new HashMap<>());
                years.get(year).get(month).computeIfAbsent(day, (key) -> new ArrayList<>());
                years.get(year).get(month).get(day).add(advancedFile);
            });
            final StringBuilder out = new StringBuilder();
            out.append(Standard.toUnderlineBold(String.format("Logs (last %d):", log_file_show_count))).append("\n").append("\n");
            years.keySet().stream().map((year) -> {
                out.append(Standard.toBold(Standard.toUnderline("YEAR:") + " " + year)).append("\n");
                return year;
            }).map((year) -> years.get(year)).forEach((months) -> {
                months.keySet().stream().map((month) -> {
                    out.append(Standard.TAB).append(Standard.TAB).append(Standard.toBold(Standard.toUnderline("MONTH:") + " " + month)).append("\n");
                    return month;
                }).map((month) -> months.get(month)).forEach((days) -> {
                    days.keySet().stream().map((day) -> {
                        out.append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.toBold(Standard.toUnderline("DAY:") + " " + day)).append("\n");
                        return day;
                    }).map((day) -> days.get(day)).forEach((logs) -> logs.stream().forEach((advancedFile) -> out.append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(Standard.TAB).append(advancedFile.getName()).append("\n")));
                });
            });
{'fraction_non_alphanumeric': 0.14754786698018138, 'fraction_numerical': 0.0031071548538797447, 'mean_word_length': 3.1321998612074946, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7064055', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3323, 'n_words': 520}