package top.leisuretimedock.dglabgame.core.manager;

import com.r3944realms.dg_lab.api.manager.Status;
import com.r3944realms.dg_lab.api.websocket.message.Message;
import com.r3944realms.dg_lab.api.websocket.sharedData.ISharedData;
import com.r3944realms.dg_lab.manager.DGPBServerManager;
import com.r3944realms.dg_lab.manager.IDGLabManager;
import com.r3944realms.dg_lab.websocket.sharedData.ServerPowerBoxSharedData;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.Set;

public class ServerManager implements IManager<DGPBServerManager>, IDGLabManager {
    private final DGPBServerManager serverManager;

    public ServerManager(@NotNull DGPBServerManager serverManager) {
        this.serverManager = serverManager;
    }

    @Override
    public void start() {
        serverManager.start();
    }

    @Override
    public void stop() {
        serverManager.stop();
    }

    @Override
    public ISharedData getSharedData() {
        return serverManager.getSharedData();
    }

    @Override
    public Status getStatus() {
        return serverManager.getStatus();
    }

    @Override
    public void setStatus(Status status) {
        serverManager.setStatus(status);
    }

    @Override
    public void startAll() {
        start();
    }

    @Override
    public void stopAll() {
        stop();
    }

    @Override
    public Optional<DGPBServerManager> getInstance() {
        return Optional.of(serverManager);
    }

    public void sendToAll(Message message) {
        ISharedData sd = serverManager.getSharedData();
        if (sd instanceof ServerPowerBoxSharedData spsd) {
            spsd.connections.keySet().forEach(id -> serverManager.send(id, message));
        }
    }

    public Set<String> getConnectedClients() {
        ISharedData sd = serverManager.getSharedData();
        if (sd instanceof ServerPowerBoxSharedData spsd) {
            return spsd.connections.keySet();
        }
        return Set.of();
    }
}
