import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class Remind {
  static void show(BuildContext context, String title, String message,
      {String confirm = '确认',
        String cancel = '取消',
        Color? confirmColor,
        Function(bool)? onTap}) async {

    final theme = Theme.of(context);
    final activeColor = theme.colorScheme.primary;
    await showDialog<bool>(
      context: context,
      barrierDismissible: false, // 设置为 false 表示点击外部不会关闭弹窗
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(35.w), // 设置圆角半径
          ),
          contentPadding: EdgeInsets.all(20), // 设置弹窗内边距
          title: Text(
            title,
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            textAlign: TextAlign.center, // 自定义标题文本居中
          ),
          content: Column(
            mainAxisSize: MainAxisSize.min, // 确保内容区域紧凑
            children: [
              Text(
                message,
                textAlign: TextAlign.center, // 内容文本居中
                style: TextStyle(fontSize: 16),
              ),
              SizedBox(height: 20), // 设置内容与按钮之间的间距
              _buildButtonRow(
                showCancel: true,
                showConfirm: true,
                cancelText: cancel,
                confirmText: confirm,
                confirmColor: confirmColor ?? activeColor,
                context: context,
                onTap: onTap,
                close: true,
              ),
            ],
          ),
        );
      },
    );
  }

  static void showWidget(BuildContext context, String title, Widget widget,
      {String confirmTxt = '确认',
        String cancelTxt = '取消',
        bool showConfirm = true,
        bool showCancel = true,
        Color? confirmColor,
        Function(bool)? onTap,
        bool close = true,
        RemindControl? control}) async {

    final theme = Theme.of(context);
    final activeColor = theme.colorScheme.primary;
    await showDialog<bool>(
      context: context,
      barrierDismissible: false, // 设置为 false 表示点击外部不会关闭弹窗
      builder: (BuildContext context) {
        if (control != null) {
          control._close = () {
            if (ModalRoute.of(context)?.isCurrent ?? false) {
              Navigator.pop(context);
            }
          };
        }
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(35.w), // 设置圆角半径
          ),
          contentPadding: EdgeInsets.all(20), // 设置弹窗内边距
          title: Text(
            title,
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            textAlign: TextAlign.center, // 自定义标题文本居中
          ),
          content: Column(
            mainAxisSize: MainAxisSize.min, // 确保内容区域紧凑
            children: [
              widget,
              SizedBox(height: 20), // 设置内容与按钮之间的间距
              _buildButtonRow(
                showCancel: showCancel,
                showConfirm: showConfirm,
                cancelText: cancelTxt,
                confirmText: confirmTxt,
                confirmColor: confirmColor ?? activeColor,
                context: context,
                onTap: onTap,
                close: close,
              ),
            ],
          ),
        );
      },
    );
  }

  static Widget _buildButtonRow({
    required bool showCancel,
    required bool showConfirm,
    required String cancelText,
    required String confirmText,
    required Color confirmColor,
    required Function(bool)? onTap,
    required BuildContext context,
    required bool close,
  }) {
    // 如果两个按钮都显示，使用 spaceEvenly 布局
    if (showCancel && showConfirm) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          TextButton(
            onPressed: () {
              if (onTap != null) {
                onTap(false);
              }
              if (close) {
                Navigator.of(context).pop(false);
              }
            },
            child: Text(cancelText),
            style: TextButton.styleFrom(
              foregroundColor: Colors.black45,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 12),
            ),
          ),
          TextButton(
            onPressed: () {
              if (onTap != null) {
                onTap(true);
              }
              if (close) {
                Navigator.of(context).pop(true);
              }
            },
            child: Text(confirmText),
            style: TextButton.styleFrom(
              foregroundColor: confirmColor,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 12),
            ),
          ),
        ],
      );
    }
    // 如果只显示取消按钮，居中显示
    else if (showCancel && !showConfirm) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TextButton(
            onPressed: () {
              if (onTap != null) {
                onTap(false);
              }
              if (close) {
                Navigator.of(context).pop(false);
              }
            },
            child: Text(cancelText),
            style: TextButton.styleFrom(
              foregroundColor: Colors.black45,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 12),
            ),
          ),
        ],
      );
    }
    // 如果只显示确认按钮，居中显示
    else if (!showCancel && showConfirm) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TextButton(
            onPressed: () {
              if (onTap != null) {
                onTap(true);
              }
              if (close) {
                Navigator.of(context).pop(true);
              }
            },
            child: Text(confirmText),
            style: TextButton.styleFrom(
              foregroundColor: confirmColor,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 12),
            ),
          ),
        ],
      );
    }
    // 如果两个都不显示，返回空的 SizedBox
    else {
      return SizedBox();
    }
  }
}

class RemindControl {
  Function? _close;

  void close() {
    _close!();
  }
}

// 需要添加一个全局的 navigatorKey
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();