/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minecraft.class_2487
 *  net.minecraft.class_2499
 *  net.minecraft.class_2520
 */
package meteordevelopment.meteorclient.systems.publicconfigs;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.System;
import meteordevelopment.meteorclient.systems.Systems;
import meteordevelopment.meteorclient.systems.publicconfigs.PublicConfig;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2520;

public class PublicConfigs
extends System<PublicConfigs> {
    public static final List<String> ADMINS = List.of("cheeseenjoyer143", "redvelvetidk");
    public final List<PublicConfig> configs = new ArrayList<PublicConfig>();
    private static final List<String> BAD_WORDS = List.of("fuck", "shit", "nigger", "retard", "ez", "trash");
    private static final Pattern RANDOM_WORDS_PATTERN = Pattern.compile("(?i)^(how hi like|asdf|qwerty|hello hello|help help)$");

    public PublicConfigs() {
        super("public-configs");
    }

    public static PublicConfigs get() {
        return Systems.get(PublicConfigs.class);
    }

    public String validate(String name, String content) {
        if (name == null || name.isBlank()) {
            return "Name cannot be empty.";
        }
        if (content == null || content.isBlank()) {
            return "Content cannot be empty.";
        }
        String fullText = (name + " " + content).toLowerCase();
        for (String word : BAD_WORDS) {
            if (!fullText.contains(word)) continue;
            return "Input contains prohibited language.";
        }
        if (RANDOM_WORDS_PATTERN.matcher(name).matches() || RANDOM_WORDS_PATTERN.matcher(content).matches()) {
            return "Input looks like spam or random words.";
        }
        if (name.length() < 3) {
            return "Name is too short.";
        }
        if (this.configs.size() >= 100) {
            return "Global limit of 100 configs reached.";
        }
        return null;
    }

    public String validateComment(String text) {
        if (text == null || text.isBlank()) {
            return "Comment cannot be empty.";
        }
        String fullText = text.toLowerCase();
        for (String word : BAD_WORDS) {
            if (!fullText.contains(word)) continue;
            return "Comment contains prohibited language.";
        }
        if (RANDOM_WORDS_PATTERN.matcher(text).matches()) {
            return "Comment looks like spam or random words.";
        }
        return null;
    }

    public boolean add(PublicConfig config) {
        if (this.configs.size() >= 100) {
            return false;
        }
        this.configs.add(config);
        this.save();
        return true;
    }

    public void remove(PublicConfig config) {
        this.configs.remove(config);
        this.save();
    }

    public boolean isAdmin() {
        if (MeteorClient.mc.method_1548() == null) {
            return false;
        }
        return ADMINS.contains(MeteorClient.mc.method_1548().method_1676());
    }

    @Override
    public class_2487 toTag() {
        class_2487 tag = new class_2487();
        class_2499 list = new class_2499();
        for (PublicConfig config : this.configs) {
            list.add((Object)config.toTag());
        }
        tag.method_10566("configs", (class_2520)list);
        return tag;
    }

    @Override
    public PublicConfigs fromTag(class_2487 tag) {
        this.configs.clear();
        if (tag.method_10545("configs")) {
            class_2499 list = tag.method_68569("configs");
            for (class_2520 element : list) {
                this.configs.add(new PublicConfig().fromTag((class_2487)element));
            }
        }
        return this;
    }
}

