import { LogUtil } from "fastsdk";
import { worker } from "@kit.ArkTS";

export class UnityApiBridge {
  initSDK() {
    let SDKData: Record<string, string> = {
      'params': '{}',
      'func': "receive_init"
    };
    this.sendMessage(SDKData)
  }

  login() {
    LogUtil.info('HLPlatform login invoke');
    let SDKData: Record<string, string> = {
      'params': '{}',
      'func': "receive_login"
    };
    this.sendMessage(SDKData)
  }

  logout() {
    let SDKData: Record<string, string> = {
      'params': '{}',
      'func': "receive_logout"
    };
    this.sendMessage(SDKData);
  }

  switchAccount() {
    let SDKData: Record<string, string> = {
      'params': '{}',
      'func': "receive_switchAccount"
    };
    this.sendMessage(SDKData);
  }

  exit() {
    let SDKData: Record<string, string> = {
      'params': '{}',
      'func': "receive_exitGame"
    };
    this.sendMessage(SDKData)
  }

  startPay(parm: string) {
    let paramsData: Record<string, string> = {
      'payData': parm
    }
    let SDKData: Record<string, string | object> = {
      'params': paramsData,
      'func': "receive_pay"
    };
    this.sendMessage(SDKData);
  }

  sendEvent(eventType: number, jsonStr: string) {
    let data: Record<string, string> = {
      'type': `${eventType}`,
      'content': jsonStr
    };
    let SDKData: Record<string, string | object> = {
      'params': data,
      'func': "receive_report"
    };
    this.sendMessage(SDKData);
  }

  accessParticipate() {

  }

  openAccount() {
    let SDKData: Record<string, string | number> = {
      'params': '{}',
      'func': "receive_account"
    };
    this.sendMessage(SDKData)
  }

  shareData() {

  }

  sendMessage(data: Record<string, string | number | object>) {
    let parent = worker.workerPort
    parent.postMessage({ type: 'HL_CUSTOM_ON_TUI_THREAD', data: data })
  }
}

export function RegisterHLPlatform() {
  let register: Record<string, Object> = {};
  register["UnityApiBridge"] = new UnityApiBridge();
  return register;
}