package de.tsschulz.tt_tagebuch.app.ui

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Phone
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.navigator.LocalNavigator
import de.tsschulz.tt_tagebuch.shared.models.Member

class MemberDetailScreen(private val member: Member) : Screen {
    @Composable
    override fun Content() {
        val navigator = LocalNavigator.current
        
        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text("${member.firstName} ${member.lastName}") },
                    navigationIcon = {
                        IconButton(onClick = { navigator?.pop() }) {
                            Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
                        }
                    },
                    actions = {
                        IconButton(onClick = {
                            navigator?.push(MemberEditScreen(member.clubId, member))
                        }) {
                            Icon(Icons.Default.Edit, contentDescription = "Edit")
                        }
                    }
                )
            }
        ) { padding ->
            Column(
                modifier = Modifier
                    .padding(padding)
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
                    .padding(16.dp)
            ) {
                // Header with Status Badges
                Row(verticalAlignment = Alignment.CenterVertically) {
                    if (member.active) {
                        StatusBadge("Aktiv", Color(0xFF4CAF50))
                    } else {
                        StatusBadge("Inaktiv", Color.Red)
                    }
                    if (member.testMembership) {
                        Spacer(Modifier.width(8.dp))
                        StatusBadge("Test", Color(0xFFFFA500))
                    }
                }
                
                Spacer(Modifier.height(16.dp))
                
                // TTR Info
                Card(elevation = 2.dp, modifier = Modifier.fillMaxWidth()) {
                    Row(modifier = Modifier.padding(16.dp), horizontalArrangement = Arrangement.SpaceBetween) {
                        Column {
                            Text("TTR", style = MaterialTheme.typography.caption)
                            Text("${member.ttr ?: "-"}", style = MaterialTheme.typography.h5)
                        }
                        Column {
                            Text("QTTR", style = MaterialTheme.typography.caption)
                            Text("${member.qttr ?: "-"}", style = MaterialTheme.typography.h5)
                        }
                        Column {
                            Text("Alter", style = MaterialTheme.typography.caption)
                            Text(member.birthDate ?: "-", style = MaterialTheme.typography.h5)
                        }
                    }
                }
                
                Spacer(Modifier.height(24.dp))
                
                // Contact Details
                Text("Kontaktdaten", style = MaterialTheme.typography.h6)
                member.contacts.forEach { contact ->
                    ContactRow(contact)
                }
                
                if (member.contacts.isEmpty()) {
                    Text("Keine Kontaktdaten hinterlegt", style = MaterialTheme.typography.body2, color = Color.Gray)
                }
                
                Spacer(Modifier.height(24.dp))
                
                // Attendance Info
                Text("Statistik", style = MaterialTheme.typography.h6)
                Text("Trainingsteilnahmen: ${member.trainingParticipations}", style = MaterialTheme.typography.body1)
                Text("Zuletzt im Training: ${member.lastTraining ?: "Nie"}", style = MaterialTheme.typography.body1)
            }
        }
    }

    @Composable
    fun StatusBadge(text: String, color: Color) {
        Surface(
            color = color.copy(alpha = 0.1f),
            shape = MaterialTheme.shapes.small,
            border = androidx.compose.foundation.BorderStroke(1.dp, color)
        ) {
            Text(
                text = text,
                modifier = Modifier.padding(horizontal = 8.dp, vertical = 2.dp),
                style = MaterialTheme.typography.caption,
                color = color
            )
        }
    }

    @Composable
    fun ContactRow(contact: de.tsschulz.tt_tagebuch.shared.models.MemberContact) {
        Row(
            modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                if (contact.type == "phone") Icons.Default.Phone else Icons.Default.Email,
                contentDescription = null,
                tint = MaterialTheme.colors.primary,
                modifier = Modifier.size(20.dp)
            )
            Spacer(Modifier.width(16.dp))
            Column {
                Text(contact.value, style = MaterialTheme.typography.body1)
                if (contact.isParent) {
                    Text("Elternteil${if (contact.parentName != null) ": ${contact.parentName}" else ""}", 
                        style = MaterialTheme.typography.caption, color = Color.Gray)
                }
            }
        }
    }
}
