package com.sambath.sbc.app

import android.content.Context
import android.content.Intent
import com.sambath.sbc.screen.login.LoginActivity
import com.sambath.sbc.util.PrefHelper
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

class ErrorInterceptor(private val context: Context, private val prefHelper: PrefHelper) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {

        val request: Request = chain.request()
        val response = chain.proceed(request)
        when (response.code) {
//            400 -> {
//                //Show Bad Request Error Message
//            }
            401 -> {
                //Show UnauthorizedError Message
                    prefHelper.logout()
                    val i =  Intent(context, LoginActivity::class.java)
                    i.flags=Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                    context.startActivity(i)
            }

//            403 -> {
//                //Show Forbidden Message
//            }
//
//            404 -> {
//                //Show NotFound Message
//            }

            // ... and so on

        }
        return response
    }
}