import 'package:dio/dio.dart';
import 'package:telnow_mobile_new/src/injector/injector.dart';
import 'package:telnow_mobile_new/src/model/refreshtoken/refresh_token_body.dart';
import 'package:telnow_mobile_new/src/model/token/token.dart';
import 'package:telnow_mobile_new/src/layouts/components/template.dart';
import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
import 'package:telnow_mobile_new/src/utils/u.dart';
import 'package:event_bus/event_bus.dart';

EventBus eventBus = EventBus();
class DioLoggingInterceptors extends InterceptorsWrapper {
  final Dio _dio;
  final SharedPreferencesManager _sharedPreferencesManager = locator<SharedPreferencesManager>();

  DioLoggingInterceptors(this._dio);

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    if (options.headers.containsKey('requirestoken')) {
      // await U.waitForRefreshAuth(); //bikin stuck
      // return;
      options.headers.remove('requirestoken');
      String? accessToken = _sharedPreferencesManager.getString(SharedPreferencesManager.keyAccessToken);
      options.headers.addAll({'Authorization': 'Bearer $accessToken'});
    }

    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    super.onResponse(response,handler);
  }

  @override
  void onError(DioError dioError, ErrorInterceptorHandler handler) async {
    // print(dioError.response?.data.toString());
    // print(dioError.response?.realUri.toString());
    try {
      if (dioError.response?.statusCode == 401) {
        // print("auth error");
        bool? checkRefreshCount = _sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyCountRefreshToken);
        int? refreshCount = _sharedPreferencesManager.getInt(SharedPreferencesManager.keyCountRefreshToken);
        if(checkRefreshCount! && refreshCount! > 2){
          // handlingError(NavigationService.navigatorKey.currentContext, 3);
          eventBus.fire("logout");
          return;
        }

        // if (dioError.response!.data.toString().contains("Invalid refresh token")) {
        //   handlingError(NavigationService.navigatorKey.currentContext, 3);
        // }
        if (dioError.response!.data.toString().contains("Access token expired")) {
          // print("token expired");

          String? refreshToken = _sharedPreferencesManager.getString(SharedPreferencesManager.keyRefreshToken)!;
          RefreshTokenBody? refreshTokenBody = RefreshTokenBody('refresh_token', refreshToken);
          Token? token = await U.refreshAuth(refreshTokenBody);
          if (token != null) {
            RequestOptions? options = dioError.response?.requestOptions;
            options?.headers.addAll({'requirestoken': true});
            try {
              var r = await _dio.fetch(options!);
              handler.resolve(r);
              await _sharedPreferencesManager.putInt(SharedPreferencesManager.keyCountRefreshToken, 0);
              return;
            } catch (error2) {
              print(error2);
            }
          }
        }
        await _sharedPreferencesManager.putInt(SharedPreferencesManager.keyCountRefreshToken, refreshCount!+1);
      }
    } catch (error) {
      print("Error dio: ${error.toString()}");
      eventBus.fire("logout");
      // handlingError(NavigationService.navigatorKey.currentContext, 3);
    }
    super.onError(dioError, handler);
  }
}