package com.applemand.zmanim.feature.calendar

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.border
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.height
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.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.applemand.zmanim.core.jewcal.HebrewMonthNames
import com.applemand.zmanim.core.ui.HolidayBadge
import kotlinx.datetime.LocalDate

@Composable
fun MonthScreen(
    year: Int,
    month: Int,
    days: List<DaySummary>,
    today: LocalDate,
    onDaySelect: (LocalDate) -> Unit,
    modifier: Modifier = Modifier,
    contentPadding: PaddingValues = PaddingValues(12.dp),
) {
    val weekdays = listOf("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
    Column(modifier = modifier.padding(contentPadding)) {
        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround) {
            weekdays.forEach { w ->
                Text(
                    text = w,
                    style = MaterialTheme.typography.labelMedium,
                    fontWeight = FontWeight.SemiBold,
                    modifier = Modifier.weight(1f),
                )
            }
        }
        BoxWithConstraints(Modifier.fillMaxSize()) {
            val cellSize = maxWidth / 7
            LazyVerticalGrid(
                columns = GridCells.Fixed(7),
                verticalArrangement = Arrangement.spacedBy(4.dp),
                horizontalArrangement = Arrangement.spacedBy(4.dp),
            ) {
                items(days, key = { it.date.toEpochDays() }) { day ->
                    MonthDayCell(
                        day = day,
                        inMonth = day.date.monthNumber == month,
                        isToday = day.date == today,
                        compact = cellSize < 72.dp,
                        onClick = { onDaySelect(day.date) },
                    )
                }
            }
        }
    }
}

@Composable
private fun MonthDayCell(
    day: DaySummary,
    inMonth: Boolean,
    isToday: Boolean,
    compact: Boolean,
    onClick: () -> Unit,
) {
    val isShabbat = day.date.dayOfWeek.ordinal == 5
    val bg = when {
        isShabbat && inMonth -> MaterialTheme.colorScheme.secondaryContainer
        !inMonth -> MaterialTheme.colorScheme.surface
        else -> MaterialTheme.colorScheme.surface
    }
    val border = if (isToday) BorderStroke(2.dp, MaterialTheme.colorScheme.primary) else null
    Surface(
        color = bg,
        shape = RoundedCornerShape(10.dp),
        modifier = Modifier
            .fillMaxWidth()
            .height(if (compact) 64.dp else 96.dp)
            .clickable(onClick = onClick)
            .then(if (border != null) Modifier.border(border, RoundedCornerShape(10.dp)) else Modifier),
    ) {
        Column(Modifier.padding(6.dp)) {
            Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth()) {
                Text(
                    text = day.date.dayOfMonth.toString(),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = if (isToday) FontWeight.Bold else FontWeight.SemiBold,
                    color = if (inMonth) MaterialTheme.colorScheme.onSurface
                            else MaterialTheme.colorScheme.onSurfaceVariant,
                )
                Text(
                    text = day.hebrewDate.dayHe,
                    style = MaterialTheme.typography.labelSmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
            if (!compact && day.holidays.isNotEmpty()) {
                val first = day.holidays.first()
                HolidayBadge(label = first.nameEn, tier = first.tier.toUi(),
                    modifier = Modifier.padding(top = 2.dp))
            }
        }
    }
}
