package com.rdiot.yx485.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import com.rdiot.yx485.ui.bind.model.BindViewModel
import com.rdiot.yx485.ui.ctrl.model.RoomCtrlViewModel
import com.rdiot.yx485.ui.family.model.FamilyDataViewModel
import com.rdiot.yx485.ui.login.model.LoginViewModel

/**
 *
 * @author mR2hao
 * @date 2022/11/17
 */
class LoginViewModelFactory : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
        if (modelClass.isAssignableFrom(LoginViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return LoginViewModel() as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

class BindModelFactory : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
        if (modelClass.isAssignableFrom(BindViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return BindViewModel() as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

class FamilyDataModelFactory : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
        if (modelClass.isAssignableFrom(FamilyDataViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return FamilyDataViewModel() as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

class RoomCtrlViewModelFactory : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
        if (modelClass.isAssignableFrom(RoomCtrlViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return RoomCtrlViewModel() as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}