package com.ponzischeme89.memby.ui.calendar

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ponzischeme89.memby.ServiceLocator
import com.ponzischeme89.memby.data.model.BaseItem

/** Connects the weekly television guide to its month-caching view model. */
@Composable
fun CalendarScreen(
    navigationFocusRequester: FocusRequester,
    contentFocusRequester: FocusRequester,
    onItemFocused: (BaseItem) -> Unit,
    onItemSelected: (BaseItem) -> Unit,
    onExit: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val calendarViewModel: CalendarViewModel = viewModel(
        key = "tv-calendar",
        factory = remember { CalendarViewModelFactory(ServiceLocator.repository) },
    )
    val state by calendarViewModel.state.collectAsStateWithLifecycle()
    CalendarContent(
        state = state,
        navigationFocusRequester = navigationFocusRequester,
        contentFocusRequester = contentFocusRequester,
        onShowMonth = calendarViewModel::showMonth,
        onSelectDate = calendarViewModel::selectDate,
        onRetry = calendarViewModel::retry,
        onItemFocused = onItemFocused,
        onItemSelected = onItemSelected,
        onExit = onExit,
        modifier = modifier,
    )
}
