package de.tsschulz.timeclock.ui.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.tsschulz.timeclock.ui.model.AppRoute
import de.tsschulz.timeclock.ui.model.MenuSection
import de.tsschulz.timeclock.ui.model.StatusAction
import de.tsschulz.timeclock.ui.model.StatusRow
import de.tsschulz.timeclock.ui.theme.TcColors
import de.tsschulz.timeclock.ui.theme.TcRadius
import de.tsschulz.timeclock.ui.theme.TcSpacing

@Composable
fun TcScaffold(
    title: String,
    userName: String,
    sections: List<MenuSection>,
    selectedRoute: AppRoute,
    isTablet: Boolean,
    statusRows: List<StatusRow>,
    primaryStatusAction: StatusAction?,
    secondaryStatusAction: StatusAction?,
    onRouteSelected: (AppRoute) -> Unit,
    onLogout: () -> Unit,
    onStatusAction: (StatusAction) -> Unit = {},
    content: @Composable ColumnScope.() -> Unit,
) {
    if (isTablet) {
        Row(modifier = Modifier.fillMaxSize().background(TcColors.Background)) {
            TcSectionMenu(
                sections = sections,
                selectedRoute = selectedRoute,
                onRouteSelected = onRouteSelected,
            )
            Column(modifier = Modifier.fillMaxSize()) {
                TcTopBar(title = title, userName = userName, compact = false, onLogout = onLogout)
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = TcSpacing.WebContainer, vertical = TcSpacing.Lg),
                    horizontalArrangement = Arrangement.End,
                ) {
                    TcStatusBox(
                        rows = statusRows,
                        primaryAction = primaryStatusAction,
                        secondaryAction = secondaryStatusAction,
                        modifier = Modifier.fillMaxWidth(0.48f),
                        onAction = onStatusAction,
                    )
                }
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .verticalScroll(rememberScrollState())
                        .padding(horizontal = TcSpacing.WebContainer)
                        .padding(bottom = TcSpacing.Xxl),
                    verticalArrangement = Arrangement.spacedBy(TcSpacing.Xl),
                    content = content,
                )
            }
        }
    } else {
        Column(modifier = Modifier.fillMaxSize().background(TcColors.Background)) {
            TcTopBar(title = title, userName = userName, compact = true, onLogout = onLogout)
            Column(
                modifier = Modifier
                    .weight(1f)
                    .verticalScroll(rememberScrollState())
                    .padding(horizontal = TcSpacing.Lg, vertical = TcSpacing.Lg),
                verticalArrangement = Arrangement.spacedBy(TcSpacing.Lg),
            ) {
                TcStatusBox(
                    rows = statusRows,
                    primaryAction = primaryStatusAction,
                    secondaryAction = secondaryStatusAction,
                    modifier = Modifier.fillMaxWidth(),
                    onAction = onStatusAction,
                )
                content()
            }
            TcBottomNavigation(
                sections = sections,
                selectedRoute = selectedRoute,
                onRouteSelected = onRouteSelected,
            )
        }
    }
}

@Composable
fun TcTopBar(
    title: String,
    userName: String,
    modifier: Modifier = Modifier,
    compact: Boolean = false,
    onLogout: () -> Unit = {},
) {
    val barModifier = modifier
        .fillMaxWidth()
        .shadow(2.dp)
        .background(TcColors.Navbar)
        .border(BorderStroke(1.dp, TcColors.BorderSoft))
        .statusBarsPadding()
        .padding(horizontal = TcSpacing.Xl, vertical = TcSpacing.Md)

    if (compact) {
        Column(
            modifier = barModifier,
            verticalArrangement = Arrangement.spacedBy(TcSpacing.Sm),
        ) {
            Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(TcSpacing.Lg)) {
                TcBrandTitle()
                Box(modifier = Modifier.weight(1f))
                TcButton(text = "Abmelden", onClick = onLogout)
            }
            Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(TcSpacing.Md)) {
                PageTitle(title = title)
                Text(
                    text = userName,
                    color = TcColors.TextMuted,
                    fontSize = 14.sp,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.weight(1f),
                )
            }
        }
        return
    }

    Row(
        modifier = barModifier,
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(TcSpacing.Lg),
    ) {
        TcBrandTitle()
        PageTitle(title = title)
        Box(modifier = Modifier.weight(1f))
        Text(text = userName, color = TcColors.TextMuted, fontSize = 14.sp)
        TcButton(text = "Abmelden", onClick = onLogout)
    }
}

@Composable
private fun PageTitle(title: String) {
    Box(
        modifier = Modifier
            .background(TcColors.Background, RoundedCornerShape(TcRadius.Small))
            .border(BorderStroke(1.dp, TcColors.Border), RoundedCornerShape(TcRadius.Small))
            .padding(horizontal = 15.dp, vertical = 4.dp),
    ) {
        Text(
            text = title,
            color = Color2c3e50,
            fontSize = 18.sp,
            fontWeight = FontWeight.SemiBold,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )
    }
}

private val Color2c3e50 = androidx.compose.ui.graphics.Color(0xFF2C3E50)
