/*
 *   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.towerrun.config;

import de.steamwar.sql.Event;
import de.steamwar.sql.EventFight;
import de.steamwar.towerrun.TowerRun;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.EnumSet;
import java.util.Set;
import java.util.stream.Collectors;

@UtilityClass
public class Config {
    public static final int MIN_PLAYERS;
    public static final int LOBBY_TIMER;
    public static final Set<Material> DESTROYABLE_BLOCKS;
    public static final int GAME_TIMER;
    public static final int GAME_ESCAPE_TIMER;

    private static final int EVENT_KAMPF_ID;

    // Event
    public static final EventFight EVENT_FIGHT;
    public static final int EVENT_TEAM_BLUE_ID;
    public static final int EVENT_TEAM_RED_ID;
    public static final int EVENT_MAXIMUM_TEAM_MEMBERS;

    static {
        File configFile = new File(TowerRun.getInstance().getDataFolder(), "config.yml");
        if (!configFile.exists()) {
            TowerRun.getInstance().getLogger().severe("Config not found!");
            Bukkit.shutdown();
        }

        ConfigurationSection config = YamlConfiguration.loadConfiguration(configFile);

        MIN_PLAYERS = config.getInt("minPlayers");
        LOBBY_TIMER = config.getInt("lobbyTimer");
        GAME_TIMER = config.getInt("gameTimer", 20 * 60);
        GAME_ESCAPE_TIMER = config.getInt("gameEscapeTimer", 60);
        DESTROYABLE_BLOCKS = EnumSet.copyOf(config.getStringList("destroyable").stream().map(Material::valueOf).collect(Collectors.toSet()));

        EVENT_KAMPF_ID = Integer.parseInt(System.getProperty("fightID", "0"));

        if (event()) {
            EVENT_FIGHT = EventFight.byId(EVENT_KAMPF_ID);

            if (EVENT_FIGHT == null) {
                Bukkit.shutdown();
                throw new IllegalStateException("Event not found");
            }

            Event event = Event.byId(EVENT_FIGHT.getEventID());

            EVENT_TEAM_BLUE_ID = EVENT_FIGHT.getTeamBlue();
            EVENT_TEAM_RED_ID = EVENT_FIGHT.getTeamRed();
            EVENT_MAXIMUM_TEAM_MEMBERS = event.getMaximumTeamMembers();
        } else {
            EVENT_FIGHT = null;

            EVENT_TEAM_BLUE_ID = 0;
            EVENT_TEAM_RED_ID = 0;
            EVENT_MAXIMUM_TEAM_MEMBERS = Integer.MAX_VALUE;
        }
    }

    public static boolean test() {
        return EVENT_KAMPF_ID == -1;
    }

    public static boolean event() {
        return EVENT_KAMPF_ID > 0;
    }
}
