import CSClient from "@features/colyseus/CSClient";

class CSClientWrap{
    constructor() {
        this.clients={};
    }
    async connect(roomName, serverUrl,messageCB=null,connectedCB=null,closeCB=null,options={}){
        this.disconnect(roomName);
        let client=new CSClient(roomName,serverUrl,messageCB,connectedCB,closeCB);
        this.clients[roomName]=client;
        let isConnected=await client.connect(options);
        return isConnected;
    }
    async disconnect(roomName){
        let client=this.clients[roomName];
        if (client!=null){
            client.disconnect();
            delete this.clients[roomName];
        }
        return true;
    }
    isConnected(roomName){
        let client=this.clients[roomName];
        if (client)return client.isConnected();
        return false;
    }
    send(roomName,type,message){
        if (!this.isConnected(roomName))return;
        let client=this.clients[roomName];
        client.send(type,message);
    }
}
let instance=null;
CSClientWrap.getInstance=()=>{
    if (instance==null){
        instance=new CSClientWrap();
    }
    return instance;
}
export default CSClientWrap.getInstance();
