import { StringUtils,LogUtil } from "fastsdk";
import { PayParams } from "fastsdk/src/main/ets/model/PayParams";
import { JSON } from "@kit.ArkTS";
import { PlayerInfo } from "fastsdk/src/main/ets/model/PlayerInfo";
import { DataType } from "core";
import { SdkBridge } from "./SdkPlugin";

export class HLMainWorker {
  private static instance: HLMainWorker | null = null;

  private constructor() {
  }

  //单例
  public static getInstance(): HLMainWorker {
    if (!HLMainWorker.instance) {
      HLMainWorker.instance = new HLMainWorker();
    }
    return HLMainWorker.instance;
  }

  receive_init(data: Record<string, string>) {
    SdkBridge.initSDK();
  }

  receive_login(data: Record<string, string>) {
    SdkBridge.login();
  }

  receive_logout(data: Record<string, string>) {
    SdkBridge.logout();
  }

  receive_pay(data: Record<string, string>) {
    const payData = data['payData'];
    if (StringUtils.isEmpty(payData)) {
      LogUtil.error("Unity start pay jsonStr is empty! please check all params.");
      return;
    }
    try {
      let payParam: PayParams = JSON.parse(payData) as PayParams;
      LogUtil.info(`pay params:${JSON.stringify(payParam)}`);
      const itemId = payParam.itemId;
      if (StringUtils.isEmpty(itemId)) {
        LogUtil.error("Unity start pay jsonStr itemId is empty! please check all params.");
        return;
      }
      const itemName = payParam.itemName;
      if (StringUtils.isEmpty(itemName)) {
        LogUtil.error("Unity start pay jsonStr itemName is empty! please check all params.");
        return;
      }
      const amount = payParam.amount;
      if (amount === 0) {
        LogUtil.error("Unity start pay jsonStr amount is empty! please check all params.");
        return;
      }
      const count = payParam.count;
      if (count === 0) {
        payParam.count = 1;
      }
      const callbackInfo = payParam.callbackInfo;
      if (StringUtils.isEmpty(callbackInfo)) {
        LogUtil.error("Unity start pay jsonStr callbackInfo is empty! please check all params.");
        return;
      }
      const currency = payParam.currency;
      if (StringUtils.isEmpty(currency)) {
        payParam.currency = "CNY";
      }
      const notifyUrl = payParam.notifyUrl;
      if (StringUtils.isEmpty(notifyUrl)) {
        payParam.notifyUrl = '';
      }
      SdkBridge.pay(payParam);
    } catch (e) {
      LogUtil.error(e);
    }

  }

  receive_exitGame(data: Record<string, string>) {
    SdkBridge.exit();
  }

  receive_account(data: Record<string, string>) {
    SdkBridge.openAccountCenter();
  }

  receive_switchAccount(data: Record<string, string>) {
    SdkBridge.switchAccount();
  }

  receive_report(data: Record<string, string>) {
    try {
      const eventType: string = data["type"] as string;
      const content: string = data["content"] as string;
      if (StringUtils.isEmpty(content)) {
        LogUtil.error("sendEvent data is empty! please check Data");
        return;
      }
      const dataModel: PlayerInfo = JSON.parse(content) as PlayerInfo;
      let playerInfo = new PlayerInfo();
      playerInfo.roleId = dataModel.roleId;
      playerInfo.roleName = dataModel.roleName;
      playerInfo.roleLevel = dataModel.roleLevel;
      playerInfo.zoneId = dataModel.zoneId;
      playerInfo.zoneName = dataModel.zoneName;
      playerInfo.serverId = dataModel.serverId;
      playerInfo.serverName = dataModel.serverName;
      playerInfo.balance = dataModel.balance;
      playerInfo.vip = dataModel.vip;
      playerInfo.partyName = dataModel.partyName;
      playerInfo.appVersion = dataModel.appVersion;
      playerInfo.appResVersion = dataModel.appResVersion;
      playerInfo.extendAction = dataModel.extendAction;
      playerInfo.roleCreateTime = dataModel.roleCreateTime;
      playerInfo.phylum = dataModel.phylum;
      playerInfo.classField = dataModel.classField;
      playerInfo.extra = dataModel.extra;
      const msgTypeValues = Object.values(DataType).filter(item => typeof item === 'number');
      // for-of 遍历数字枚举值，支持break
      for (const val of msgTypeValues) {
        console.log("枚举值：", val);
        if (val === Number(eventType)) {
          SdkBridge.report(val, playerInfo);
          break;
        }
      }
    } catch (error) {
      LogUtil.error(JSON.stringify(error));
    }
  }
}