/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-19 15:34:22
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-09-27 11:13:01
 * @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:marking_app/common/config/request_config.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/request/rest_client.dart';
import 'package:marking_app/utils/request/rest_client_report.dart';
import 'package:marking_app/utils/request/rest_dio.dart';
import 'package:marking_app/utils/the_global.dart';

mixin CommonMixin {
  // 获取DIO
  Future<Dio> getDio() async => await RestDio().getDio();

  // 获取 CLIENT
  Future<RestClient> getClient() async {
    Dio dio = await getDio();
    return RestClient(dio, baseUrl: RequestConfig().baseUrl);
  }

  // 获取 CLIENT
  Future<RestClientReport> getClientReport() async {
    Dio dio = await getDio();
    return RestClientReport(dio, baseUrl: RequestConfig().baseUrlOfReport);
  }

  void setTimeOut(int seconds, call) => Future.delayed(Duration(seconds: seconds), call);

  // 执行 context
  void getGlobalKey(Function(BuildContext context) call) {
    Future.delayed(const Duration(seconds: 0)).then((onValue) {
      BuildContext context = TheGlobal.navigatorKey.currentState!.overlay!.context;
      call(context);
    });
  }

  // 异常处理信息
  void returnExceptionPrompt(context, err) {
    if (err is DioError) {
      ExceptionHandle.exceptionPrompt(context, err.error);
    }
  }

  void toLoginPage(BuildContext context) {
    RouterManager.router.navigateTo(context, RouterManager.loginPath, clearStack: true, transition: getTransition());
  }
}
