/*
 *   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 com.velocitypowered.api.proxy.server.RegisteredServer;
import lombok.Getter;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.introspector.BeanAccess;
import org.yaml.snakeyaml.representer.Representer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Getter
public class Config {

    public static <T> T load(Class<T> clazz, File file, Consumer<TypeDescription> description) {
        TypeDescription typeDescription = new TypeDescription(Config.class);
        description.accept(typeDescription);

        Constructor constructor = new Constructor(clazz, new LoaderOptions());
        constructor.addTypeDescription(typeDescription);

        Representer representer = new Representer();
        representer.getPropertyUtils().setSkipMissingProperties(true);

        Yaml yaml = new Yaml(constructor, representer);
        yaml.setBeanAccess(BeanAccess.FIELD);

        try{
            return yaml.load(new FileInputStream(file));
        }catch(IOException e){
            VelocityCore.getProxy().shutdown();
            throw new SecurityException("Could not load config", e);
        }
    }

    public static Config load() {
        return load(Config.class, new File(VelocityCore.get().getDataDirectory().toFile(), "config.yml"), description -> description.addPropertyParameters("servers", String.class, Config.Server.class));
    }

    private String lobbyserver;
    private boolean eventmode = false;
    private Map<String, Server> servers = Collections.emptyMap();
    private List<String> broadcasts = Collections.emptyList();
    private Poll poll = null;

    public RegisteredServer lobbyserver() {
        return VelocityCore.getProxy().getServer(lobbyserver).orElseThrow();
    }

    @Getter
    public static class Poll {
        private String question;
        private List<String> answers;
    }

    @Getter
    public static class Server {
        private int spectatePort = 0;
        private List<String> commands;
    }
}
