import { AbilityConstant, common, Want } from "@kit.AbilityKit";
import { fastSdk, InitResult, LogUtil } from "fastsdk";
import { window } from "@kit.ArkUI";
import { processMgr } from "fastsdk/src/main/ets/utils/PageManager";
import { GoodsInfo } from "fastsdk/src/main/ets/model/GoodsInfo";
import { LoginResult } from "fastsdk/src/main/ets/model/LoginResult";
import { PayParams } from "fastsdk/src/main/ets/model/PayParams";
import { PlayerInfo } from "fastsdk/src/main/ets/model/PlayerInfo";
import { DataType } from "core";
import { Tuanjie } from "./utils/TuanjieNative";

class SdkPlugin {
  private static instance: SdkPlugin | null = null
  private uiContext?: UIContext;

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

  //---------------lifecycle event--------------
  public onCreate(want: Want, launchParam: AbilityConstant.LaunchParam, context: common.UIAbilityContext): void {
    fastSdk.onCreate(want, launchParam, context);
  }

  public loadContentSuccess(windowStage: window.WindowStage): void {
    fastSdk.loadContentSuccess(windowStage);
    this.uiContext = AppStorage.get<UIContext>('uiContext');
  }

  public onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    fastSdk.onNewWant(want, launchParam);
  }

  public onDestory(): void {
    fastSdk.onDestory();
  }

  //---------------lifecycle event--------------

  private register(): void {
    fastSdk.hlSystemListener = {
      onInitSuccess: (result: InitResult) => {
        LogUtil.info('初始化成功:' + JSON.stringify(result));
        let jsonString = JSON.stringify(result);
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onInitSuccess", jsonString);
      },
      onInitFailed: (reason: string) => {
        LogUtil.error('初始化失败:' + reason);
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onInitFailed", reason);
      },
      onCustomExit: () => {
        LogUtil.info('自定义退出流程触发');
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onCustomExit", "");

        // this.uiContext?.showAlertDialog({
        //   title: '自定义退出',
        //   message: '确定退出游戏？',
        //   autoCancel: true,
        //   alignment: DialogAlignment.Center,
        //   buttons: [{
        //     value: "再玩一会",
        //     action: () => {
        //       LogUtil.info("点击了再玩一会");
        //     }
        //   }, {
        //     enabled: true,
        //     defaultFocus: true,
        //     style: DialogButtonStyle.HIGHLIGHT,
        //     value: '确定退出',
        //     action: () => {
        //       //自定义退出弹窗
        //       LogUtil.info('自定义退出成功');
        //       fastSdk.hlSystemListener?.onExitSuccess("");
        //     }
        //   }]
        // })
      },
      onExitSuccess: (result: string) => {
        LogUtil.info('退出成功:' + result);
        processMgr.exit(0)
      }
    };
    fastSdk.hlAccountListener = {
      onRefreshUser: (result: LoginResult): void => {
        //暂不使用
      },
      onLoginSuccess: (result: LoginResult): void => {
        LogUtil.info('登录成功:' + JSON.stringify(result));
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onLoginSuccess", JSON.stringify(result));
      },
      onLoginFailed: (reason: string): void => {
        LogUtil.info('登录失败:' + reason);
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onLoginFailed", reason);
      },
      onLogout: (): void => {
        LogUtil.info('已登出');
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onLogout", "");
      }
    };

    fastSdk.hlPaymentListener = {
      onPaySuccess: (result: string): void => {
        LogUtil.info(`支付成功:${result}`);
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onPaySuccess", result);
      },
      onPayFailed: (reason: string): void => {
        LogUtil.info(`支付失败:${reason}`);
        Tuanjie.TuanjieSendMessage("PlatformCallback", "onPayFailed", reason);
      },
      onQuerySuccess: (products: GoodsInfo[]): void => {
        //暂不使用
      }
    }
  }

  public initSDK() {
    this.register();
    fastSdk.initSDK();
  }
  public login(){
    fastSdk.login();
  }
  public logout(){
    fastSdk.logout();
  }

  public switchAccount(){
    fastSdk.switchAccount();
  }

  public exit(){
    fastSdk.exit();
  }

  public pay(payParms:PayParams){
    fastSdk.pay(payParms);
  }

  public openAccountCenter(){
    fastSdk.openAccountCenter();
  }

  public report(eventType: DataType, playerInfo: PlayerInfo){
    fastSdk.report(eventType,playerInfo);
  }
}

export const SdkBridge = SdkPlugin.getInstance();