/*
 * @Descripttion:
 * @version:
 * @Author: wy
 * @Date: 2020-06-16 19:35:24
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-28 16:12:13
 */
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyFutureBuilder {
  static Widget toBuilder<T>(context, Future future, Function call) {
    return FutureBuilder(
      future: future,
      builder: (context, snapshot) {
        return call(snapshot);
      },
    );
  }

  // static Widget toBuilderCallMapData(context1, url, call,
  //     {Map<String, dynamic> paras, Widget loding, bool widtToken = true}) {
  //   return FutureBuilder(
  //     future: widtToken
  //         ? Request.post(context1, url, paras)
  //         : Request.postNotToken(context1, url, paras),
  //     builder: (context, snapshot) {
  //       if (snapshot.hasData) {
  //         Map map = _checkAndGetDataMap(context, snapshot);
  //         return call(map);
  //       } else {
  //         return loding ?? getBldLoad(context1);
  //       }
  //     },
  //   );
  // }

  static FutureBuilder<List<T>> buildFutureBuilder<T>(BuildContext context, Future<List<T>> future, Function(List<T>) call) {
    return FutureBuilder<List<T>>(
      builder: (context, AsyncSnapshot<List<T>> async) {
        //在这里根据快照的状态，返回相应的widget
        if (async.connectionState == ConnectionState.active || async.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        }
        if (async.connectionState == ConnectionState.done) {
          // if (async.hasError) {
          //   return const Center(child: Text("ERROR"));
          // } else
          if (async.hasData) {
            List<T> list = async.data ?? [];
            return call(list);
          }
        }
        return getBldLoad(context);
      },
      future: future,
    );
  }

  static FutureBuilder<T?> buildFutureBuilderOfSingleInstance<T>(BuildContext context, Future<T?> future, Function(T?) call, {Widget? widget}) {
    return FutureBuilder<T?>(
      builder: (context, AsyncSnapshot<T?> async) {
        //在这里根据快照的状态，返回相应的widget
        if (async.connectionState == ConnectionState.active || async.connectionState == ConnectionState.waiting) {
          return widget ?? const Center(child: CircularProgressIndicator());
        }
        if (async.connectionState == ConnectionState.done) {
          // if (async.hasData) {
          //   T? data = async.data;
          //   return call(data);
          // }

          T? data = async.data;
          return call(data);
        }
        return getBldLoad(context);
      },
      future: future,
    );
  }

  // static Widget toBuilderCallListData(context, url, paras, call,
  //     {Widget loding, bool widtToken = true}) {
  //   return FutureBuilder(
  //     future: widtToken
  //         ? Request.post(context, url, paras)
  //         : Request.postNotToken(context, url, paras),
  //     builder: (context, snapshot) {
  //       if (snapshot.hasData) {
  //         List list = _checkAndGetDataList(context, snapshot);
  //         return call(list);
  //       } else {
  //         return getBldLoad(context);
  //       }
  //     },
  //   );
  // }

  static Widget getBldLoad(context) {
    return Container(
      alignment: Alignment.center,
      child: Center(
        child: SpinKitCircle(
          color: Theme.of(context).primaryColor,
          size: 100.w,
        ),
      ),
    );
  }

  // static Map _checkAndGetDataMap(context, snapshot) {
  //   int code = snapshot.data['code'];
  //   if (code == 0) {
  //     var res = snapshot.data['result'];
  //     if (!(res is Map)) {
  //       Map resMap = Map();
  //       resMap['data'] = res;
  //       return resMap;
  //     } else {
  //       return res;
  //     }
  //   } else {
  //     Request.getCodeToMas(
  //         context, code.toString(), "${snapshot.data["message"]}",
  //         delayed: true);
  //     return null;
  //   }
  // }

  // static List _checkAndGetDataList(context, snapshot) {
  //   int code = snapshot.data['code'];
  //   if (code == 0) {
  //     return snapshot.data['result'];
  //   } else {
  //     Request.getCodeToMas(
  //         context, code.toString(), "${snapshot.data["message"]}",
  //         delayed: true);
  //     return null;
  //   }
  // }
}
