package com.example.myapplication.database

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.createSavedStateHandle
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.example.myapplication.BikeApplication
import com.example.myapplication.database.entities.composeui.BikeListViewModel
import com.example.myapplication.database.entities.composeui.BikeViewModel
import com.example.myapplication.database.entities.composeui.edit.BikeEditViewModel
import com.example.myapplication.database.entities.composeui.edit.ItemEditViewModel
import com.example.myapplication.database.room.viewmodel.cart.CartViewModel
import com.example.myapplication.database.room.viewmodel.user.CurrentUserViewModel
import com.example.myapplication.database.room.viewmodel.user.EntryUserViewModel
import com.example.myapplication.database.room.viewmodel.item.ItemListViewModel
import com.example.myapplication.database.room.viewmodel.user.RegisterUserViewModel
import com.example.myapplication.database.room.viewmodel.rent.RentListViewModel
import com.example.myapplication.database.room.viewmodel.rent.RentViewModel
import com.example.myapplication.database.room.viewmodel.rent.ReportViewModel

object AppViewModelProvider {
    val Factory = viewModelFactory {
        initializer {
            CurrentUserViewModel(bikeApplication().container.userRestRepository)
        }
        initializer {
            EntryUserViewModel(bikeApplication().container.userRestRepository)
        }
        initializer {
            RegisterUserViewModel(bikeApplication().container.userRestRepository)
        }
        initializer {
            BikeListViewModel(bikeApplication().container.bikeRestRepository)
        }
        initializer {
            ReportViewModel(
                bikeApplication().container.itemRestRepository,
            )
        }
        initializer {
            BikeEditViewModel(
                this.createSavedStateHandle(),
                bikeApplication().container.bikeRestRepository
            )
        }
        initializer {
            BikeViewModel(
                this.createSavedStateHandle(),
                bikeApplication().container.bikeRestRepository,
            )
        }
        initializer {
            ItemListViewModel(
                bikeApplication().container.itemRestRepository,
                bikeApplication().container.userItemRestRepository,
            )
        }
        initializer {
            ItemEditViewModel(
                this.createSavedStateHandle(),
                bikeApplication().container.itemRestRepository,
            )
        }
        initializer {
            CartViewModel(
                bikeApplication().container.userItemRestRepository,
                bikeApplication().container.rentRestRepository,
                bikeApplication().container.rentItemRestRepository,
                bikeApplication().container.userRestRepository,
            )
        }
        initializer {
            RentListViewModel(
                bikeApplication().container.rentRestRepository,
            )
        }
        initializer {
            RentViewModel(
                this.createSavedStateHandle(),
                bikeApplication().container.rentRestRepository,
            )
        }
    }
}

fun CreationExtras.bikeApplication(): BikeApplication =
    (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as BikeApplication)