package top.leisuretimedock.dglabgame.core.manager;

import com.r3944realms.dg_lab.api.websocket.message.role.WebSocketServerRole;
import com.r3944realms.dg_lab.manager.DGPBServerManager;
import com.r3944realms.dg_lab.websocket.PowerBoxWSServer;
import com.r3944realms.dg_lab.websocket.handler.server.DefaultServerOperation;
import com.r3944realms.dg_lab.websocket.sharedData.ServerPowerBoxSharedData;
import top.leisuretimedock.dglabgame.config.CommonConfig;

import java.util.Optional;

public class DGLabManagerHolder {
    private static ServerManager serverManager;
    private static final ClientManager clientManager = new ClientManager();

    public static ClientManager getClientManager() {
        return clientManager;
    }

    public static Optional<ServerManager> getServerManager() {
        return Optional.ofNullable(serverManager);
    }

    public static ServerManager getOrCreateServerManager() {
        if (serverManager == null) {
            ServerPowerBoxSharedData sharedData = new ServerPowerBoxSharedData();
            WebSocketServerRole role = WebSocketServerRole.of("dglabgame-server");
            PowerBoxWSServer server = new PowerBoxWSServer(
                    sharedData, role, new DefaultServerOperation(),
                    CommonConfig.SERVER_PORT.get()
            );
            DGPBServerManager dgpbServerManager = new DGPBServerManager(server);
            serverManager = new ServerManager(dgpbServerManager);
        }
        return serverManager;
    }

    public static void setServerManager(ServerManager manager) {
        if (serverManager != null) {
            serverManager.stopAll();
        }
        serverManager = manager;
    }
}
