
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';

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({ required String title, String? message, Widget? content, ModalFunc? onConfirm, ModalFunc? onCancel }) {
  Get.dialog(
    Modal(
      type: 'dialog',
      title: title,
      message: message,
      content: content,
      onConfirm: () => _handleFunc(onConfirm),
      onCancel: () => _handleFunc(onCancel),
    )
  );
}

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

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(0xFFFF703B);
  final _btnSize = Size(90.w, 36.w);
  final _btnShape = RoundedRectangleBorder(
    side: const BorderSide(color: Color(0xFFFF703B), width: 2.0),
    borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
  );

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

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

  Widget _confirmBtn() {
    return ElevatedButton(
      child: Text("确定", style: TextStyle(color: Colors.white, fontSize: 18.sp, letterSpacing: 5.sp)),
      style: ElevatedButton.styleFrom(
          primary: _primaryColor,
          elevation: 0,
          minimumSize: _btnSize,
          shape: _btnShape,
      ),
      onPressed: _handleConfirm,
    );
  }

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

  Widget _alert() {
    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),
          Center(
            child: _confirmBtn(),
          ),
          SizedBox(height: 10.w),
        ]
    );
  }

  Widget _dialog() {
    Widget hSpacer = SizedBox(width: 16.w);

    return SimpleDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
        titleTextStyle: _titleStyle,
        title: Text(title, textAlign: TextAlign.center),
        alignment: Alignment.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: [
              hSpacer,
              Center(
                child: _confirmBtn(),
              ),
              hSpacer,
              Center(
                child: _cancelBtn(),
              ),
              hSpacer,
            ],
          ),
          SizedBox(height: 10.w),
        ]
    );
  }

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

}
