/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024  SteamWar.de-Serverteam
 *
 *   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 <https://www.gnu.org/licenses/>.
 */

package de.steamwar.velocitycore;

import lombok.Getter;
import lombok.Setter;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
public class GameModeConfig {

    private static final Pattern terminatingNumber = Pattern.compile("(\\d+)$");

    public static <T extends GameModeConfig> void loadAll(Class<T> config, BiConsumer<File, T> consumer) {
        File folder = new File(VelocityCore.get().getDataDirectory().getParent().toFile(), "FightSystem");
        if(!folder.exists())
            return;

        for(File file : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) {
            consumer.accept(file, Config.load(config, file, description -> {}));
        }
    }

    private Server Server = null;
    private List<String> CheckQuestions = Collections.emptyList();
    private String deadline = null;
    private Schematic Schematic = null;
    @Setter
    private String GameName;

    @Getter
    public static class Server {
        private String Folder;
        private List<String> ChatNames = Collections.emptyList();
        private List<String> Maps;
        private boolean Spigot = false;
        private boolean Ranked = false;
        private boolean Historic = false;
    }

    @Getter
    public static class Schematic {
        private String Type;
        private String Shortcut;
        private String Material;
        private boolean ManualCheck = true;
    }

    public ServerVersion getVersion() {
        Matcher matcher = terminatingNumber.matcher(getServer().getFolder());
        matcher.find();
        return ServerVersion.valueOf((getServer().isSpigot() ? "SPIGOT_" : "PAPER_") + matcher.group(1));
    }

    public String getFolder() {
        return getServer().getFolder();
    }

    public List<String> getMaps() {
        return getServer().getMaps();
    }

    public boolean isHistoric() {
        return getServer().isHistoric();
    }

    public boolean isRanked() {
        return getServer().isRanked();
    }

    public String getSchemType() {
        return getSchematic().getType();
    }
}
