import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import '../components/commonFun.dart';

//int my_count = 0; //重试次数

class MyDelayToast {
  int showTime; // MyDelayToast显示时间
  double top;
  double left;
  double width;
  double height;

  OverlayEntry _overlayEntry; // MyDelayToast靠它加到屏幕上
  Timer _timer;
  double minEdge = 2.5;

  MyDelayToast(
      {@required BuildContext context,
      this.showTime = 5,
      this.top = 100,
      this.left = 20,
      this.width = 80,
      this.height = 50}) {
    OverlayState overlayState = Overlay.of(context);
    startTimer();
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(
        builder: (BuildContext context) {
          return Positioned(
            top: top,
            left: left,
            child: Draggable(
              child: getView(context),
              feedback: getView(context),
              onDragStarted: () {
                print('onDragStarted:');
              },
              // onDragEnd: (detail) {
              //   print('onDragEnd:${detail.offset}');
              //   getView(top: detail.offset.dy, left: detail.offset.dx);
              // },
              onDraggableCanceled: (Velocity velocity, Offset _offset) {
                //ScreenUtil().statusBarHeight  //Top Status bar height , Notch will be higher
                //ScreenUtil().bottomBarHeight  //Bottom safe zone distance, suitable for buttons with full screen
                top = _offset.dy <= ScreenUtil().statusBarHeight
                    ? ScreenUtil().statusBarHeight + minEdge
                    : (_offset.dy <
                            ScreenUtil().screenHeight - height - ScreenUtil().bottomBarHeight)
                        ? _offset.dy
                        : (ScreenUtil().screenHeight -
                            height -
                            ScreenUtil().bottomBarHeight -
                            minEdge);
                left = _offset.dx < minEdge
                    ? minEdge
                    : (_offset.dx < ScreenUtil().screenWidth - width - minEdge)
                        ? _offset.dx
                        : (ScreenUtil().screenWidth - width - minEdge);
                getView(context);
                _overlayEntry.markNeedsBuild();
              },
              childWhenDragging: Container(),
            ),
          );
        },
      );
      overlayState.insert(_overlayEntry);
    } else {
      //重新绘制UI，类似setState
      _overlayEntry.markNeedsBuild();
    }
  }

  Widget getView(BuildContext context, {double width = 80, double height = 50}) {
    return GestureDetector(
      child: Container(
        width: width,
        height: height,
        padding: EdgeInsets.all(0),
        color: Theme.of(context).buttonColor,
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
          Text('第 ${getCount} 次获取 ',
              style: TextStyle(
                fontFamily: Theme.of(context).accentTextTheme.caption.fontFamily,
                fontSize: 13,
                color: Colors.black,
                decoration: TextDecoration.none,
                fontWeight: FontWeight.normal,
              ),
              textAlign: TextAlign.center),
          Text('（${showTime} 秒）',
              style: TextStyle(
                fontFamily: Theme.of(context).accentTextTheme.caption.fontFamily,
                fontSize: 13,
                color: Colors.black,
                decoration: TextDecoration.none,
                fontWeight: FontWeight.normal,
              ),
              textAlign: TextAlign.center),
        ]),
      ),
      onTap: () {},
      onDoubleTap: () {
        endMyDelayToast();
      },
      onLongPress: () {
        endMyDelayToast();
      },
    );
  }

  endMyDelayToast() async {
    _timer?.cancel();
    _overlayEntry.markNeedsBuild();
    await Future.delayed(Duration(milliseconds: 400));
    _overlayEntry.remove();
    _overlayEntry = null;
  }

  void startTimer() {
    _timer = Timer.periodic(
      Duration(seconds: 1),
      (Timer timer) {
        if (showTime < 1) {
          timer.cancel();
          endMyDelayToast();
        } else {
          if (!getingDwVideo) {
            endMyDelayToast();
          }
          showTime--;
          // if (showTime % 3 == 0) {
          //   getCount++;
          // }
          _overlayEntry.markNeedsBuild();
        }
      },
    );
  }
}
