package com.doverfuelingsolutions.issp.api.basic

import com.doverfuelingsolutions.issp.api.WayneApiConfig
import com.wayne.www.waynelib.util.log.DFSLog
import okhttp3.Interceptor
import okhttp3.Response

class LoggingInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val time = System.currentTimeMillis()

        DFSLog.v(
            "=====>>>>> request done in ${System.currentTimeMillis() - time}ms",
            "method: ${request.method()}",
            "url: ${request.url()}"
        )

        val response = chain.proceed(request)
        val body = response.body()
        val stringBody = if (body == null) "null" else response.peekBody(Long.MAX_VALUE).string()

        var responseLog = "=====>>>>> request done in ${System.currentTimeMillis() - time}ms\n"+
        "method: ${request.method()}\n"+
        "url: ${request.url()}\n"+
        "code: ${response.code()}\n"

        if (!"${request.url()}".contains(WayneApiConfig.URL_SERVICE_DISCOVER)) responseLog += "body: $stringBody \n"

        DFSLog.v(responseLog)
        return response
    }
}