package com.applemand.zmanim.feature.calendar

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.applemand.zmanim.core.datetime.DateRanges
import kotlinx.datetime.LocalDate

@Composable
fun YearScreen(
    year: Int,
    onMonthSelect: (Int) -> Unit,
    modifier: Modifier = Modifier,
    contentPadding: PaddingValues = PaddingValues(12.dp),
) {
    BoxWithConstraints(modifier.fillMaxSize()) {
        val columns = when {
            maxWidth > 1000.dp -> 4
            maxWidth > 640.dp -> 3
            else -> 2
        }
        LazyVerticalGrid(
            columns = GridCells.Fixed(columns),
            contentPadding = contentPadding,
            verticalArrangement = Arrangement.spacedBy(12.dp),
            horizontalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            items(12, key = { it }) { idx ->
                MiniMonth(year = year, monthIndex = idx + 1, onClick = { onMonthSelect(idx + 1) })
            }
        }
    }
}

@Composable
private fun MiniMonth(year: Int, monthIndex: Int, onClick: () -> Unit) {
    val days = DateRanges.monthGrid(year, monthIndex)
    ElevatedCard(
        modifier = Modifier.fillMaxWidth().clickable(onClick = onClick),
        colors = CardDefaults.elevatedCardColors(containerColor = MaterialTheme.colorScheme.surface),
    ) {
        Column(Modifier.padding(12.dp)) {
            Text(
                text = LocalDate(year, monthIndex, 1).month.name
                    .lowercase().replaceFirstChar { it.uppercase() },
                style = MaterialTheme.typography.titleMedium,
            )
            Column(Modifier.fillMaxWidth().padding(top = 8.dp)) {
                val rows = (days.size + 6) / 7
                repeat(rows) { r ->
                    Row(Modifier.fillMaxWidth()) {
                        repeat(7) { c ->
                            val i = r * 7 + c
                            Box(
                                modifier = Modifier.weight(1f).padding(2.dp),
                                contentAlignment = Alignment.Center,
                            ) {
                                if (i < days.size) {
                                    val date = days[i]
                                    Text(
                                        text = date.dayOfMonth.toString(),
                                        style = MaterialTheme.typography.labelSmall,
                                        color = if (date.monthNumber == monthIndex)
                                            MaterialTheme.colorScheme.onSurface
                                        else MaterialTheme.colorScheme.onSurfaceVariant,
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
