package com.doverfuelingsolutions.issp.api.basic

import com.doverfuelingsolutions.issp.utils.log.DFSLog
import com.google.gson.Gson
import okhttp3.Interceptor
import okhttp3.Response
import java.util.*

class LoggingInterceptor : Interceptor {

    private var index = 1

    override fun intercept(chain: Interceptor.Chain): Response {
        if (index >= 1000) index = 1
        val currentIndex = index
        index++

        val request = chain.request()
        val time = System.currentTimeMillis()
        DFSLog.v("=====>>>>> request[$currentIndex]: ${request.method()} ${request.url()}")

        val response = chain.proceed(request)
        val body = response.body()
        val stringBody = if (body == null) "null" else response.peekBody(Long.MAX_VALUE).string()
        DFSLog.v("=====>>>>> response[$currentIndex]: ${System.currentTimeMillis() - time}ms", "code = ${response.code()}", "body = $stringBody")
        return response
    }
}