import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class EAAlertDialog {
  static Future<T?> show<T>({
    String? title,
    String? contentText,
    Widget? contentWidget,
    String? confirmText,
    String? cancelText,
    VoidCallback? cancelOnTap,
    VoidCallback? confirmOnTap,
    VoidCallback? onDismiss,
  }) {
    return SmartDialog.show<T>(
      builder: (_) {
        return _EAAlertDialog(
            title: title,
            content: contentText,
            contentWidget: contentWidget,
            confirmText: confirmText,
            cancelText: cancelText,
            confirmOnTap: confirmOnTap,
            cancelOnTap: cancelOnTap);
      },
      tag: "EAAlertDialog",
      onDismiss: onDismiss,
      clickMaskDismiss: false,
    );
  }

  static void dismiss<T>({T? result}) {
    SmartDialog.dismiss(tag: "EAAlertDialog", result: result);
  }
}

class _EAAlertDialog extends Dialog {
  // 标题
  final String? title;

  final Widget? contentWidget;

  // 内容
  final String? content;

  // 取消按钮文字
  final String? cancelText;

  // 取消按钮回调
  final VoidCallback? cancelOnTap;

  // 确认按钮文字
  final String? confirmText;

  // 确认按钮文字回调
  final VoidCallback? confirmOnTap;

  const _EAAlertDialog({
    this.title,
    this.content,
    this.contentWidget,
    required this.cancelText,
    required this.confirmText,
    this.cancelOnTap,
    this.confirmOnTap,
  });

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          const Spacer(),

          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(vertical: 24.h, horizontal: 16.w),
            width: ScreenUtil().screenWidth - 80.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(12.w)),
              color: "#FFFFFF".toColor(),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                /// 标题
                if (title != null)
                  Text(
                    title!,
                    style: TextStyle(
                      color: "#25262A".toColor(),
                      fontSize: 15.sp,
                      fontWeight: FontWeight.w500,
                      decoration: TextDecoration.none,
                    ),
                  ),

                if (title != null) SizedBox(height: 12.h),

                if (content != null)

                  /// 内容
                  Text(
                    content ?? "",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: "#5F5F61".toColor(),
                      fontWeight: FontWeight.w500,
                      fontSize: 14.sp,
                      decoration: TextDecoration.none,
                    ),
                  ),

                if (contentWidget != null) contentWidget!,

                SizedBox(height: 34.h),

                ///
                _buttonWidget(context),
              ],
            ),
          ),

          //
          const Spacer(),
        ],
      ),
    );
  }

  Widget _buttonWidget(BuildContext context) {
    if (cancelText == null) {
      return Row(
        children: [
          const Spacer(),

          ///确认按钮
          GestureDetector(
            onTap: () {
              _clickConfirm();
            },
            child: _button(text: confirmText ?? "", color: "#6177F2".toColor()),
          ),

          //
          const Spacer(),
        ],
      );
    } else {
      return Row(
        children: [
          ///  取消按钮
          Expanded(
            child: InkWell(
              onTap: () {
                _clickCancel();
              },
              child: _button(
                text: cancelText ?? "",
                color: "#F0F0F0".toColor(),
                textColor: "#5F5F61".toColor(),
              ),
            ),
          ),

          ///
          const SizedBox(width: 8),

          /// 确认按钮
          Expanded(
            child: InkWell(
              onTap: () {
                _clickConfirm();
              },
              child:
                  _button(text: confirmText ?? "", color: "#6177F2".toColor()),
            ),
          ),
        ],
      );
    }
  }

  Widget _button({
    required String text,
    Color? color,
    BoxBorder? border,
    Color? textColor = Colors.white,
  }) {
    return Container(
      height: 36.w,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(8.w)),
        color: color,
        border: border,
      ),
      alignment: Alignment.center,
      child: Text(
        text,
        style: TextStyle(
          color: textColor,
          fontSize: 14.sp,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }

  void _clickConfirm() {
    if (confirmOnTap != null) {
      confirmOnTap!();
    }

    EAAlertDialog.dismiss();
  }

  void _clickCancel() {
    if (cancelOnTap != null) {
      cancelOnTap!();
    }

    EAAlertDialog.dismiss();
  }
}
