import { AbilityConstant, UIAbility, Want,  } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window, UIContext } from '@kit.ArkUI';
import PaymentManager from '../Manager/PaymentManger'
import  { UPPayResult, UPPayResultStatus } from 'uppayment'

export default class EntryAbility extends UIAbility {

  funcAbilityWant: Want | undefined = undefined;
  uiContext: UIContext | undefined = undefined;


  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.funcAbilityWant = want;
  }

  handleWant() {
    if (this.funcAbilityWant?.uri && this.uiContext) {

      PaymentManager.handlePaymentResult(this.funcAbilityWant.uri,(data: UPPayResult) => {
        switch (data.code) {
          case UPPayResultStatus.CANCEL:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '取消！',
              alignment:DialogAlignment.Center
            });
            break;
          case UPPayResultStatus.FAIL:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '失败！',
              alignment:DialogAlignment.Center
            });
            break;
          case UPPayResultStatus.SUCCESS:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '成功！',
              alignment:DialogAlignment.Center
            });
            break;

          default:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '取消！',
              alignment:DialogAlignment.Center
            });
            break;
        }
        console.log('支付结果：' + JSON.stringify(data));
      });

    }
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      let windowClass: window.Window;
      windowStage.getMainWindow((err, data) => {
        if (err.code) {
          console.error(`Failed to obtain the main window. Code is ${err.code}, message is ${err.message}`);
          return;
        }
        windowClass = data;
        this.uiContext = windowClass.getUIContext();
        this.handleWant();
      })
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }

  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
    if (want?.uri && want.uri.includes('uppayresult') && this.uiContext) {
      PaymentManager.handlePaymentResult(want.uri,(data: UPPayResult) => {
        switch (data.code) {
          case UPPayResultStatus.CANCEL:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '取消！',
              alignment:DialogAlignment.Center
            });
            break;
          case UPPayResultStatus.FAIL:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '失败！',
              alignment:DialogAlignment.Center
            });
            break;
          case UPPayResultStatus.SUCCESS:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '成功！',
              alignment:DialogAlignment.Center
            });
            break;

          default:
            this.uiContext?.showAlertDialog({
              title: '支付结果',
              message: '取消！',
              alignment:DialogAlignment.Center
            });
            break;
        }
        console.log('支付结果：' + JSON.stringify(data));
      });

    }
  }
}
