package com.sambath.klaklouk_main.app

import com.sambath.klaklouk_main.util.PrefHelper
import okhttp3.Interceptor
import okhttp3.Response

class ServiceInterceptor(private val prefHelper: PrefHelper) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        if (request.header("No-Authentication") == null) {
            //or use Token Function
            if (prefHelper.getToken().isNotEmpty()) {
                val finalToken = prefHelper.getToken()
                val channelType = prefHelper.getChannelType()
                request = request.newBuilder()
                    .addHeader("Authorization", finalToken)
                    .addHeader("channel-type", channelType.toString())
                    .build()
            }
        }else{
            request = request.newBuilder()
                .addHeader("Public-App-Key", "base64:mhpYIvNhSUfNaBydgqJdHIUPBSWCLFoJLQ2ZFNNJkn8=")
                .build()
//            .addHeader("Public-App-Key", "base64:mhpYIvNhSUfNaBydgqJdHIUPBSWCLFoJLQ2ZFNNJkn8=")//"base64:GxsYRGGhhfYgfw+6XoUEw0HJaYzpjMbDsSxnJ7rq7N8=")
        }
        return chain.proceed(request)
    }
}