import 'package:flutter/Material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../../../../../data/bean/option_select_config.dart';
import '../../../../../resource/colors.gen.dart';
import '../widget/reply_tone_option_list.dart';

/// 回复语气选项Attach锚点弹窗
class ReplyToneOptionAttachDialog {
  /// 用于标识弹窗的唯一性
  static const _tag = "ReplyToneOptionAttachDialog";

  /// 弹窗是否显示中
  static bool _isShowing = false;

  /// 弹窗是否显示中
  static bool isShowing() {
    return _isShowing;
  }

  /// 显示
  /// [anchorWidgetKey] 锚点组件的GlobalKey
  /// [replyToneOptionSelectConfigList] 回复语气列表
  /// [onSelectOptionCallback] 选择时回调
  static void show(
    GlobalKey anchorWidgetKey,
    BuildContext context, {
    required List<OptionSelectConfig> replyToneOptionSelectConfigList,
    required OnSelectOptionCallback onSelectOptionCallback,
  }) {
    if (_isShowing) {
      return;
    }

    SmartDialog.showAttach(
      // 绑定唯一tag
      tag: _tag,
      // 绑定锚点的BuildContext，才能确定弹窗的显示位置
      targetContext: context,
      // 在锚点的顶部显示
      alignment: Alignment.bottomCenter,
      // 使用动画
      useAnimation: true,
      // 动画类型
      animationType: SmartAnimationType.fade,
      // 是否允许事件穿透
      usePenetrate: false,
      // 透明遮罩
      maskColor: ColorName.transparent,
      // 监听弹窗关闭
      onDismiss: () {
        _isShowing = false;
      },
      // 构建弹窗内容
      builder: (_) {
        return Container(
          // 固定弹窗的宽度
          width: double.infinity,
          margin: EdgeInsets.symmetric(horizontal: 22.w),
          padding: EdgeInsets.only(left: 14.w, right: 14.w, top: 14.h),
          decoration: BoxDecoration(
            color: ColorName.white,
            border: Border.all(color: ColorName.white, width: 1.0.w),
            borderRadius: BorderRadius.all(Radius.circular(16.r)),
          ),
          child: ReplyToneOptionList(
            replyToneOptionSelectConfigList: replyToneOptionSelectConfigList,
            onSelectOptionCallback: onSelectOptionCallback,
          ),
        );
      },
    );

    // 设置为正在显示中
    _isShowing = true;
  }

  /// 隐藏
  static void dismiss() {
    if (!_isShowing) {
      return;
    }
    SmartDialog.dismiss(tag: _tag);
  }
}
