package com.hermes.app

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.compose.foundation.layout.width
import com.hermes.app.R
import com.hermes.app.core.designsystem.HermesPalette
import com.hermes.app.core.designsystem.HermesSecondaryButton
import com.hermes.app.core.designsystem.HermesTheme
import com.hermes.app.core.localization.HermesLocalizationStore
import com.hermes.app.feature.feed.FeedScreen
import com.hermes.app.feature.feed.FeedViewModel
import com.hermes.app.feature.session.SessionView
import com.hermes.app.feature.session.SessionViewModel
import com.hermes.app.feature.settings.SettingsView
import com.hermes.app.feature.round.RoundScreen
import com.hermes.app.feature.round.RoundViewModel
import androidx.compose.material3.ExperimentalMaterial3Api

@Composable
fun HermesApp(container: HermesAppContainer) {
    val feedViewModel: FeedViewModel = viewModel(factory = container.feedViewModelFactory())
    val roundViewModel: RoundViewModel = viewModel(factory = container.roundViewModelFactory())
    val sessionViewModel: SessionViewModel = viewModel(factory = container.sessionViewModelFactory())
    val localeCode by container.localizationStore.localeCode.collectAsStateWithLifecycle()
    val sessionState by sessionViewModel.uiState.collectAsStateWithLifecycle()
    val feedState by feedViewModel.uiState.collectAsStateWithLifecycle()
    val roundState by roundViewModel.uiState.collectAsStateWithLifecycle()

    LaunchedEffect(localeCode) {
        sessionViewModel.bootstrap(localeCode)
    }

    Scaffold(
        topBar = {
            HermesAppBar(
                title = container.localizationStore.string(localeCode, R.string.app_name),
                localeCode = localeCode,
                localizationStore = container.localizationStore,
                onLocaleSelected = container.localizationStore::setLocale,
            )
        },
        containerColor = HermesPalette.colors.background
    ) { padding ->
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding),
            contentPadding = PaddingValues(20.dp),
            verticalArrangement = Arrangement.spacedBy(20.dp)
        ) {
            item {
                FeedScreen(
                    uiState = feedState,
                    onWatchPreview = feedViewModel::onWatchPreview,
                    onRetry = sessionViewModel::retryBootstrap,
                )
            }
            item {
                RoundScreen(
                    uiState = roundState,
                    playerCoordinator = container.playerCoordinator,
                    onSelectOutcome = roundViewModel::onSelectOutcome,
                    onConfirmSelection = roundViewModel::onConfirmSelection,
                    onRevealComplete = roundViewModel::onRevealAcknowledged,
                    onNextRound = roundViewModel::onNextRound,
                    onRetry = sessionViewModel::retryBootstrap,
                )
            }
            item {
                SessionView(
                    uiState = sessionState,
                    localizationStore = container.localizationStore,
                    onRetry = sessionViewModel::retryBootstrap,
                )
            }
            item {
                SettingsView(
                    localizationStore = container.localizationStore,
                    localeCode = localeCode,
                )
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun HermesAppBar(
    title: String,
    localeCode: String,
    localizationStore: HermesLocalizationStore,
    onLocaleSelected: (String) -> Unit,
) {
    androidx.compose.material3.CenterAlignedTopAppBar(
        title = { Text(text = title) },
        actions = {
            HermesSecondaryButton(
                text = localizationStore.localeName("en", localeCode),
                selected = localeCode == "en",
                onClick = { onLocaleSelected("en") },
            )
            androidx.compose.foundation.layout.Spacer(modifier = Modifier.width(8.dp))
            HermesSecondaryButton(
                text = localizationStore.localeName("sv", localeCode),
                selected = localeCode == "sv",
                onClick = { onLocaleSelected("sv") },
            )
        },
        colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
            containerColor = HermesPalette.colors.background
        )
    )
}
