package com.cygnusa.hyperm.util;

import androidx.multidex.MultiDexApplication;

import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//import com.cygnusa.hyperm.training.rtc.AgoraEventHandler;


public class MainApplication extends MultiDexApplication {

    Retrofit restClient;
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();


    @Override
    public void onCreate() {
        super.onCreate();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        restClient = new Retrofit.Builder()
                .baseUrl(GlobalData.SERVER_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .readTimeout(60, TimeUnit.SECONDS)
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .addInterceptor(chain -> {
                            Request request = chain.request();
                            HttpUrl url = request.url().newBuilder().build();
                            request = request.newBuilder().url(url).build();
                            return chain.proceed(request);
                        }).addInterceptor(loggingInterceptor)
                        .build())
                .build();

        try {
            // mRtcEngine = RtcEngine.create(getApplicationContext(), getString(R.string.private_app_id), mHandler);
            //   mRtcEngine.setChannelProfile(io.agora.rtc.Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
            //  mRtcEngine.enableVideo();
        } catch (Exception e) {
            e.printStackTrace();
        }

        initConfig();
    }

    private void initConfig() {
    }


//    public RtcEngine rtcEngine() {
//        return mRtcEngine;
//    }


//    public void registerEventHandler(EventHandler handler) {
//        mHandler.addHandler(handler);
//    }

//    public void removeEventHandler(EventHandler handler) {
//        mHandler.removeHandler(handler);
//    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        //   RtcEngine.destroy();
    }

    public void addAccessToken(final String accessToken) {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        restClient = new Retrofit.Builder()
                .baseUrl(GlobalData.SERVER_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .readTimeout(60, TimeUnit.SECONDS)
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .addInterceptor(chain -> {
                            Request.Builder ongoing = chain.request().newBuilder();
                            ongoing.header("Authorization", accessToken);
                            ongoing.header("Accept", "application/json");
                            ongoing.method(chain.request().method(), chain.request().body());
                            return chain.proceed(ongoing.build());
                        }).addInterceptor(loggingInterceptor)
                        .build())
                .build();
    }

    public ApiService getClient() {
        return restClient.create(ApiService.class);
    }

}
