package de.ypchat.android.core

import android.content.Context
import de.ypchat.android.data.api.RestApi
import de.ypchat.android.data.api.SocketClient
import de.ypchat.android.data.repository.ChatRepository
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class AppContainer(context: Context) {
    val cookieJar = SessionCookieJar(context)
    val profileStore = ProfileStore(context)

    val okHttpClient: OkHttpClient = OkHttpClient.Builder()
        .cookieJar(cookieJar)
        .connectTimeout(15, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .build()

    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl(AppConfig.baseUrl + "/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val restApi: RestApi = retrofit.create(RestApi::class.java)
    val socketClient = SocketClient(AppConfig.baseUrl, okHttpClient)
    val chatRepository = ChatRepository(restApi, socketClient, cookieJar, profileStore)
}
