package de.tsschulz.tt_tagebuch.app.ui

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import de.tsschulz.tt_tagebuch.app.viewmodel.ParticipantScreenModel
import de.tsschulz.tt_tagebuch.app.viewmodel.ParticipantState

class ParticipantScreen(private val dateId: Int, private val dateStr: String) : Screen {
    @Composable
    override fun Content() {
        val navigator = LocalNavigator.current
        val viewModel = getScreenModel<ParticipantScreenModel>()
        val state by viewModel.state.collectAsState()

        LaunchedEffect(dateId) {
            viewModel.loadParticipants(dateId)
        }

        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text("Teilnehmer - $dateStr") },
                    navigationIcon = {
                        IconButton(onClick = { navigator?.pop() }) {
                            Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
                        }
                    }
                )
            }
        ) { padding ->
            Column(modifier = Modifier.padding(padding).fillMaxSize()) {
                when (state) {
                    is ParticipantState.Loading -> {
                        CircularProgressIndicator(modifier = Modifier.padding(16.dp))
                    }
                    is ParticipantState.Success -> {
                        val participants = (state as ParticipantState.Success).participants
                        LazyColumn {
                            items(participants) { participant ->
                                ParticipantItem(participant) { newStatus ->
                                    viewModel.updateStatus(dateId, participant.memberId, newStatus)
                                }
                            }
                        }
                    }
                    is ParticipantState.Error -> {
                        Text(
                            text = (state as ParticipantState.Error).message,
                            color = MaterialTheme.colors.error,
                            modifier = Modifier.padding(16.dp)
                        )
                    }
                }
            }
        }
    }

    @Composable
    fun ParticipantItem(
        participant: de.tsschulz.tt_tagebuch.shared.models.Participant,
        onStatusChange: (String) -> Unit
    ) {
        val member = participant.member
        val name = if (member != null) "${member.firstName} ${member.lastName}" else "Unbekannt"
        
        Card(modifier = Modifier.fillMaxWidth().padding(8.dp), elevation = 2.dp) {
            Column(modifier = Modifier.padding(16.dp)) {
                Text(name, style = MaterialTheme.typography.h6)
                Spacer(Modifier.height(8.dp))
                
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                    StatusToggle("Anwesend", participant.attendanceStatus == "present", Color(0xFF4CAF50)) {
                        onStatusChange("present")
                    }
                    StatusToggle("Entschuldigt", participant.attendanceStatus == "excused", Color(0xFF2196F3)) {
                        onStatusChange("excused")
                    }
                    StatusToggle("Abgesagt", participant.attendanceStatus == "cancelled", Color.Red) {
                        onStatusChange("cancelled")
                    }
                }
            }
        }
    }

    @Composable
    fun StatusToggle(label: String, isSelected: Boolean, color: Color, onClick: () -> Unit) {
        val backgroundColor = if (isSelected) color else color.copy(alpha = 0.1f)
        val contentColor = if (isSelected) Color.White else color
        
        Button(
            onClick = onClick,
            colors = ButtonDefaults.buttonColors(
                backgroundColor = backgroundColor,
                contentColor = contentColor
            ),
            elevation = ButtonDefaults.elevation(0.dp),
            shape = MaterialTheme.shapes.small,
            modifier = Modifier.height(32.dp).padding(horizontal = 4.dp),
            contentPadding = PaddingValues(horizontal = 8.dp)
        ) {
            Text(label, style = MaterialTheme.typography.caption)
        }
    }
}
