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

enum DialogPosition {
  top,
  center,
  bottom,
}

enum DialogAnimation {
  /// 全部位置都为渐隐动画
  fade,

  /// 全部位置都为缩放动画
  scale,

  /// 中间位置的为渐隐动画, 其他位置为位移动画
  centerFade_otherSlide,

  /// 中间位置的为缩放, 其他位置为位移动画
  centerScale_otherSlide,
}

class DialogEngine {
  //展示依附弹窗
  static Future showAttach({
    required BuildContext? targetContext,
    required Widget widget,
    DialogPosition position = DialogPosition.center,
    DialogAnimation? animType,
    VoidCallback? onDismiss,
    String? tag,
    Color? maskColor,
    bool? clickMaskDismiss,
    bool? backDismiss,
    bool? keepSingle,
  }) {
    Alignment alignment;
    if (position == DialogPosition.bottom) {
      alignment = Alignment.bottomCenter;
    } else if (position == DialogPosition.top) {
      alignment = Alignment.topCenter;
    } else {
      alignment = Alignment.center;
    }

    SmartAnimationType? smartAnimType;
    if (animType == DialogAnimation.fade) {
      smartAnimType = SmartAnimationType.fade;
    } else if (animType == DialogAnimation.scale) {
      smartAnimType = SmartAnimationType.scale;
    } else if (animType == DialogAnimation.centerFade_otherSlide) {
      smartAnimType = SmartAnimationType.centerFade_otherSlide;
    } else if (animType == DialogAnimation.centerScale_otherSlide) {
      smartAnimType = SmartAnimationType.centerScale_otherSlide;
    }

    return SmartDialog.showAttach(
      targetContext: targetContext,
      builder: (context) => widget,
      usePenetrate: false,
      debounce: true,
      clickMaskDismiss: clickMaskDismiss,
      backDismiss: backDismiss,
      animationType: smartAnimType,
      alignment: alignment,
      onDismiss: onDismiss,
      tag: tag,
      maskColor: Colors.transparent,
      keepSingle: keepSingle,
    );
  }

  /// 展示默认弹窗
  static Future show({
    required Widget widget,
    DialogPosition position = DialogPosition.center,
    DialogAnimation? animType,
    VoidCallback? onDismiss,
    String? tag,
    Color? maskColor,
    bool? clickMaskDismiss,
    bool? backDismiss,
    bool? keepSingle,
  }) {
    Alignment alignment;
    if (position == DialogPosition.bottom) {
      alignment = Alignment.bottomCenter;
    } else if (position == DialogPosition.top) {
      alignment = Alignment.topCenter;
    } else {
      alignment = Alignment.center;
    }

    SmartAnimationType? smartAnimationType;
    if (animType == DialogAnimation.fade) {
      smartAnimationType = SmartAnimationType.fade;
    } else if (animType == DialogAnimation.scale) {
      smartAnimationType = SmartAnimationType.scale;
    } else if (animType == DialogAnimation.centerFade_otherSlide) {
      smartAnimationType = SmartAnimationType.centerFade_otherSlide;
    } else if (animType == DialogAnimation.centerScale_otherSlide) {
      smartAnimationType = SmartAnimationType.centerScale_otherSlide;
    }

    return SmartDialog.show(
      builder: (context) => widget,
      alignment: alignment,
      usePenetrate: false,
      debounce: true,
      clickMaskDismiss: clickMaskDismiss,
      backDismiss: backDismiss,
      animationType: smartAnimationType,
      onDismiss: onDismiss,
      tag: tag,
      maskColor: maskColor,
      keepSingle: keepSingle,
    );
  }

  // 隐藏
  static void dismiss({String? tag}) {
    SmartDialog.dismiss(status: SmartStatus.dialog, tag: tag);
  }
}