package hu.bbara.purefin.tv.home.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.outlined.Person
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
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

@Composable
fun TvHomeDrawerContent(
    title: String,
    subtitle: String,
    primaryNavItems: List<TvHomeNavItem>,
    secondaryNavItems: List<TvHomeNavItem>,
    user: TvHomeUser,
    onLibrarySelected: (TvHomeNavItem) -> Unit,
    onLogout: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier.fillMaxSize()) {
        TvHomeDrawerHeader(
            title = title,
            subtitle = subtitle
        )
        TvHomeDrawerNav(
            primaryItems = primaryNavItems,
            secondaryItems = secondaryNavItems,
            onLibrarySelected = onLibrarySelected
        )
        Spacer(modifier = Modifier.weight(1f))
        TvHomeDrawerFooter(user = user, onLogout = onLogout)
    }
}

@Composable
fun TvHomeDrawerHeader(
    title: String,
    subtitle: String,
    modifier: Modifier = Modifier
) {
    val scheme = MaterialTheme.colorScheme

    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(start = 24.dp, end = 16.dp, top = 24.dp, bottom = 20.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Row(
            modifier = Modifier
                .size(40.dp)
                .background(scheme.primary, RoundedCornerShape(12.dp)),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            Icon(
                imageVector = Icons.Filled.PlayArrow,
                contentDescription = "Play",
                tint = scheme.onPrimary
            )
        }
        Column(modifier = Modifier.padding(start = 12.dp)) {
            Text(
                text = title,
                color = scheme.onBackground,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold
            )
            Text(
                text = subtitle,
                color = scheme.onSurfaceVariant,
                fontSize = 12.sp
            )
        }
    }
    HorizontalDivider(color = scheme.onSurfaceVariant.copy(alpha = 0.2f))
}

@Composable
fun TvHomeDrawerNav(
    primaryItems: List<TvHomeNavItem>,
    secondaryItems: List<TvHomeNavItem>,
    onLibrarySelected: (TvHomeNavItem) -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(
        modifier = modifier
            .fillMaxWidth()
            .padding(vertical = 16.dp)
    ) {
        primaryItems.forEach { item ->
            TvHomeDrawerNavItem(item = item, onLibrarySelected = onLibrarySelected)
        }
        if (secondaryItems.isNotEmpty()) {
            HorizontalDivider(
                modifier = Modifier
                    .padding(horizontal = 20.dp, vertical = 12.dp),
                color = MaterialTheme.colorScheme.outlineVariant
            )
            secondaryItems.forEach { item ->
                TvHomeDrawerNavItem(item = item, onLibrarySelected = onLibrarySelected)
            }
        }
    }
}

@Composable
fun TvHomeDrawerNavItem(
    item: TvHomeNavItem,
    modifier: Modifier = Modifier,
    onLibrarySelected: (TvHomeNavItem) -> Unit
) {
    val scheme = MaterialTheme.colorScheme
    var isFocused by remember { mutableStateOf(false) }
    val background = when {
        isFocused -> scheme.primary.copy(alpha = 0.28f)
        item.selected -> scheme.primary.copy(alpha = 0.12f)
        else -> Color.Transparent
    }
    val tint = if (item.selected || isFocused) scheme.primary else scheme.onSurfaceVariant
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 4.dp)
            .then(if (isFocused) Modifier.border(2.dp, scheme.primary, RoundedCornerShape(12.dp)) else Modifier)
            .background(background, RoundedCornerShape(12.dp))
            .onFocusChanged { isFocused = it.isFocused }
            .clickable { onLibrarySelected(item) }
            .padding(horizontal = 16.dp, vertical = 12.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = item.icon,
            contentDescription = item.label,
            tint = tint
        )
        Text(
            text = item.label,
            color = if (item.selected || isFocused) scheme.primary else scheme.onBackground,
            fontSize = 15.sp,
            fontWeight = FontWeight.Medium,
            modifier = Modifier.padding(start = 12.dp)
        )
    }
}

@Composable
fun TvHomeDrawerFooter(
    user: TvHomeUser,
    onLogout: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val scheme = MaterialTheme.colorScheme

    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(16.dp)
            .background(scheme.surfaceVariant, RoundedCornerShape(12.dp))
            .padding(12.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        TvHomeAvatar(
            size = 32.dp,
            borderWidth = 1.dp,
            borderColor = scheme.outlineVariant,
            backgroundColor = scheme.primaryContainer,
            icon = Icons.Outlined.Person,
            iconTint = scheme.onBackground
        )
        Column(modifier = Modifier.padding(start = 12.dp)
            .clickable { onLogout() }) {
            Text(
                text = user.name,
                color = scheme.onBackground,
                fontSize = 14.sp,
                fontWeight = FontWeight.SemiBold,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
            Text(
                text = user.plan,
                color = scheme.onSurfaceVariant,
                fontSize = 11.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }
}
