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

import 'package:yumi/app/routes/sc_fluro_navigator.dart';

typedef OnFutureFunc = Future<dynamic> Function();
typedef OnSuccess = Function(dynamic v);

class _DialogLoadingIndicator extends StatelessWidget {
  const _DialogLoadingIndicator();

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 32.w,
      height: 32.w,
      child: const CircularProgressIndicator(
        strokeWidth: 3,
        valueColor: AlwaysStoppedAnimation<Color>(Color(0xffFF50BC)),
      ),
    );
  }
}

class STLoadingDialog extends Dialog {
  String text;
  Widget? centerContent;
  final GestureTapCallback? verifyCallback;
  final double? height;
  final double? width;

  STLoadingDialog({
    Key? key,
    required this.text,
    this.height,
    this.width,
    this.centerContent,
    this.verifyCallback,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: new Center(
        //保证控件居中效果
        child: new Container(
          constraints: BoxConstraints(maxWidth: ScreenUtil().screenWidth * 0.4),
          decoration: ShapeDecoration(
            color: Color(0xffffffff),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(8.0),
              ),
            ),
          ),
          child: AspectRatio(
            aspectRatio: 1,
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                const _DialogLoadingIndicator(),
                Text(
                  "${text ?? "loading..."}",
                  style: TextStyle(fontSize: 14.sp, color: Color(0xff888888)),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class FutureLoadingDialog extends StatefulWidget {
  String text;
  Widget? centerContent;
  final GestureTapCallback? verifyCallback;
  final double? height;
  final double? width;
  final OnFutureFunc future;
  final OnSuccess? onSuccess;
  final OnSuccess? onError;

  FutureLoadingDialog({
    Key? key,
    this.text = "",
    this.height,
    this.width,
    this.centerContent,
    this.verifyCallback,
    required this.future,
    this.onSuccess,
    this.onError,
  })  : assert(future != null),
        super(key: key);

  @override
  _FutureLoadingDialogState createState() => _FutureLoadingDialogState();
}

class _FutureLoadingDialogState extends State<FutureLoadingDialog> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widget.future.call().then((value) {
      SCNavigatorUtils.goBack(context);
      widget.onSuccess?.call(value);
    }).catchError((e) {
      SCNavigatorUtils.goBack(context);
      widget.onError?.call(e);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: new Center(
        //保证控件居中效果
        child: new Container(
          constraints: BoxConstraints(maxWidth: ScreenUtil().screenWidth * 0.4),
          decoration: ShapeDecoration(
            color: Color(0xffffffff),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(8.0),
              ),
            ),
          ),
          child: AspectRatio(
            aspectRatio: 1,
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                const _DialogLoadingIndicator(),
                Text(
                  "${widget.text ?? "loading..."}",
                  style: TextStyle(fontSize: 14.sp, color: Color(0xff888888)),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
