package com.remax.visualnovel.app.base.app

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import com.remax.visualnovel.app.viewmodel.AppGlobalViewModel
import com.remax.visualnovel.app.viewmodel.AppIMViewModel
import com.remax.visualnovel.repository.api.MessageRepository
import com.remax.visualnovel.repository.api.UserRepository
import javax.inject.Inject

/**
 * 用于创建[AppIMViewModel, AppGlobalViewModel]等实例
 */
class AppViewModelFactory @Inject constructor(
    private val application: Application,
    private val userRepository: UserRepository,
    private val messageRepository: MessageRepository
) : ViewModelProvider.Factory {

    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
        return when (modelClass) {
            AppIMViewModel::class.java -> AppIMViewModel(application)
            AppGlobalViewModel::class.java -> AppGlobalViewModel(application, userRepository, messageRepository)
            else -> throw IllegalArgumentException("Unknown class $modelClass")
        } as T
    }
}