package com.rdiot.yx485.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rdiot.yx485.R;

public class DialogUtils {

    /**
     * 显示提醒弹窗
     *
     * @param context          上下文
     * @param title            标题
     * @param message          内容
     * @param positiveText     确定按钮文字
     * @param negativeText     取消按钮文字
     * @param positiveListener 确定按钮点击事件
     * @param negativeListener 取消按钮点击事件
     * @return AlertDialog 实例
     */
    public static AlertDialog showAlertDialog(Context context,
                                              String title,
                                              String message,
                                              String positiveText,
                                              String negativeText,
                                              DialogInterface.OnClickListener positiveListener,
                                              DialogInterface.OnClickListener negativeListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(positiveText, positiveListener)
                .setNegativeButton(negativeText, negativeListener)
                .setCancelable(false);

        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }

    /**
     * 显示提醒弹窗（简化版，使用默认按钮文字）
     *
     * @param context          上下文
     * @param title            标题
     * @param message          内容
     * @param positiveListener 确定按钮点击事件
     * @param negativeListener 取消按钮点击事件
     * @return AlertDialog 实例
     */
    public static AlertDialog showAlertDialog(Context context,
                                              String title,
                                              String message,
                                              DialogInterface.OnClickListener positiveListener,
                                              DialogInterface.OnClickListener negativeListener) {
        return showAlertDialog(context, title, message, "确定", "取消", positiveListener, negativeListener);
    }

    /**
     * 显示提醒弹窗（只带确定按钮）
     *
     * @param context          上下文
     * @param title            标题
     * @param message          内容
     * @param positiveListener 确定按钮点击事件
     * @return AlertDialog 实例
     */
    public static AlertDialog showAlertDialog(Context context,
                                              String title,
                                              String message,
                                              DialogInterface.OnClickListener positiveListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", positiveListener)
                .setCancelable(false);

        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }

    /**
     * 显示提醒弹窗（只带确定按钮，使用默认点击事件）
     *
     * @param context 上下文
     * @param title   标题
     * @param message 内容
     * @return AlertDialog 实例
     */
    public static AlertDialog showAlertDialog(Context context,
                                              String title,
                                              String message) {
        return showAlertDialog(context, title, message, (dialog, which) -> dialog.dismiss());
    }

    public static void showInputDialog(Activity activity, String title, String oldValue, String hint, OnConfirmClickListener onConfirmClick) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        LayoutInflater inflater = activity.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_add_room, null);
        builder.setView(dialogView);

        final EditText editText = dialogView.findViewById(R.id.room_name);
        editText.setHint(hint);
        if (!TextUtils.isEmpty(oldValue)) {
            editText.setText(oldValue);
        }
        TextView btnCancel = dialogView.findViewById(R.id.btn_cancel);
        TextView btnOk = dialogView.findViewById(R.id.btn_ok);
        TextView tvTitle = dialogView.findViewById(R.id.tv_title);

        // 创建对话框实例
        final AlertDialog dialog = builder.create();  // 需要定义为 final 以便在监听器中使用
        tvTitle.setText(title);  // 标题应该设置在 dialog 上

        btnCancel.setOnClickListener(v -> {
            dialog.dismiss();  // 直接关闭对话框
        });

        btnOk.setOnClickListener(v -> {
            if (onConfirmClick != null) {
                String content = editText.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(activity, editText.getHint().toString(), Toast.LENGTH_SHORT).show();
                    return;
                }
                onConfirmClick.onConfirm(content);
            }

            dialog.dismiss();  // 正确关闭方式
        });

        dialog.show();
    }

    /**
     * 创建加载弹窗
     *
     * @param context 上下文
     * @param message 提示信息
     * @return Dialog 实例
     */
    public static Dialog createLoadingDialog(Context context, String message) {
        Dialog dialog = new Dialog(context, R.style.LoadingDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
        TextView tvMessage = view.findViewById(R.id.tv_message);
        tvMessage.setText(message);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    public interface OnConfirmClickListener {
        void onConfirm(String content);
    }
} 