/*
 * @Author: XianKaiQun
 * @Date: 2020-04-02 11:22:29
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-09-21 10:31:12
 * @Descripttion: 函数式调用modal弹出层
 */
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WisModalUtil {
  ///显示
  static Future<T?> custom<T>({
    WillPopCallback? onWillPop,
    Widget? child,
  }) async {
    {
      return await WNavUtil.instance!.push(
        PageRouteBuilder(
            opaque: false,
            transitionDuration: const Duration(milliseconds: 150),
            pageBuilder: (context, animation, secondaryAnimation) {
              return WillPopScope(
                onWillPop: onWillPop != null ? onWillPop : () async => false,
                child: child!,
              );
            }),
      );
    }
  }

  ///显示消息提示框
  ///
  ///会返回一个bool值
  ///
  ///点击确定的时候  返回 true
  ///
  ///点击取消的时候  返回 false
  ///
  ///[showCancel] 是否显示取消按钮
  ///
  ///[data]      内容
  static Future<bool> show(String data,
      {String title = '提示',
      bool showCancel = true,
      bool showTitle = true,
      String? cancelText = '取消',
      Color? cancelColor = Colors.grey,
      String? confirmText = '确定',
      Color? confirmColor,
      bool? isInputForm = false,
      Widget? form}) async {
    {
      return await showWidget(
        title: title,
        child: isInputForm! ? form : Text(data),
        showCancel: showCancel,
        showTitle: showTitle,
        cancelText: cancelText!,
        cancelColor: cancelColor!,
        confirmText: confirmText!,
        confirmColor: confirmColor,
      );
    }
  }

  ///显示消息提示框
  ///
  ///会返回一个bool值
  ///
  ///点击确定的时候  返回 true
  ///
  ///点击取消的时候  返回 false
  ///
  ///[showCancel] 是否显示取消按钮
  ///
  ///[child]      内容
  static Future<bool> showWidget({
    String title = '提示',
    required Widget? child,
    bool showCancel = true,
    bool showTitle = true,
    String? cancelText = '取消',
    Color cancelColor = Colors.grey,
    String? confirmText = '确定',
    Color? confirmColor,
  }) async {
    var completer = Completer<bool>();
    await WNavUtil.instance!.push(
      PageRouteBuilder(
          opaque: false,
          transitionDuration: const Duration(milliseconds: 150),
          pageBuilder: (context, animation, secondaryAnimation) {
            ///标题
            Widget titleW = Container(
              padding: EdgeInsets.symmetric(vertical: 16),
              alignment: Alignment.center,
              child: Text(
                title,
                style: TextStyle(
                  fontSize: 18,
                  color: Colors.black,
                ),
                maxLines: 1,
              ),
            );

            ///内容
            Widget contentW = Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
              constraints: BoxConstraints(
                minHeight: 75,
              ),
              child: DefaultTextStyle(
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 15,
                  color: Colors.grey,
                ),
                child: child ?? Wisdom(),
              ),
            );

            ///取消
            Widget btnLeftW = Expanded(
              child: TextButton(
                onPressed: () {
                  completer.complete(false);
                  Navigator.pop(context);
                },
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 14),
                  alignment: Alignment.center,
                  child: Text(
                    cancelText ?? '取消',
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.grey,
                    ),
                  ),
                ),
              ),
            );

            ///确定
            Widget btnRightW = Expanded(
              child: TextButton(
                onPressed: () {
                  completer.complete(true);
                  Navigator.pop(context);
                },
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 14),
                  alignment: Alignment.center,
                  child: Text(
                    confirmText ?? '确定',
                    style: TextStyle(
                      fontSize: 15,
                      color: Theme.of(context).indicatorColor,
                    ),
                  ),
                ),
              ),
            );
            return WillPopScope(
              onWillPop: () async => false,
              child: Container(
                alignment: Alignment.center,
                color: Colors.black26,
                child: Container(
                  width: 300,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: DefaultTextStyle(
                    style: TextStyle(
                      decoration: TextDecoration.none,
                      fontWeight: FontWeight.w400,
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        if (showTitle) titleW,
                        contentW,
                        Container(
                          height: 50,
                          decoration: BoxDecoration(
                            border: Border(
                              top: BorderSide(color: Colors.grey, width: 0.3),
                            ),
                          ),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: <Widget>[
                              if (showCancel == true) btnLeftW,
                              if (showCancel == true) VerticalDivider(),
                              btnRightW,
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            );
          }),
    );
    // showDialog<bool>(
    //   context: rd.context,
    //   barrierDismissible: false,
    //   builder: (BuildContext context) {

    //   },
    // );
    return completer.future;
  }
}
