package ch.pizzalemon.android;

import androidx.multidex.MultiDexApplication;
import com.onesignal.OneSignal;
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;

import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import ch.pizzalemon.android.api.ApiConstants;
import ch.pizzalemon.android.api.SSLTrustManager;
import io.github.inflationx.calligraphy3.CalligraphyConfig;
import io.github.inflationx.calligraphy3.CalligraphyInterceptor;
import io.github.inflationx.viewpump.ViewPump;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by cimenmus on 11/09/2017.
 */

public class App extends MultiDexApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        initCalligraphy();
        initPicasso();
        initOneSignal();
    }

    private void initCalligraphy(){
        ViewPump.init(ViewPump.builder()
                .addInterceptor(new CalligraphyInterceptor(
                        new CalligraphyConfig.Builder()
                                .setDefaultFontPath("fonts/Quicksand-Regular.ttf")
                                .setFontAttrId(R.attr.fontPath)
                                .build()))
                .build());
    }

    private void initPicasso(){

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .cache(new Cache(getCacheDir(), 48 * 1024 * 1024))
                .connectTimeout(ApiConstants.API_CONNECT_TIMEOUT, java.util.concurrent.TimeUnit.SECONDS)
                .readTimeout(ApiConstants.API_READ_TIMEOUT, java.util.concurrent.TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .addInterceptor(logging);
        addSSLTrustToOkHttp(builder);

        OkHttpClient okHttpClient = builder.build();
        OkHttp3Downloader okHttp3Downloader = new OkHttp3Downloader(okHttpClient);

        Picasso picasso = new Picasso.Builder(this)
                .downloader(okHttp3Downloader)
                .build();
        Picasso.setSingletonInstance(picasso);
    }

    private void initOneSignal(){
        OneSignal.initWithContext(this);
        OneSignal.setAppId(ApiConstants.ONESIGNAL_APP_ID);
        OneSignal.unsubscribeWhenNotificationsAreDisabled(true);
    }

    private void addSSLTrustToOkHttp(OkHttpClient.Builder okHttpClientBuilder) {
        try {
            SSLTrustManager trustManager = new SSLTrustManager();
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{ trustManager }, new SecureRandom());
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            okHttpClientBuilder.sslSocketFactory(sslSocketFactory, trustManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}