/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-19 15:34:22
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-28 15:26:56
 * @FilePath: \marking_app\lib\utils\easy_refresh\ss.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:marking_app/common/config/request_config.dart';
import 'package:marking_app/common/model/common/base_page_data_report.dart';
import 'package:marking_app/common/model/common/base_page_report.dart';
import 'package:marking_app/common/model/common/base_structure_result_report.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/request/rest_dio.dart';

mixin RefreshDataHandleReport<T, U extends BasePageReport> {
  // 获取DIO
  Future<Dio> getDio() async => await RestDio().getDio();

  void toDataCatchHandle(EasyRefreshController controller, bool isReFresh, U params) {
    setTimeOut(100, () {
      if (!isReFresh) {
        params.page -= 1; // 加载出错，返回一页
        controller.finishLoad();
      } else {
        controller.finishRefresh();
      }
    });
  }

  void toDataSuccessHandle(EasyRefreshController controller, bool isReFresh, U params, List data) {
    bool noMore = data.isEmpty || data.length < params.limit;
    setTimeOut(100, () {
      if (isReFresh) {
        controller.finishRefresh();
        if (noMore) {
          controller.finishLoad(noMore: noMore);
        }
      } else {
        controller.finishLoad(noMore: noMore);
      }
    });
  }

  // ignore: avoid_shadowing_type_parameters
  Future<BasePageDataReport<T>?> toRefreshData<T>(EasyRefreshController controller,
      {required Future<BaseStructureResultReport<BasePageDataReport<T>>> Function(U) api,
      required U params,
      required BuildContext context,
      List<String>? pathParams,
      bool isReFresh = false}) async {
    if (isReFresh) {
      params.page = RequestConfig.basePage.page;
      params.limit = RequestConfig.basePage.limit;
    }

    try {
      BaseStructureResultReport<BasePageDataReport<T>> result = await api(params);
      if (result.code == 200) {
        BasePageDataReport<T> pageData = result.data!;

        toDataSuccessHandle(controller, isReFresh, params, pageData.items);

        return pageData;
      }
      throw Exception(result.message ?? '上拉加载下拉刷新失败');
    } catch (e) {
      debugPrint('上拉下拉错误===: ${e.toString()}');
      toDataCatchHandle(controller, isReFresh, params);
    }
    return null;
  }

  // ignore: avoid_shadowing_type_parameters
  Future<BasePageDataReport<T>?> toRefreshDataWithPath(EasyRefreshController controller,
      {required Future<BaseStructureResultReport<BasePageDataReport<T>>> Function(String, U) api,
      required U params,
      required BuildContext context,
      required String pahtVal,
      bool isReFresh = false}) async {
    if (isReFresh) {
      params.page = RequestConfig.basePage.page;
      params.limit = RequestConfig.basePage.limit;
    }

    try {
      BaseStructureResultReport<BasePageDataReport<T>> result = await api(pahtVal, params);
      if (result.code == 200) {
        BasePageDataReport<T> pageData = result.data!;
        toDataSuccessHandle(controller, isReFresh, params, pageData.items);

        return pageData;
      }
      throw Exception(result.message ?? '上拉加载下拉刷新失败');
    } catch (e) {
      debugPrint("上拉下拉执行异常,${e}");
      toDataCatchHandle(controller, isReFresh, params);
    }
  }
}
