package com.khmer9.lotto.app

import android.app.Application
import android.content.Context
import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor
import com.mazenrashed.printooth.Printooth
import com.khmer9.lotto.BuildConfig
import com.khmer9.lotto.remote.service.ApiService
import com.khmer9.lotto.remote.service.SocketService
import com.khmer9.lotto.util.*
import com.squareup.moshi.Moshi
import com.khmer9.lotto.config.Config
import com.squareup.picasso.Picasso
import okhttp3.OkHttpClient
import retrofit2.CallAdapter
import retrofit2.Converter
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory
import timber.log.Timber


class App : Application() {

    companion object {
        private lateinit var retrofit: Retrofit
        private lateinit var apiService: ApiService
        private lateinit var socketService: SocketService
        private lateinit var picasso: Picasso

        //private lateinit var loginViewModel: LoginViewModel
        //private lateinit var settingViewModel: SettingViewModel
        private lateinit var shareViewModel: ShareViewModel
        private lateinit var prefHelper: PrefHelper
        var guidelineLeft: Float = 0f
        var guidelineRight: Float = 100f

        fun injectApiService() = apiService
        //fun injectLoginViewModel() = loginViewModel
        fun injectShareViewModel() = shareViewModel
        //fun injectSettingViewModel() = settingViewModel
        fun injectPrefHelper() = prefHelper
        // Called on main activity create
        fun onAppStart(userID: String) {
           // Log.d("Socket", "onAppStart")
            shareViewModel.startListeningToSocketServer(userID)
        }

        // Called on main activity destroy
        fun onAppStop() {
            //Log.d("Socket", "onAppStop")
            shareViewModel.stopListeningToSocketServer()
        }

    }

    override fun onCreate() {
        super.onCreate()
        ModelPreferencesManager.with(this)
        Printooth.init(this)
        picasso = Picasso.Builder(this).loggingEnabled(true).build()
       // FirebaseApp.initializeApp(this)
        Picasso.setSingletonInstance(picasso)

        if (BuildConfig.DEBUG){
            Timber.uprootAll()
            Timber.plant(Timber.DebugTree())

        }
        val widthPx = screenRectPx.width()
        val heightPx = screenRectPx.height()
        val percentage =widthPx*100/heightPx
       // Log.d("DATA", ""+widthPx)
       // Log.d("DATA", ""+heightPx)
      //  Log.d("DATA", ""+percentage)

        when(percentage){
            in 30..54->{
                guidelineLeft = 0f
                guidelineRight = 1f

            }
            else -> {
                guidelineLeft = 0.05f
                guidelineRight = 0.95f
            }


        }

        prefHelper = PrefHelper(context =appContext(this))
        val builder = OkHttpClient.Builder()

        if(BuildConfig.DEBUG){
            builder
                .addInterceptor(ServiceInterceptor(prefHelper))
                .addInterceptor(OkHttpProfilerInterceptor())
                .addInterceptor(ErrorInterceptor(appContext(this), prefHelper))
               // .readTimeout(5, TimeUnit.MINUTES)
               // .writeTimeout(60,TimeUnit.SECONDS)

        }else{

            builder
                .addInterceptor(ServiceInterceptor(prefHelper))
                .addInterceptor(ErrorInterceptor(appContext(this), prefHelper))
               // .readTimeout(5, TimeUnit.MINUTES)
               // .writeTimeout(60,TimeUnit.SECONDS)

        }

        val client = builder.build()
        retrofit = Retrofit.Builder()
            .baseUrl(Config.BASE_URL)
            .client(client)
            .addCallAdapterFactory(callFactory())
            .addConverterFactory(converterFactory(moshi()))
            .build()

        apiService = retrofit.create(ApiService::class.java)
        socketService = SocketService()
        //loginViewModel = LoginViewModel(apiService, prefHelper)
        //settingViewModel = SettingViewModel(apiService, prefHelper)
        shareViewModel = ShareViewModel(apiService, prefHelper, socketService)



    }


    private fun callFactory(): CallAdapter.Factory = RxJava2CallAdapterFactory.create()



    private fun converterFactory(moshi: Moshi): Converter.Factory =
        MoshiConverterFactory.create(moshi).withNullSerialization()

    private fun moshi(): Moshi = Moshi.Builder().build()
    //fun unSafeOkHttpClient(): OkHttpClient = UnsafeOkHttpClient.getClient()
    private fun appContext(app: App): Context = app

    fun unSafeOkHttpClient(): OkHttpClient = UnsafeOkHttpClient.getClient()

}
