package com.zyzf.coffeepreorder.ui

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.zyzf.coffeepreorder.CoffeeApplication
import com.zyzf.coffeepreorder.ui.cart.CartViewModel
import com.zyzf.coffeepreorder.ui.coffee.CoffeeListViewModel
import com.zyzf.coffeepreorder.ui.login.LoginViewModel
import com.zyzf.coffeepreorder.ui.profile.ProfileViewModel
import com.zyzf.coffeepreorder.ui.register.RegisterViewModel

object AppViewModelProvider {
    val Factory = viewModelFactory {
        initializer {
            CoffeeListViewModel(coffeeApplication().container.coffeeRestRepository, coffeeApplication().container.cartRepository)
        }
        initializer {
            CartViewModel(coffeeApplication().container.cartRepository)
        }
        initializer {
            LoginViewModel(coffeeApplication().container.userRestRepository)
        }
        initializer {
            RegisterViewModel(coffeeApplication().container.userRestRepository)
        }
        initializer {
            ProfileViewModel(coffeeApplication().container.userRestRepository)
        }
    }
}

fun CreationExtras.coffeeApplication(): CoffeeApplication =
    (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as CoffeeApplication)