package com.applemand.zmanim.feature.dafyomi

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.applemand.zmanim.core.jewcal.DafYomiCalculator
import com.applemand.zmanim.core.ui.PanelCard
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DafYomiDetailScreen(
    onBack: () -> Unit,
    date: LocalDate? = null,
    modifier: Modifier = Modifier,
) {
    val today = remember { Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date }
    val d = date ?: today
    val dafCalc = remember { DafYomiCalculator() }
    val bavli = remember(d) { dafCalc.bavli(d) }
    val yerushalmi = remember(d) { dafCalc.yerushalmi(d) }

    Scaffold(
        modifier = modifier,
        topBar = {
            TopAppBar(
                title = { Text("Daf Yomi") },
                navigationIcon = {
                    IconButton(onClick = onBack) {
                        Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
                    }
                },
            )
        },
    ) { padding ->
        Column(
            modifier = Modifier.fillMaxSize().padding(padding).verticalScroll(rememberScrollState()),
            verticalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            PanelCard(title = "Bavli", modifier = Modifier.padding(16.dp)) {
                if (bavli != null) {
                    Text(bavli.displayEn, style = MaterialTheme.typography.headlineSmall)
                    Text(bavli.displayHe, style = MaterialTheme.typography.titleLarge,
                        color = MaterialTheme.colorScheme.onSurfaceVariant)
                } else Text("Outside current cycle")
            }
            PanelCard(title = "Yerushalmi (Vilna)", modifier = Modifier.padding(horizontal = 16.dp)) {
                if (yerushalmi != null) {
                    Text(yerushalmi.displayEn, style = MaterialTheme.typography.titleLarge)
                    Text(yerushalmi.displayHe, style = MaterialTheme.typography.titleMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant)
                } else Text("Outside current cycle")
            }
        }
    }
}
