package de.tsschulz.tt_tagebuch.app.ui

import androidx.compose.foundation.clickable
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.filled.Add
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel
import de.tsschulz.tt_tagebuch.app.viewmodel.DiaryScreenModel
import de.tsschulz.tt_tagebuch.app.viewmodel.DiaryState

class DiaryScreen(private val clubId: Int) : Screen {
    @Composable
    override fun Content() {
        val navigator = LocalNavigator.current
        val viewModel = getScreenModel<DiaryScreenModel>()
        val state by viewModel.state.collectAsState()

        LaunchedEffect(clubId) {
            viewModel.loadDiaryDates(clubId)
        }

        Scaffold(
            topBar = {
                TopAppBar(title = { Text("Trainingstagebuch") })
            },
            floatingActionButton = {
                FloatingActionButton(onClick = { /* Add new entry */ }) {
                    Icon(Icons.Default.Add, contentDescription = "Add")
                }
            }
        ) { padding ->
            Column(modifier = Modifier.padding(padding).fillMaxSize()) {
                when (state) {
                    is DiaryState.Loading -> {
                        CircularProgressIndicator(modifier = Modifier.padding(16.dp))
                    }
                    is DiaryState.Success -> {
                        val dates = (state as DiaryState.Success).dates
                        LazyColumn {
                            items(dates) { date ->
                                DiaryDateItem(date) {
                                    navigator?.push(ParticipantScreen(date.id, date.date))
                                }
                            }
                        }
                    }
                    is DiaryState.Error -> {
                        Text(
                            text = (state as DiaryState.Error).message,
                            color = MaterialTheme.colors.error,
                            modifier = Modifier.padding(16.dp)
                        )
                    }
                }
            }
        }
    }

    @Composable
    fun DiaryDateItem(date: de.tsschulz.tt_tagebuch.shared.models.DiaryDate, onClick: () -> Unit) {
        Card(
            modifier = Modifier.fillMaxWidth().padding(8.dp).clickable { onClick() },
            elevation = 4.dp
        ) {
            Column(modifier = Modifier.padding(16.dp)) {
                Text(date.date, style = MaterialTheme.typography.h6)
                if (date.trainingStart != null) {
                    Text("${date.trainingStart} - ${date.trainingEnd ?: ""}")
                }
            }
        }
    }
}
