package hu.bbara.purefin.ui.screen.episode

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.compose.runtime.remember
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import hu.bbara.purefin.core.feature.content.episode.EpisodeScreenViewModel
import hu.bbara.purefin.core.image.ArtworkKind
import hu.bbara.purefin.core.image.ImageUrlBuilder
import hu.bbara.purefin.model.Episode
import hu.bbara.purefin.core.navigation.EpisodeDto
import hu.bbara.purefin.navigation.LocalNavigationManager
import hu.bbara.purefin.core.navigation.Route
import hu.bbara.purefin.ui.common.media.MediaDetailOverviewSection
import hu.bbara.purefin.ui.common.media.MediaDetailPlaybackSection
import hu.bbara.purefin.ui.common.media.MediaDetailSectionTitle
import hu.bbara.purefin.ui.common.media.TvMediaDetailBodyBox
import hu.bbara.purefin.ui.common.media.TvMediaDetailScaffold
import hu.bbara.purefin.ui.screen.episode.components.TvEpisodeHeroSection
import hu.bbara.purefin.ui.screen.waiting.PurefinWaitingScreen

@Composable
fun TvEpisodeScreen(
    episode: EpisodeDto,
    viewModel: EpisodeScreenViewModel = hiltViewModel(),
    modifier: Modifier = Modifier
) {
    val navigationManager = LocalNavigationManager.current

    LaunchedEffect(episode) {
        viewModel.selectEpisode(episode)
    }

    val episode = viewModel.episode.collectAsStateWithLifecycle()
    val selectedEpisode = episode.value

    if (selectedEpisode == null) {
        PurefinWaitingScreen()
        return
    }

    TvEpisodeScreenContent(
        episode = selectedEpisode,
        seriesTitle = selectedEpisode.seriesName,
        onPlay = remember(selectedEpisode.id, navigationManager) {
            {
                navigationManager.navigate(
                    Route.PlayerRoute(mediaId = selectedEpisode.id.toString())
                )
            }
        },
        modifier = modifier
    )
}

@Composable
internal fun TvEpisodeScreenContent(
    episode: Episode,
    seriesTitle: String?,
    onPlay: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val playFocusRequester = remember { FocusRequester() }

    LaunchedEffect(episode.id) {
        withFrameNanos { }
        playFocusRequester.requestFocus()
    }

    TvMediaDetailScaffold(
        resetScrollKey = episode.id,
        modifier = modifier
    ) {
        TvMediaDetailBodyBox(
            backgroundImageUrl = ImageUrlBuilder.finishImageUrl(
                episode.imageUrlPrefix,
                ArtworkKind.PRIMARY
            ),
            modifier = it
        ) {
            TvEpisodeHeroSection(
                episode = episode,
                seriesTitle = seriesTitle,
                onPlay = onPlay,
                playFocusRequester = playFocusRequester,
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(2.dp))
        }
        Column(modifier = it.fillMaxWidth()) {
            Spacer(modifier = Modifier.height(4.dp))
            MediaDetailOverviewSection(
                synopsis = episode.synopsis,
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(8.dp))
        }
        Column(modifier = it.fillMaxWidth()) {
            MediaDetailPlaybackSection(
                audioTrack = "ENG",
                subtitles = "ENG",
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(8.dp))
        }
        Column(modifier = it.fillMaxWidth()) {
            MediaDetailSectionTitle(text = "Cast")
            Spacer(modifier = Modifier.height(14.dp))
//          MediaCastRow(cast = episode.cast)
            Spacer(modifier = Modifier.height(24.dp))
        }
    }
}
