package de.oaa.xxx.app.ui.screens.aufgaben

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.oaa.xxx.app.data.model.AufgabenGruppeDisplay
import de.oaa.xxx.app.ui.viewmodel.AufgabenViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AufgabenScreen(
    viewModel: AufgabenViewModel,
    onNavigateBack: () -> Unit,
    onGruppeClick: (String) -> Unit
) {
    val userGruppen by viewModel.userGruppen.collectAsState()
    val systemGruppen by viewModel.systemGruppen.collectAsState()
    val subscriptions by viewModel.subscriptions.collectAsState()
    val isLoading by viewModel.isLoading.collectAsState()
    var selectedTab by remember { mutableIntStateOf(0) }
    val tabs = listOf("Eigene", "System", "Abonniert")

    LaunchedEffect(Unit) {
        viewModel.loadUserGruppen()
        viewModel.loadSystemGruppen()
        viewModel.loadSubscriptions()
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("Aufgabengruppen") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(Icons.Default.ArrowBack, contentDescription = "Zurück")
                    }
                }
            )
        }
    ) { padding ->
        Column(modifier = Modifier.fillMaxSize().padding(padding)) {
            TabRow(selectedTabIndex = selectedTab) {
                tabs.forEachIndexed { index, title ->
                    Tab(
                        selected = selectedTab == index,
                        onClick = { selectedTab = index },
                        text = { Text(title) }
                    )
                }
            }

            if (isLoading) {
                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                    CircularProgressIndicator()
                }
            } else {
                val currentList = when (selectedTab) {
                    0 -> userGruppen
                    1 -> systemGruppen
                    else -> subscriptions
                }
                LazyColumn(
                    modifier = Modifier.fillMaxSize(),
                    contentPadding = PaddingValues(16.dp),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    items(currentList) { gruppe ->
                        GruppeCard(
                            gruppe = gruppe,
                            showCopy = selectedTab == 1,
                            onClick = { gruppe.gruppenId?.let(onGruppeClick) },
                            onCopy = { gruppe.gruppenId?.let { viewModel.copyGruppe(it) {} } }
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun GruppeCard(
    gruppe: AufgabenGruppeDisplay,
    showCopy: Boolean,
    onClick: () -> Unit,
    onCopy: () -> Unit
) {
    Card(
        onClick = onClick,
        modifier = Modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface)
    ) {
        Row(
            modifier = Modifier.padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Column(modifier = Modifier.weight(1f)) {
                Text(
                    text = gruppe.name ?: "(Unbekannt)",
                    fontWeight = FontWeight.Bold,
                    fontSize = 16.sp
                )
                gruppe.beschreibung?.takeIf { it.isNotBlank() }?.let {
                    Text(
                        text = it,
                        fontSize = 13.sp,
                        color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f),
                        maxLines = 2
                    )
                }
                gruppe.von?.takeIf { it.isNotBlank() }?.let {
                    Text(text = "von: $it", fontSize = 12.sp, color = MaterialTheme.colorScheme.primary)
                }
            }
            if (showCopy) {
                IconButton(onClick = onCopy) {
                    Icon(Icons.Default.ContentCopy, contentDescription = "Kopieren", tint = MaterialTheme.colorScheme.primary)
                }
            }
        }
    }
}
