package com.example.pmulabs.viewModels

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.example.pmulabs.NewsPortalApplication

object AppViewModelProvider {
    val Factory = viewModelFactory {
        initializer {
            CurrentUserViewModel(newsPortalApplication().container.userRepository)
        }
        initializer {
            TagItemViewModel(newsPortalApplication().container.tagRepository)
        }
        initializer {
            CommentItemViewModel(newsPortalApplication().container.userRepository,newsPortalApplication().container.commentRepository)
        }
        initializer {
            ArticleItemViewModel(newsPortalApplication().container.tagRepository,newsPortalApplication().container.commentRepository,newsPortalApplication().container.articleRepository)
        }
        initializer {
            EntryScreenViewModel(newsPortalApplication().container.userRepository)
        }
        initializer {
            RegisterScreenViewModel(newsPortalApplication().container.userRepository)
        }
        initializer {
            ArticleScreenViewModel(newsPortalApplication().container.articleRepository)
        }
        initializer {
            ArticlePageScreenViewModel(newsPortalApplication().container.tagRepository,newsPortalApplication().container.commentRepository,newsPortalApplication().container.articleRepository,newsPortalApplication().container.userRepository)
        }
    }
}

fun CreationExtras.newsPortalApplication(): NewsPortalApplication =
    (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as NewsPortalApplication)