package de.softwareapp_hb.privateqrscanner.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.History
import androidx.compose.material.icons.filled.QrCodeScanner
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import de.softwareapp_hb.privateqrscanner.AppContainer
import de.softwareapp_hb.privateqrscanner.R
import de.softwareapp_hb.privateqrscanner.i18n.AppLocaleOption
import de.softwareapp_hb.privateqrscanner.ui.screens.HistoryScreen
import de.softwareapp_hb.privateqrscanner.ui.screens.ScannerScreen
import de.softwareapp_hb.privateqrscanner.ui.screens.SettingsScreen
import de.softwareapp_hb.privateqrscanner.ui.theme.PrivateQrColors

private enum class RootTab { Scanner, History, Settings }

@Composable
fun CleanScannerAppRoot(
    container: AppContainer,
    localeOptions: List<AppLocaleOption>
) {
    val appViewModel: AppViewModel = viewModel(factory = AppViewModel.Factory(container))
    val scannerViewModel: ScannerViewModel = viewModel(factory = ScannerViewModel.Factory(container))
    val appState by appViewModel.uiState.collectAsStateWithLifecycle()
    val scannerState by scannerViewModel.uiState.collectAsStateWithLifecycle()

    var activeTab by remember { mutableStateOf(RootTab.Scanner) }

    Scaffold(
        containerColor = PrivateQrColors.AppBackground,
        bottomBar = {
            val navColors = NavigationBarItemDefaults.colors(
                selectedIconColor = PrivateQrColors.Teal700,
                selectedTextColor = PrivateQrColors.Teal700,
                indicatorColor = PrivateQrColors.Mint,
                unselectedIconColor = PrivateQrColors.TextSecondary,
                unselectedTextColor = PrivateQrColors.TextSecondary
            )
            NavigationBar(containerColor = PrivateQrColors.Surface) {
                NavigationBarItem(
                    selected = activeTab == RootTab.Scanner,
                    onClick = {
                        activeTab = RootTab.Scanner
                        scannerViewModel.resumeScanning()
                    },
                    label = { Text(stringResource(R.string.scan)) },
                    icon = { Icon(Icons.Default.QrCodeScanner, contentDescription = null) },
                    colors = navColors
                )
                NavigationBarItem(
                    selected = activeTab == RootTab.History,
                    onClick = {
                        activeTab = RootTab.History
                    },
                    label = { Text(stringResource(R.string.history)) },
                    icon = { Icon(Icons.Default.History, contentDescription = null) },
                    colors = navColors
                )
                NavigationBarItem(
                    selected = activeTab == RootTab.Settings,
                    onClick = {
                        activeTab = RootTab.Settings
                    },
                    label = { Text(stringResource(R.string.settings)) },
                    icon = { Icon(Icons.Default.Settings, contentDescription = null) },
                    colors = navColors
                )
            }
        }
    ) { padding ->
        Box(modifier = Modifier.padding(padding)) {
            when (activeTab) {
                RootTab.Scanner -> ScannerScreen(
                    analysisEnabled = scannerState.analysisEnabled,
                    lastResult = scannerState.lastResult,
                    batchMode = scannerState.batchMode,
                    batchResults = scannerState.batchResults,
                    eventTicketWhitelistCount = scannerState.eventTicketWhitelistCount,
                    duplicateFeedbackNonce = scannerState.duplicateFeedbackNonce,
                    scanFeedbackNonce = scannerState.scanFeedbackNonce,
                    warningsEnabled = appState.warningsEnabled,
                    scanFeedbackEnabled = appState.scanFeedbackEnabled,
                    useCaseView = appState.useCaseView,
                    onScan = scannerViewModel::onScan,
                    onEvaluateEventTicketScan = scannerViewModel::evaluateEventTicketScan,
                    onAuditDuplicateTicket = scannerViewModel::auditDuplicateTicketScan,
                    onAuditUnregisteredTicket = scannerViewModel::auditUnregisteredTicketScan,
                    onReplaceEventTicketWhitelist = scannerViewModel::replaceEventTicketWhitelist,
                    onScanAgain = scannerViewModel::resumeScanning,
                    onBatchModeChange = scannerViewModel::setBatchMode,
                    onClearBatchResults = scannerViewModel::clearBatchResults,
                    onOpenHistory = { activeTab = RootTab.History }
                )

                RootTab.History -> HistoryScreen(
                    query = appState.searchQuery,
                    history = appState.history,
                    useCaseView = appState.useCaseView,
                    historyEnabled = appState.historyEnabled,
                    warningsEnabled = appState.warningsEnabled,
                    scanFeedbackEnabled = appState.scanFeedbackEnabled,
                    onQueryChange = appViewModel::setQuery,
                    onDelete = appViewModel::deleteHistoryItem,
                    onClearAll = appViewModel::clearHistory,
                    onHistoryToggle = appViewModel::setHistoryEnabled,
                    onWarningsToggle = appViewModel::setWarningsEnabled,
                    onScanFeedbackToggle = appViewModel::setScanFeedbackEnabled
                )

                RootTab.Settings -> SettingsScreen(
                    historyEnabled = appState.historyEnabled,
                    warningsEnabled = appState.warningsEnabled,
                    scanFeedbackEnabled = appState.scanFeedbackEnabled,
                    selectedUseCaseView = appState.useCaseView,
                    appLocaleOptions = localeOptions,
                    selectedAppLanguageTag = appState.appLanguageTag,
                    onHistoryToggle = appViewModel::setHistoryEnabled,
                    onWarningsToggle = appViewModel::setWarningsEnabled,
                    onScanFeedbackToggle = appViewModel::setScanFeedbackEnabled,
                    onUseCaseViewSelected = appViewModel::setUseCaseView,
                    onAppLanguageSelected = appViewModel::setAppLanguageTag
                )
            }
        }
    }
}
