/*
 * @Author: XianKaiQun
 * @Date: 2020-04-03 10:12:41
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-05-25 15:51:34
 * @Descripttion: 编程式调用吐司提示
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

///吐司位置枚举
enum WToastLoction { top, center, bottom }

class WToastUtil {
  /// 显示吐司提示 ByText
  ///
  ///[data]   文本
  ///
  ///[duration] 持续时间
  ///
  ///[animatedDuration] 动画过渡时间
  static Future show(
    String data, {
    Duration duration = const Duration(milliseconds: 3000),
    Duration animatedDuration = const Duration(milliseconds: 100),
    WToastLoction location = WToastLoction.center,
  }) async {
    return await showWidget(
      child: Text(data),
      duration: duration,
      animatedDuration: animatedDuration,
      location: location,
    );
  }

  /// 显示吐司提示 ByWidget
  ///
  ///[data]   文本
  ///
  ///[duration] 持续时间
  ///
  ///[animatedDuration] 动画过渡时间
  static Future showWidget({
    Widget? child,
    Duration duration = const Duration(milliseconds: 3000),
    Duration animatedDuration = const Duration(milliseconds: 100),
    WToastLoction location = WToastLoction.bottom,
  }) async {

    bool _isShow = false;
    OverlayEntry _overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        double? top;
        double? bottom;

        switch (location) {
          case WToastLoction.center:
            bottom = null;
            top = MediaQuery.of(context).size.height / 2;
            break;
          case WToastLoction.top:
            bottom = null;
            top = 50;
            break;
          default:
            bottom = 150;
            top = null;
        }
        return Positioned(
          top: top,
          left: 25,
          right: 25,
          bottom: bottom,
          child: Center(
            child: DefaultTextStyle(
              style: TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
              textAlign: TextAlign.center,
              child: AnimatedOpacity(
                opacity: _isShow ? 1 : 0,
                duration: animatedDuration,
                child: AnimatedPadding(
                  padding: EdgeInsets.only(
                    bottom: _isShow ? 5 : 0,
                  ),
                  duration: animatedDuration,
                  child: Container(
                    padding: EdgeInsets.all(8),
                    decoration: BoxDecoration(
                      color: Colors.black54,
                      borderRadius: BorderRadius.circular(5),
                    ),
                    child: child,
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
    WNavUtil.instance!.overlay!.insert(_overlayEntry);
    //重新绘制UI，   开始显示
    await Future.delayed(animatedDuration);
    _isShow = true;
    _overlayEntry.markNeedsBuild();
    //重新绘制UI，   开始隐藏
    await Future.delayed(duration);
    _isShow = false;
    _overlayEntry.markNeedsBuild();

    ///延迟动画执行时间后  删掉
    await Future.delayed(animatedDuration);
    _overlayEntry.remove();
  }
}
