package hu.bbara.purefin.ui.screen.home

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import hu.bbara.purefin.core.model.LibraryUiModel
import hu.bbara.purefin.core.model.MediaUiModel
import hu.bbara.purefin.ui.screen.AppBottomBar
import hu.bbara.purefin.ui.screen.home.components.HomeContent
import hu.bbara.purefin.ui.screen.home.components.HomeTopBar
import hu.bbara.purefin.ui.screen.home.components.rememberDefaultTopBarScrollBehavior
import java.util.UUID

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(
    libraries: List<LibraryUiModel>,
    libraryContent: Map<UUID, List<MediaUiModel>>,
    suggestions: List<MediaUiModel>,
    continueWatching: List<MediaUiModel>,
    nextUp: List<MediaUiModel>,
    isRefreshing: Boolean,
    onRefresh: () -> Unit,
    onMediaSelected: (MediaUiModel) -> Unit,
    onLibrarySelected: (LibraryUiModel) -> Unit,
    onMarkWatched: (MediaUiModel, Boolean) -> Unit,
    onProfileClick: () -> Unit,
    onCheckForUpdates: () -> Unit,
    isCheckingForUpdates: Boolean,
    onSettingsClick: () -> Unit,
    onLogoutClick: () -> Unit,
    onSearchClick: () -> Unit,
    snackbarHostState: SnackbarHostState,
    selectedTab: Int,
    isOnline: Boolean,
    onTabSelected: (Int) -> Unit,
    modifier: Modifier = Modifier
) {
    val topBarScrollBehavior = rememberDefaultTopBarScrollBehavior()

    Scaffold(
        modifier = modifier
            .fillMaxSize()
            .nestedScroll(topBarScrollBehavior.nestedScrollConnection),
        containerColor = MaterialTheme.colorScheme.background,
        contentColor = MaterialTheme.colorScheme.onBackground,
        topBar = {
            HomeTopBar(
                onSearchClick = onSearchClick,
                onProfileClick = onProfileClick,
                onCheckForUpdates = onCheckForUpdates,
                isCheckingForUpdates = isCheckingForUpdates,
                onSettingsClick = onSettingsClick,
                onLogoutClick = onLogoutClick,
                scrollBehavior = topBarScrollBehavior
            )
        },
        snackbarHost = { SnackbarHost(snackbarHostState) },
        bottomBar = {
            AppBottomBar(
                selectedTab = selectedTab,
                isOnline = isOnline,
                onTabSelected = onTabSelected
            )
        }
    ) { innerPadding ->
        HomeContent(
            libraries = libraries,
            libraryContent = libraryContent,
            suggestions = suggestions,
            continueWatching = continueWatching,
            nextUp = nextUp,
            isRefreshing = isRefreshing,
            onRefresh = onRefresh,
            onMediaSelected = onMediaSelected,
            onLibrarySelected = onLibrarySelected,
            onBrowseLibrariesClick = { onTabSelected(1) },
            onMarkAsWatched = onMarkWatched,
            modifier = Modifier.padding(innerPadding)
        )
    }
}
