import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:worker_client/widgets/GradientButton.dart';

typedef ModalFunc = dynamic Function();

/// func 函数如果返回 bool 值, 则只有 true 的情况下才默认关闭弹窗
/// 如果返回 Future , 则只有正常结束的时候 才关闭弹窗
/// 其余情况, 都默认关闭弹窗
void _handleFunc(ModalFunc? func) {
  if (null != func) {
    dynamic res = func();
    if (res.runtimeType == bool) {
      if (res) {
        _closeModal();
      }
    } else if (res is Future) {
      res.then((_) => _closeModal());
    } else {
      _closeModal();
    }
  } else {
    _closeModal();
  }
}

/// 打开一个 dialog
void showDialog(
    {String? title,
    String? message,
    Widget? content,
    ModalFunc? onConfirm,
    }) {
  Get.dialog(Modal(
    type: 'dialog',
    title: title,
    message: message,
    content: content,
    onConfirm: () => _handleFunc(onConfirm),
  ));
}

/// 打开一个 alert
void showAlert(
    {required String title,
    String? message,
    Widget? content,
    ModalFunc? onConfirm,
      ModalFunc? onCancel
    }) {
  Get.dialog(Modal(
    type: 'alert',
    title: title,
    message: message,
    content: content,
    onConfirm: () => _handleFunc(onConfirm),
    onCancel: () => _handleFunc(onCancel),
  ));
}

void _closeModal() {
  // Get.back(closeOverlays: true);
  Navigator.of(Get.overlayContext!, rootNavigator: true).pop();
}

class Modal extends StatelessWidget {
  String? title;
  String type;
  String? message;
  Widget? content;
  VoidCallback? onConfirm;
  VoidCallback? onCancel;

  final _titleStyle = TextStyle(
      color: const Color(0xFF222222),
      fontSize: 20.sp,
      fontWeight: FontWeight.bold);
  final _messageStyle =
      TextStyle(color: const Color(0xFF666666), fontSize: 18.sp);
  final _primaryColor = const Color(0xFF5BD583);
  final _btnSize = Size(100.w, 49.h);
  final _btnShape = RoundedRectangleBorder(
    side: const BorderSide(color: Color(0xFF00AE39), width: 1),
    borderRadius: BorderRadius.all(Radius.circular(17.w)),
  );

  void _handleConfirm() {
    if (null != onConfirm) {
      onConfirm!();
    }
  }

  void _handleCancel() {
    if (null != onCancel) {
      onCancel!();
    }
  }

  Modal({
    Key? key,
    this.type = 'alert',
    this.title,
    this.message,
    this.content,
    this.onConfirm,
    this.onCancel,
  }) : super(key: key);

  Widget _confirmBtn() {
    return SizedBox(
      width: 100.w,
      height: 49.h,
      child: GradientButton(
          colors: [Color(0xFFA0E067), Color(0xFF00AE39)],
          borderRadius: BorderRadius.all(Radius.circular(17.w)),
          onPressed: _handleConfirm,
          child: Text(
            "确认",
            style: TextStyle(
                fontSize: 17.sp, fontWeight: FontWeight.bold, letterSpacing: 2),
          )),
    );
  }

  Widget _cancelBtn() {
    return ElevatedButton(
      child: Text("取消",
          style: TextStyle(
              color: _primaryColor, fontSize: 17.sp,letterSpacing: 2)),
      style: ElevatedButton.styleFrom(
        primary: Colors.white,
        elevation: 0,
        minimumSize: _btnSize,
        shape: _btnShape,
      ),
      onPressed: _handleCancel,
    );
  }

  Widget _alert() {
    Widget hSpacer = SizedBox(width: 16.w);
    return SimpleDialog(
        shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
        titleTextStyle: _titleStyle,
        title: Text(title!, textAlign: TextAlign.center),
        children: [
          if (null != message)
            Text(message!, textAlign: TextAlign.center, style: _messageStyle),
          if (null != content) content!,
          SizedBox(height: 16.w),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _confirmBtn(),
              hSpacer,
              _cancelBtn()
            ],
          )
        ]);
  }

  Widget _dialog() {
    return SimpleDialog(
        shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
        titleTextStyle: _titleStyle,
        alignment: Alignment.center,
        children: [
          SizedBox(height: 50.h),
          if (null != message)
            Text(message!, textAlign: TextAlign.center, style: _messageStyle),
          if (null != content) content!,
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _confirmBtn(),
            ],
          ),
          SizedBox(height: 50.h)
        ]);
  }

  @override
  Widget build(BuildContext context) {
    return type == 'alert' ? _alert() : _dialog();
  }
}
