package org.fnives.tiktokdownloader.di

import android.content.Context
import androidx.lifecycle.ViewModelProvider
import org.fnives.tiktokdownloader.di.module.AndroidFileManagementModule
import org.fnives.tiktokdownloader.di.module.LocalSourceModule
import org.fnives.tiktokdownloader.di.module.NetworkModule
import org.fnives.tiktokdownloader.di.module.PermissionModule
import org.fnives.tiktokdownloader.di.module.UseCaseModule
import org.fnives.tiktokdownloader.di.module.ViewModelModule
import org.fnives.tiktokdownloader.ui.service.QueueServiceViewModel
import java.util.concurrent.TimeUnit

@Suppress("ObjectPropertyName", "MemberVisibilityCanBePrivate")
object ServiceLocator {

    private val DEFAULT_DELAY_BEFORE_REQUEST = TimeUnit.SECONDS.toMillis(4)
    private var _viewModelModule: ViewModelModule? = null
    private val viewModelModule: ViewModelModule
        get() = _viewModelModule ?: throw IllegalStateException("$this.start has not been called!")

    private var _permissionModule: PermissionModule? = null
    val permissionModule: PermissionModule
        get() = _permissionModule ?: throw IllegalStateException("$this.start has not been called!")

    private var _useCaseModule: UseCaseModule? = null
    val useCaseModule: UseCaseModule
        get() = _useCaseModule ?: throw IllegalStateException("$this.start has not been called!")

    fun viewModelFactory(): ViewModelProvider.Factory =
        ViewModelFactory(viewModelModule)

    val queueServiceViewModel: QueueServiceViewModel
        get() = viewModelModule.queueServiceViewModel

    fun start(context: Context) {
        val androidFileManagementModule = AndroidFileManagementModule(context)
        val localSourceModule =
            LocalSourceModule(androidFileManagementModule = androidFileManagementModule)
        val networkModule = NetworkModule(delayBeforeRequest = DEFAULT_DELAY_BEFORE_REQUEST)
        val useCaseModule = UseCaseModule(
            localSourceModule = localSourceModule,
            networkModule = networkModule
        )
        _useCaseModule = useCaseModule
        _permissionModule = PermissionModule()
        _viewModelModule = ViewModelModule(useCaseModule)
    }
}