import { LogUtil } from "fastsdk"
import { HLMainWorker } from "./HLMainWorker";

export class HLReceive {
  private static instance: HLReceive | null = null

  private constructor() {
  }

  //单例
  public static getInstance(): HLReceive {
    if (HLReceive.instance == null) {
      LogUtil.info('重新生成instance')
      HLReceive.instance = new HLReceive();
    }
    return HLReceive.instance;
  }

  //接收消息
  public receiveMessage(receiveData: Record<string, string | Object>) {
    LogUtil.info('receive data;' + JSON.stringify(receiveData));
    LogUtil.info('接收数据的类型:' + typeof receiveData + ' func:' + receiveData['func'] + ' dataType:' +
      typeof receiveData['params'] + ' data:' + receiveData['params']);

    let funcName = receiveData.func as string;
    LogUtil.info('funcName:' + funcName);
    this.gotoSDK(receiveData['params'] as Record<string, string>, funcName);
  }

  public static receiveMessage(receiveData: Record<string, string | Object>) {
    HLReceive.getInstance().receiveMessage(receiveData);
  }

  gotoSDK(data: Record<string, string>, func: string) {
    LogUtil.info('receive init 参数：' + JSON.stringify(data));
    switch (func) {
      case 'receive_init':
        HLMainWorker.getInstance().receive_init(data);
        break;
      case 'receive_login':
        HLMainWorker.getInstance().receive_login(data);
        break;
      case 'receive_logout':
        HLMainWorker.getInstance().receive_logout(data);
        break;
      case 'receive_exitGame':
        HLMainWorker.getInstance().receive_exitGame(data);
        break;
      case 'receive_pay':
        HLMainWorker.getInstance().receive_pay(data);
        break;
      case 'receive_account':
        HLMainWorker.getInstance().receive_account(data);
        break;
      case 'receive_switchAccount':
        HLMainWorker.getInstance().receive_switchAccount(data);
        break;
      case 'receive_report':
        HLMainWorker.getInstance().receive_report(data);
        break;

      default:
        LogUtil.info('找不到方法：' + func);
        break;
    }
  }
}