package ru.dragonestia.kubik.msb;

import lombok.extern.log4j.Log4j2;
import net.minestom.server.Auth;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.GameMode;
import ru.dragonestia.kubik.client.Kubik;
import ru.dragonestia.kubik.client.server.ServerControl;
import ru.dragonestia.kubik.client.util.EnvUtil;
import ru.dragonestia.kubik.msb.command.TransferCommand;
import ru.dragonestia.msb.boot.ServerBootstrap;
import ru.dragonestia.msb.boot.ServerInitializer;
import ru.dragonestia.msb.module.DebugCommands;
import ru.dragonestia.msb.module.DefaultFlatWorld;

@Log4j2
public class Main extends ServerInitializer {

    private Kubik kubik;
    private ServerControl kubikServerControl;

    static void main() {
        ServerBootstrap.start("0.0.0.0", 25565, new Main(), new Auth.Velocity("PRk6nlzCuJaE"));
    }

    @Override
    public void onLoad() {
        var kubikHttpUrl = EnvUtil.getString("KUBIK_DISCOVERY_HTTP_URL", "http://localhost:8080");
        var kubikWsUrl = EnvUtil.getString("KUBIK_DISCOVERY_WS_URL", "ws://localhost:8080");

        log.info("Using kubik urls: {}, {}", kubikHttpUrl, kubikWsUrl);

        kubik = new Kubik(
                kubikHttpUrl,
                kubikWsUrl
        );
    }

    @Override
    public void onDefaultModulesLoaded() {
        DefaultFlatWorld.init(GameMode.CREATIVE);
        DebugCommands.init();
    }

    @Override
    public void onServerStarted() {
        var kubikGroupId = EnvUtil.getString("KUBIK_GROUP_ID", "none");
        if (!kubikGroupId.equals("none")) {
            kubikServerControl = kubik.initServer(kubikGroupId, 25565);
        }

        MinecraftServer.getCommandManager().register(new TransferCommand(kubik, kubikServerControl));
    }
}
