import { getGlobal, genUnikey } from "../common/base";
import { Logger, LogConfig } from "../common/logger";
import { Core } from "./core";
import global from "./global";

// 全局事件管理
export const GameEventInstance = global;

// 客户端代理
export interface ClientProxyConfig extends LogConfig {}

export class ClientProxy {
  core: Core;
  logger: Logger;

  constructor(config?: ClientProxyConfig) {
    config && this.init(config);
  }

  init(config: ClientProxyConfig) {
    // 只能初始化一次
    if (this.core) {
      this.logger.warn("already inited");
      return;
    }

    // 寻找全局 hyExt
    const { hyExt } = getGlobal();
    if (!hyExt) {
      this.logger.warn("proxy fail: hyExt is undefined");
      return;
    }

    // 初始化
    this.logger = new Logger("Client", config);
    this.core = new Core({ logger: this.logger, SDK: hyExt });
    this.logger.log("proxy success");
    return;
  }

  setConfig(config: LogConfig) {
    this.logger.setConfig(config);
  }

  destroy() {
    this.core.destroy();
  }
}

// 初始化唯一
export default new ClientProxy({ debug: true });
