import { eg } from "../frameWork/eg";
import { GameServerConfig } from "../network/GameServerConfig";

import { HttpClient as HttpClient_Browser, WsClient as WsClient_Browser } from 'tsrpc-browser';
import { HttpClient as HttpClient_Miniapp, WsClient as WsClient_Miniapp } from 'tsrpc-miniapp';
import { serviceProto as ServiceProtoRoom, ServiceType, ServiceType as ServiceTypeRoom } from "../shared/serviceProto";
import IDataModel from "../frameWork/model/IDataModel";


/** 登录服 */
export class LoginServerModel extends IDataModel {
    /** 连接网关服务器 Http 客户端 */
    hcGate: HttpClient_Miniapp<ServiceTypeRoom> | HttpClient_Browser<ServiceTypeRoom> = null!;
    Init() {
        this.hcGate = eg.tspcNet.createLoginServer(GameServerConfig.LOGIN_SERVER);
    }

    /** 请求api */
    public async ReqApi<T extends string & keyof ServiceType['api']>(apiName: T, req: ServiceType['api'][T]['req']) {
        let ret = await this.hcGate.callApi(apiName, req);
        if (!ret.isSucc) {
            console.error(ret.err);
        }
        return ret;
    }


}