import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:sk_base_mobile/widgets/core/sk_bottomsheet_picker.dart';

class ModalUtil {
  static Future<bool> alert(
      {String? title,
      String? contentText,
      String? confirmText,
      Widget? content,
      VoidCallback? onConfirm,
      Widget Function(BuildContext)? builder,
      EdgeInsetsGeometry? contentPadding,
      bool showActions = true,
      bool barrierDismissible = true,
      bool showCancel = true}) async {
    bool confirmed = false;
    await showDialog(
      context: Get.overlayContext!,
      barrierDismissible: barrierDismissible,
      builder: builder ??
          (context) {
            return AlertDialog(
              clipBehavior: Clip.hardEdge,
              backgroundColor: AppTheme.nearlyWhite,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(ScreenAdaper.sp(20)),
              ),
              actionsPadding: EdgeInsets.zero,
              titlePadding: EdgeInsets.zero,
              contentPadding: contentPadding ??
                  EdgeInsets.symmetric(
                    vertical: ScreenAdaper.height(10),
                  ),
              title: title != null
                  ? Container(
                      padding: EdgeInsets.symmetric(
                        vertical: ScreenAdaper.height(20),
                      ),
                      decoration: const BoxDecoration(
                          border: Border(
                              bottom:
                                  BorderSide(color: AppTheme.dividerColor))),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          const Icon(
                            Icons.error_outline,
                            color: AppTheme.dangerColor,
                          ),
                          SizedBox(
                            width: ScreenAdaper.width(10),
                          ),
                          Text(
                            title,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontWeight: FontWeight.w500,
                              fontSize: ScreenAdaper.height(30),
                              color: AppTheme.black,
                            ),
                          )
                        ],
                      ),
                    )
                  : null,
              content: content ??
                  (contentText != null
                      ? Container(
                          padding: EdgeInsets.symmetric(
                            vertical: ScreenAdaper.height(20),
                          ),
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom: BorderSide(
                                      color: AppTheme.dividerColor,
                                      width: ScreenAdaper.height(2)))),
                          child: Text(
                            // '${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title}${title} ${title}${title}${title}${title}',
                            contentText,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontSize: ScreenAdaper.height(30),
                                fontWeight: FontWeight.w500),
                          ))
                      : null),
              actions: !showActions
                  ? null
                  : [
                      Row(
                        children: [
                          if (showCancel)
                            Expanded(
                                child: InkWell(
                              onTap: () {
                                Navigator.of(context).pop();
                                confirmed = false;
                              },
                              child: Container(
                                  padding: EdgeInsets.symmetric(
                                      vertical: ScreenAdaper.height(15)),
                                  alignment: Alignment.center,
                                  child: Text(
                                    '取消',
                                    style: TextStyle(
                                        color: Colors.black,
                                        fontSize: ScreenAdaper.height(30)),
                                  )),
                            )),
                          Expanded(
                              child: InkWell(
                            onTap: () {
                              if (onConfirm != null) onConfirm();
                              Navigator.pop(context);
                              confirmed = true;
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(
                                  vertical: ScreenAdaper.height(15)),
                              alignment: Alignment.center,
                              child: Text(
                                confirmText ?? '确定',
                                style: TextStyle(
                                    color: Colors.black,
                                    fontSize: ScreenAdaper.height(30)),
                              ),
                            ),
                          )),
                        ],
                      )
                    ],
            );
          },
    );
    return confirmed;
  }

  /// 底部选择器，支持3级
  static Future<void> showBottomSheetPicker(
          {title = '请选择',
          required onChanged,
          required firstLevel,
          secondLevel,
          thirdLevel,
          itemHeight,
          popupHeight}) async =>
      showCupertinoModalPopup(
        context: Get.overlayContext!,
        builder: (BuildContext context) {
          return SkBottomSheetPicker(
              title: title,
              onChanged: onChanged,
              firstLevel: firstLevel,
              secondLevel: secondLevel,
              thirdLevel: thirdLevel,
              itemHeight: itemHeight ?? 80.0,
              popupHeight: popupHeight ?? 400);
        },
      );

  static Future<T?> showGeneralDialog<T>(
      {required Widget content,
      double? width,
      double? height,
      Offset? offset}) {
    return Get.generalDialog<T>(
      barrierLabel: "generalDialog",
      barrierDismissible: true,
      transitionDuration: const Duration(milliseconds: 400),
      pageBuilder: (_, __, ___) {
        return SafeArea(
            child: ScreenAdaper.isTablet()
                ? Center(
                    child: ClipRRect(
                        borderRadius:
                            BorderRadius.circular(ScreenAdaper.sp(30)),
                        child: Material(
                          child: SizedBox(
                            height:
                                height ?? Get.height - ScreenAdaper.height(150),
                            width: width ?? Get.width - ScreenAdaper.width(150),
                            child: content,
                          ),
                        )))
                : Container(
                    margin: EdgeInsets.only(top: Get.height / 10),
                    child: ClipRRect(
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(ScreenAdaper.sp(30)),
                          topRight: Radius.circular(ScreenAdaper.sp(30))),
                      child: Material(
                        child: content,
                      ),
                    )));
      },
      transitionBuilder: (_, anim, __, child) {
        Tween<Offset> tween;
        tween = Tween(begin: offset ?? const Offset(0, 1), end: Offset.zero);
        return SlideTransition(
          position: tween.animate(
            CurvedAnimation(parent: anim, curve: Curves.easeInOut),
          ),
          child: child,
        );
      },
    );
  }
}
