import UIAbility from "@ohos:app.ability.UIAbility";
import type AbilityConstant from "@ohos:app.ability.AbilityConstant";
import type Want from "@ohos:app.ability.Want";
import hilog from "@ohos:hilog";
import type window from "@ohos:window";
import type { UIContext } from "@ohos:arkui.UIContext";
import PaymentManager from "@bundle:com.unionpay.ohos.paydemo/entry/ets/manager/PaymentManger";
import { UPPayResultStatus } from "@package:pkg_modules/.ohpm/uppayment@gx7nzjcazfb3kvk6jc+phqy7h84=/pkg_modules/uppayment/Index";
import type { UPPayResult } from "@package:pkg_modules/.ohpm/uppayment@gx7nzjcazfb3kvk6jc+phqy7h84=/pkg_modules/uppayment/Index";
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));
            });
        }
    }
}
