import 'dart:developer';

import 'package:dio/dio.dart';

class DioLogger extends Interceptor {
  bool baseurl;
  bool path;
  bool statuscode;
  bool reqdata;
  bool resdata;
  DioLogger(
      {this.baseurl = false,
      this.path = true,
      this.statuscode = true,
      this.reqdata = true,
      this.resdata = false});

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (baseurl || path || statuscode || reqdata || resdata) {
      log("DioLogger: ____________________________________________________________________________");
      if (baseurl) ("DioLogger: |${response.requestOptions.baseUrl} ");
      if (path) {
        log("DioLogger: |${response.requestOptions.method} ${response.requestOptions.path}");
      }
      if (statuscode) {
        log("DioLogger: |${response.statusCode} ${response.statusMessage} restype:${response.requestOptions.responseType}");
      }
      if (reqdata) log("DioLogger: |reqdata: ${response.requestOptions.data}");
      if (resdata) log("DioLogger: |response: ${response.data}");
      log("DioLogger: ____________________________________________________________________________");
    }
    handler.next(response);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    log("DioLogger: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    log("-----error: ${err.error} ${err.message} restype:${err.requestOptions.responseType}");
    if (baseurl) ("DioLogger: |${err.requestOptions.baseUrl} ");
    if (path) {
      log("DioLogger: |${err.requestOptions.method} ${err.requestOptions.path}");
    }
    if (statuscode) {
      log("DioLogger: |${err.response?.statusCode} ${err.response?.statusMessage}");
    }
    if (reqdata) log("DioLogger: |reqdata: ${err.requestOptions.data}");
    if (resdata) log("DioLogger: |response: ${err.response?.data}");
    log("DioLogger: ____________________________________________________________________________");
    super.onError(err, handler);
  }
}
