package com.applemand.zmanim.feature.calendar

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.applemand.zmanim.core.design.ZmanimColors
import com.applemand.zmanim.core.jewcal.Holiday
import com.applemand.zmanim.core.ui.HolidayBadge
import com.applemand.zmanim.core.ui.HolidayTier
import com.applemand.zmanim.core.ui.PanelCard
import com.applemand.zmanim.core.ui.ZmanRow
import com.applemand.zmanim.core.zmanim.ZmanKey
import com.applemand.zmanim.core.zmanim.ZmanimFormatter

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun DayScreen(
    day: DaySummary,
    visibleZmanim: Map<String, Boolean>,
    twelveHour: Boolean,
    showSeconds: Boolean,
    modifier: Modifier = Modifier,
    contentPadding: PaddingValues = PaddingValues(16.dp),
) {
    LazyColumn(
        modifier = modifier,
        contentPadding = contentPadding,
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        item {
            Column {
                Text(
                    text = "${day.hebrewDate.dayOfMonth} ${day.hebrewDate.monthEn} ${day.hebrewDate.year}",
                    style = MaterialTheme.typography.headlineMedium,
                )
                Text(
                    text = day.hebrewDate.displayHe(),
                    style = MaterialTheme.typography.titleLarge,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }

        if (day.holidays.isNotEmpty()) {
            item {
                FlowRow(
                    horizontalArrangement = Arrangement.spacedBy(6.dp),
                    verticalArrangement = Arrangement.spacedBy(6.dp),
                ) {
                    day.holidays.forEach { h ->
                        HolidayBadge(label = h.nameEn, tier = h.tier.toUi())
                    }
                }
            }
        }

        if (day.zmanim != null) {
            item {
                PanelCard(title = "Zmanim") {
                    val fmt = ZmanimFormatter(
                        tzid = day.zmanim.location.tzid,
                        twelveHour = twelveHour,
                        showSeconds = showSeconds,
                    )
                    Column {
                        ZmanKey.entries
                            .filter { visibleZmanim[it.keyId] != false }
                            .forEach { key ->
                                ZmanRow(
                                    label = key.nameEn,
                                    time = fmt.format(day.zmanim[key]),
                                    accent = key.accent(),
                                )
                            }
                    }
                }
            }
        }

        if (day.daf != null) {
            item {
                PanelCard(title = "Daf Yomi") {
                    Text(day.daf.displayEn, style = MaterialTheme.typography.titleMedium)
                    Text(
                        day.daf.displayHe,
                        style = MaterialTheme.typography.bodyLarge,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                    )
                }
            }
        }

        if (day.omer != null) {
            item {
                PanelCard(title = "Sefirat HaOmer") {
                    Text(day.omer.displayEn, style = MaterialTheme.typography.bodyLarge)
                }
            }
        }

        if (day.parasha != null) {
            item {
                PanelCard(title = "Parashat HaShavua") {
                    Text(day.parasha.nameEn, style = MaterialTheme.typography.titleMedium)
                    Text(
                        day.parasha.nameHe,
                        style = MaterialTheme.typography.bodyLarge,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                    )
                }
            }
        }
    }
}

internal fun com.applemand.zmanim.core.jewcal.HolidayTier.toUi(): HolidayTier = when (this) {
    com.applemand.zmanim.core.jewcal.HolidayTier.YOM_TOV -> HolidayTier.YOM_TOV
    com.applemand.zmanim.core.jewcal.HolidayTier.FAST -> HolidayTier.FAST
    com.applemand.zmanim.core.jewcal.HolidayTier.MINOR -> HolidayTier.MINOR
    com.applemand.zmanim.core.jewcal.HolidayTier.ROSH_CHODESH -> HolidayTier.ROSH_CHODESH
    com.applemand.zmanim.core.jewcal.HolidayTier.SHABBAT -> HolidayTier.SHABBAT
}

internal fun ZmanKey.accent(): Color = when (this) {
    ZmanKey.ALOT_HASHACHAR -> ZmanimColors.AlotHaShachar
    ZmanKey.ZMAN_TZITZIT -> ZmanimColors.ZmanTzitzit
    ZmanKey.SUNRISE -> ZmanimColors.Sunrise
    ZmanKey.SOF_SHMA_GRA -> ZmanimColors.SofShmaGRA
    ZmanKey.SOF_SHMA_MA -> ZmanimColors.SofShmaMA
    ZmanKey.SOF_TFILA -> ZmanimColors.SofTfila
    ZmanKey.MIDDAY -> ZmanimColors.Midday
    ZmanKey.MINCHA_GEDOLAH -> ZmanimColors.MinchaGedolah
    ZmanKey.PLAG_HAMINCHA -> ZmanimColors.PlagHaMincha
    ZmanKey.SUNSET -> ZmanimColors.Sunset
    ZmanKey.CANDLE_LIGHTING -> ZmanimColors.CandleLighting
    ZmanKey.TZEIT_KOCHAVIM -> ZmanimColors.TzeitKochavim
    ZmanKey.RABBEINU_TAM -> ZmanimColors.RabbeinuTam
}
