package com.fujica.abk.utils;

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

/**
 * 自定义确认对话框
 * 
 * 使用示例：
 * <pre>
 * // 基础用法
 * new ConfirmDialog(context)
 *     .setTitle("温馨提示")
 *     .setMessage("确定要退出登录吗？")
 *     .setConfirmListener(dialog -> {
 *         // 执行确定操作
 *         dialog.dismiss();
 *     })
 *     .setCancelListener(dialog -> {
 *         // 执行取消操作
 *         dialog.dismiss();
 *     })
 *     .show();
 * 
 * // 自定义按钮文字
 * new ConfirmDialog(context)
 *     .setMessage("是否删除该记录？")
 *     .setConfirmText("删除")
 *     .setCancelText("保留")
 *     .setConfirmListener(dialog -> {
 *         deleteRecord();
 *         dialog.dismiss();
 *     })
 *     .show();
 * </pre>
 * 
 * 特点：
 * - 使用 XML 布局文件，取消按钮在左，确定按钮在右
 * - 确定按钮使用 button.xml（橙色），取消按钮使用 button_cancel.xml（灰色）
 * - 弹框宽度固定 280vp，背景半透明黑色遮罩
 * - 支持链式调用，使用方便
 */
public class ConfirmDialog {
    
    private Context context;
    private CommonDialog dialog;
    private Component rootView;
    private Text titleText;
    private Text messageText;
    private Button confirmButton;
    private Button cancelButton;
    
    private String title = "提示";
    private String message = "确认要执行此操作吗？";
    private String confirmText = "确定";
    private String cancelText = "取消";
    private OnClickListener confirmListener;
    private OnClickListener cancelListener;
    
    /**
     * 点击监听器接口
     */
    public interface OnClickListener {
        void onClick(ConfirmDialog dialog);
    }
    
    public ConfirmDialog(Context context) {
        this.context = context;
        initDialog();
    }
    
    /**
     * 初始化对话框
     */
    private void initDialog() {
        dialog = new CommonDialog(context);
        
        // 加载自定义布局
        rootView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_confirm_dialog, null, false);
        
        // 查找组件
        titleText = (Text) rootView.findComponentById(ResourceTable.Id_dialog_title);
        messageText = (Text) rootView.findComponentById(ResourceTable.Id_dialog_message);
        confirmButton = (Button) rootView.findComponentById(ResourceTable.Id_btn_confirm);
        cancelButton = (Button) rootView.findComponentById(ResourceTable.Id_btn_cancel);
        
        // 设置按钮点击事件
        confirmButton.setClickedListener(component -> {
            if (confirmListener != null) {
                confirmListener.onClick(this);
            } else {
                dismiss();
            }
        });
        
        cancelButton.setClickedListener(component -> {
            if (cancelListener != null) {
                cancelListener.onClick(this);
            } else {
                dismiss();
            }
        });
        
        // 设置对话框内容
        dialog.setContentCustomComponent(rootView);
        
        // 设置对话框属性
        dialog.setTransparent(true); // 设置透明背景
        dialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, 
                      ComponentContainer.LayoutConfig.MATCH_PARENT);
    }
    
    /**
     * 设置标题
     */
    public ConfirmDialog setTitle(String title) {
        this.title = title;
        if (titleText != null) {
            titleText.setText(title);
        }
        return this;
    }
    
    /**
     * 设置消息内容
     */
    public ConfirmDialog setMessage(String message) {
        this.message = message;
        if (messageText != null) {
            messageText.setText(message);
        }
        return this;
    }
    
    /**
     * 设置确定按钮文本
     */
    public ConfirmDialog setConfirmText(String text) {
        this.confirmText = text;
        if (confirmButton != null) {
            confirmButton.setText(text);
        }
        return this;
    }
    
    /**
     * 设置取消按钮文本
     */
    public ConfirmDialog setCancelText(String text) {
        this.cancelText = text;
        if (cancelButton != null) {
            cancelButton.setText(text);
        }
        return this;
    }
    
    /**
     * 设置确定按钮点击监听器
     */
    public ConfirmDialog setConfirmListener(OnClickListener listener) {
        this.confirmListener = listener;
        return this;
    }
    
    /**
     * 设置取消按钮点击监听器
     */
    public ConfirmDialog setCancelListener(OnClickListener listener) {
        this.cancelListener = listener;
        return this;
    }
    
    /**
     * 显示对话框
     */
    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }
    
    /**
     * 关闭对话框
     */
    public void dismiss() {
        if (dialog != null) {
            dialog.hide();
        }
    }
    
    /**
     * 销毁对话框
     */
    public void destroy() {
        if (dialog != null) {
            dialog.destroy();
            dialog = null;
        }
    }
}

