/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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 de.steamwar.sql.GameModeConfig;
import de.steamwar.sql.SchematicType;
import lombok.Getter;
import lombok.experimental.UtilityClass;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@UtilityClass
public class ArenaMode {

    private static final Map<String, GameModeConfig<String, String>> byChat = new HashMap<>();
    private static final Map<String, GameModeConfig<String, String>> byInternal = new HashMap<>();
    private static final Map<SchematicType, GameModeConfig<String, String>> bySchemType = new HashMap<>();
    @Getter
    private static final List<GameModeConfig<String, String>> allModes = new LinkedList<>();

    static {
        init();
    }

    public static void init() {
        byChat.clear();
        byInternal.clear();
        bySchemType.clear();
        allModes.clear();

        File folder = new File(VelocityCore.get().getDataDirectory().getParent().toFile(), "FightSystem");
        if (!folder.exists()) return;

        GameModeConfig.init();
        SchematicType.init();
        for (GameModeConfig<String, String> gameModeConfig : GameModeConfig.<String>getAll()) {
            if (!gameModeConfig.Server.loaded) continue;
            allModes.add(gameModeConfig);
            byInternal.put(gameModeConfig.configFile.getName().replace(".yml", ""), gameModeConfig);
            for (String name : gameModeConfig.Server.ChatNames) {
                byChat.put(name.toLowerCase(), gameModeConfig);
            }
            if (gameModeConfig.Schematic.loaded && gameModeConfig.Schematic.Type != SchematicType.Normal) {
                bySchemType.put(gameModeConfig.Schematic.Type, gameModeConfig);
                System.out.println(gameModeConfig.configFile.getAbsolutePath());
                SchematicType checkType = gameModeConfig.Schematic.Type.checkType();
                if (checkType != null) bySchemType.put(checkType, gameModeConfig);
            }
        }
    }

    public static GameModeConfig<String, String> getByChat(String name) {
        return byChat.get(name.toLowerCase());
    }

    public static GameModeConfig<String, String> getByInternal(String name) {
        return byInternal.get(name);
    }

    public static List<String> getAllChatNames(boolean historic) {
        List<String> chatNames = new LinkedList<>();
        for (GameModeConfig<String, String> mode : byInternal.values()) {
            if (mode.isActive() && historic == mode.Server.Historic) {
                chatNames.addAll(mode.Server.ChatNames);
            }
        }
        return chatNames;
    }

    public static GameModeConfig<String, String> getBySchemType(SchematicType schemType) {
        return bySchemType.get(schemType);
    }
}
