package eu.jonahbauer.wizard.core.model;

import eu.jonahbauer.wizard.common.model.Configuration;
import eu.jonahbauer.wizard.core.model.deck.Decks;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

@UtilityClass
public class Configurations {
    private static final Map<Configuration, GameConfiguration> CONFIGURATIONS = new HashMap<>();

    public static final GameConfiguration DEFAULT = register(Configuration.DEFAULT, new GameConfiguration(
            Decks.DEFAULT,
            true,
            10 * 60 * 1000
    ));

    public static final GameConfiguration DEFAULT_PM1 = register(Configuration.DEFAULT_PM1, new GameConfiguration(
            Decks.DEFAULT,
            false,
            10 * 60 * 1000
    ));

    public static final GameConfiguration ANNIVERSARY_2016 = register(Configuration.ANNIVERSARY_2016, new GameConfiguration(
            Decks.ANNIVERSARY_2016,
            true,
            10 * 60 * 1000
    ));

    public static final GameConfiguration ANNIVERSARY_2016_PM1 = register(Configuration.ANNIVERSARY_2016_PM1, new GameConfiguration(
            Decks.ANNIVERSARY_2016,
            false,
            10 * 60 * 1000
    ));

    public static final GameConfiguration ANNIVERSARY_2021 = register(Configuration.ANNIVERSARY_2021, new GameConfiguration(
            Decks.ANNIVERSARY_2021,
            true,
            10 * 60 * 1000
    ));

    public static final GameConfiguration ANNIVERSARY_2021_PM1 = register(Configuration.ANNIVERSARY_2021_PM1, new GameConfiguration(
            Decks.ANNIVERSARY_2021,
            false,
            10 * 60 * 1000
    ));

    @Contract("_,_ -> param2")
    private static GameConfiguration register(@NotNull Configuration name, @NotNull GameConfiguration configuration) {
        if (CONFIGURATIONS.putIfAbsent(name, configuration) != null) {
            throw new IllegalArgumentException("Name already taken.");
        }
        return configuration;
    }

    @NotNull
    public static GameConfiguration get(@NotNull Configuration name) {
        var out = CONFIGURATIONS.get(name);
        if (out == null) throw new NoSuchElementException("Configuration with name '" + name + "' does not exist.");
        return out;
    }
}
