/*
 * @Author: XianKaiQun
 * @Date: 2020-09-08 16:50:55
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-08-18 15:03:22
 * @Description: 
 */

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

///[builder] 构造视图
///[popTrigger] 触发弹窗关闭，传入回调的参数，
///使用了[WillPopScope],必须通过此函数才能关闭弹窗
typedef WModalBuider<T> = Widget Function(
  BuildContext context,
  void Function(T argument) popTrigger,
);

///Modal弹窗
class WModalUtil {
  WModalUtil._();

  ///需要自己构造Modal视图，包括朦胧层。
  ///
  ///[builder] 构造视图，
  ///[popTrigger] 触发弹窗关闭，传入回调的参数，
  ///使用了[WillPopScope],必须通过此函数才能关闭弹窗
  static Future<T?> custom<T>({
    WModalBuider? builder,
  }) async {
    bool willPop = false;
    void popTrigger<T>(T argument) {
      willPop = true;
      WNavUtil.instance!.pop<T>(argument);
    }

    return await WNavUtil.instance!.push<T>(
      PageRouteBuilder<T>(
        opaque: false,
        transitionDuration: const Duration(milliseconds: 150),
        pageBuilder: (context, animation, animation2) {
          return WillPopScope(
            onWillPop: () async => willPop,
            child: builder!(context, popTrigger),
          );
        },
      ),
    );
  }

  ///显示modal弹窗
  ///
  ///路由关闭为同步事件，
  ///构造内容中均为[Widget]，
  ///用此类函数可以添加一些自定义的图标、自定义文本样式等
  static Future<bool?> showFormWidgetSync({
    bool? canCancel = true,
    Widget? title,
    Widget? content,
    Widget? cancelText = const Text('取消'),
    Widget? confirmText = const Text('确认'),
  }) {
    canCancel ??= false;
    cancelText ??= const Text('取消');
    confirmText ??= const Text('确认');
    return custom(
      builder: (context, popTrigger) {
        final colorScheme = WTheme.of(context).colorScheme;
        void onConfirm() => popTrigger(true);
        void onCancel() => popTrigger(false);
        return WModal(
          header: title,
          content: content,
          footers: [
            if (canCancel == true)
              WButton(
                height: 44.pt,
                color: colorScheme.background,
                textColor: colorScheme.text,
                onPressed: onCancel,
                child: cancelText,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(24.5.pt),
                ),
              ).nestExpanded(),
            if (canCancel == true) SizedBox(width: 15.pt),
            WButton(
              height: 44.pt,
              color: Color(0XFF0F67F8),
              onPressed: onConfirm,
              child: confirmText,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(24.5.pt),
              ),
            ).nestExpanded()
          ],
        );
      },
    );
  }

  ///快捷显示modal弹窗
  ///
  ///路由关闭为同步事件，
  ///相比[showFormWidgetSync],构造参数均为[String]类型
  static Future<bool?> showSync(
    String content, {
    String? title,
    bool canCancel = true,
    String cancelText = '取消',
    String confirmText = '确认',
  }) {
    return showFormWidgetSync(
      canCancel: canCancel,
      content: WisText('$content'),
      title: title != null ? Text('$title') : null,
      cancelText: WisText(cancelText),
      confirmText: WisText(confirmText),
    );
  }
}
