package com.app.rewardapp.restApi;

import static com.ads.androidsdk.sdk.util.Tool.vpn;
import static com.app.rewardapp.util.Constant_Api.Authorization;
import static com.app.rewardapp.util.Constant_Api.CONTENT_TYPE;
import static com.app.rewardapp.util.Constant_Api.isVpn;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.ads.androidsdk.sdk.util.Tool;
import com.app.rewardapp.MyApplication;
import com.app.rewardapp.util.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {
    public  static Retrofit retrofit;
    private static final String CACHE_CONTROL = "Cache-Control";

    public static Retrofit getClient(Activity context) {
        Session session;
        session = new Session(context);

        if (retrofit == null) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.level(HttpLoggingInterceptor.Level.BODY);

            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(chain -> {
                        Request request;
                        request = chain.request().newBuilder()
                                .addHeader("Accept", CONTENT_TYPE)
                                .addHeader(Authorization, session.getData(session.TOKEN))
                                .build();
                        return chain.proceed(request);
                    })
                    .addInterceptor(logging)
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .build();

            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                    .create();

            retrofit = new Retrofit.Builder()
                    .baseUrl(WebApi.Api.BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }

        if(isVpn && vpn()){
            Toast.makeText(context, "Do not Use VPN", Toast.LENGTH_SHORT).show();
            return null;
        } else {
            return retrofit;
        }
    }
}
