package com.sxyz.rfid.retrofit.http;

import com.blankj.utilcode.util.NetworkUtils;
import com.sxyz.rfid.utils.LocalUtils;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class NoNetCacheInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        boolean connected = NetworkUtils.isConnected();
        //如果没有网络，则启用 FORCE_CACHE
        if (!connected) {
            request = request.newBuilder()
                    .addHeader("XX-Device-Type", "android")
                    .addHeader("token", LocalUtils.getLoginModel().getToken()).cacheControl(CacheControl.FORCE_CACHE).build();

            Response response = chain.proceed(request);
            //没网的时候如果也没缓存的话就走网络
            if (response.code() == 504) {
                request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
                return chain.proceed(request);
            }

            return response.newBuilder().removeHeader("Pragma").header("Cache-Control", "public, only-if-cached, max-stale=3600").build();
        }
        //有网络的时候，这个拦截器不做处理，直接返回
        return chain.proceed(request);
    }

}
