package com.pixelized.headache.ui.navigation.destination

import android.icu.util.Calendar
import androidx.compose.runtime.Stable
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.navigation3.runtime.EntryProviderScope
import com.pixelized.headache.repository.event.Event
import com.pixelized.headache.ui.navigation.main.MainDestination
import com.pixelized.headache.ui.navigation.main.MainNavigator
import com.pixelized.headache.ui.page.event.list.EventPage
import com.pixelized.headache.ui.page.event.list.EventViewModel
import com.pixelized.headache.utils.extention.event
import java.util.Date

data class EventDestination(
    val date: Event.Date?,
    val misspelledFilter: Boolean,
    val invalidFilter: Boolean,
) : MainDestination

fun EntryProviderScope<Any>.eventDestinationEntry() {
    entry<EventDestination> { key ->
        val viewModel = hiltViewModel<EventViewModel, EventViewModel.Factory>(
            creationCallback = { factory ->
                factory.create(key)
            }
        )
        EventPage(
            viewModel = viewModel,
        )
    }
}

fun MainNavigator.navigateToEventPage(
    misspelledFilter: Boolean = false,
    invalidFilter: Boolean = false,
    date: Date? = null,
) {
    val route = EventDestination(
        date = date?.let { Calendar.getInstance().apply { time = it }.event.copy(day = 1) },
        misspelledFilter = misspelledFilter,
        invalidFilter = invalidFilter,
    )
    goTo(route)
}