package com.fujica.abk.component;

import com.fujica.abk.ResourceTable;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

/**
 * 授权确认对话框
 */
public class AuthorizationDialog extends CommonDialog {
    private Context context;
    private OnAuthorizationListener listener;

    public AuthorizationDialog(Context context) {
        super(context);
        this.context = context;
        initDialog();
    }

    /**
     * 初始化对话框
     */
    private void initDialog() {
        setAutoClosable(true);
        setTransparent(true);
        
        // 加载自定义布局
        Component dialogComponent = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_authorization, null, false);
        setContentCustomComponent(dialogComponent);

        // 授权按钮
        Button btnAuthorize = (Button) dialogComponent.findComponentById(ResourceTable.Id_btn_authorize);
        if (btnAuthorize != null) {
            btnAuthorize.setClickedListener(v -> {
                if (listener != null) {
                    listener.onAuthorized();
                }
                destroy();
            });
        }

        // 取消按钮
        Button btnBack = (Button) dialogComponent.findComponentById(ResourceTable.Id_btn_back);
        if (btnBack != null) {
            btnBack.setClickedListener(v -> {
                if (listener != null) {
                    listener.onCancelled();
                }
                destroy();
            });
        }

        // 设置对话框关闭监听
        setDestroyedListener(() -> {
            // 如果对话框被其他方式关闭（如点击外部），也视为取消
            if (listener != null) {
                // 这里可以根据需要决定是否调用 onCancelled
            }
        });
    }

    /**
     * 设置授权监听器
     */
    public void setOnAuthorizationListener(OnAuthorizationListener listener) {
        this.listener = listener;
    }

    /**
     * 授权监听器接口
     */
    public interface OnAuthorizationListener {
        /**
         * 用户确认授权
         */
        void onAuthorized();

        /**
         * 用户取消授权
         */
        void onCancelled();
    }
}

