import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';

class CustomInterceptor extends InterceptorsWrapper {
  int retryCount = 0;
  @override
  // ignore: avoid_void_async
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    // final String token = LocalStoreManager.getString(UserSettings.tokenUser);
    // if (token.isNotEmpty) options.headers["Authorization"] = "Bearer $token";

    final String method = options.method.toLowerCase();
    if (method == 'get' || method == 'put') {
      // xử lý double request đá .0
      // options.queryParameters.forEach((String key, dynamic value) {
      //   if (value != null && value is double && Utils.getDecimal(value) == 0) {
      //     options.queryParameters[key] = value.toInt();
      //   }
      // });

    }

    return super.onRequest(options, handler);
  }

  @override
  // ignore: always_specify_types
  onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.data.runtimeType.toString().toLowerCase() == "string") {
      response.data = json.decode(response.data);
    }
    try {
      if (response.data["result"] != null) {
        response.data = response.data["result"];
      }
    } catch (_) {}

    if (retryCount > 0) {
      retryCount = 0;
    }
    return super.onResponse(response, handler);
  }

  @override
  onError(DioError err, ErrorInterceptorHandler handler) async {
    if (retryCount >= 3) {
      return;
    }
    if (err.response?.statusCode == 403 || err.response?.statusCode == 401) {
      retryCount++;
      final Dio dio = GetIt.I();
      dio.lock();
      dio.interceptors.requestLock.lock();
      dio.interceptors.responseLock.lock();

      //Refresh token
      // final CoreUserRepository sessionRepository = GetIt.I();
      // final Token? token = await sessionRepository.refreshToken(
      //     clientId: UserSettings.oidcClientId, refreshToken: LocalStoreManager.getString(UserSettings.refreshToken));
      // if (token == null) {
      //   // final AuthenticateApp authenticateApp = GetIt.I();
      //   // await authenticateApp.authenticate(UserSettings.oidcClientId, <String>["profile", "email", "offline_access"]);
      //   await Navigator.pushNamedAndRemoveUntil(
      //       navigatorKey!.currentState!.context, vhs3LoginUser, (Route<dynamic> route) => false);
      // } else {
      //   dio.unlock();
      //   dio.interceptors.requestLock.unlock();
      //   dio.interceptors.responseLock.unlock();
      //   options.headers = <String, dynamic>{
      //     "Content-type": "application/json",
      //     "Authorization": "Bearer ${LocalStoreManager.getString(UserSettings.tokenUser)}"
      //   };
      //   await dio.fetch<dynamic>(options);
      // }
    }

    final dynamic errorData = err.response?.data;
    //&& err.response?.statusCode == 400
    if (errorData != null && errorData["responseException"] != null) {
      final dynamic temp = errorData["responseException"]["exceptionMessage"];
      try {
        if (temp != null && temp["error"] != null) {
          err.response?.data = temp["error"];
        } else {
          err.response?.data = temp;
        }
      } catch (e) {
        err.response?.data = temp;
      }
    }

    return super.onError(err, handler);
  }
}
