package com.applemand.zmanim.feature.calendar.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.ArrowForward
import androidx.compose.material.icons.automirrored.filled.MenuBook
import androidx.compose.material.icons.filled.FileUpload
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Place
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Today
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.FilledIconButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.applemand.zmanim.core.jewcal.HebrewMonthNames
import com.applemand.zmanim.core.jewcal.HebrewDate
import com.applemand.zmanim.feature.calendar.CalendarView
import kotlinx.datetime.LocalDate

@Composable
fun CalendarHeader(
    currentDate: LocalDate,
    hebrewDate: HebrewDate?,
    view: CalendarView,
    onPrev: () -> Unit,
    onNext: () -> Unit,
    onToday: () -> Unit,
    onViewChange: (CalendarView) -> Unit,
    onOpenLocationPicker: () -> Unit,
    onOpenSettings: () -> Unit,
    onOpenDafYomi: () -> Unit,
    onOpenIcsExport: () -> Unit,
    modifier: Modifier = Modifier,
) {
    var menuOpen by remember { mutableStateOf(false) }
    Row(
        modifier = modifier
            .fillMaxWidth()
            .statusBarsPadding()
            .padding(horizontal = 16.dp, vertical = 12.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            IconButton(onClick = onPrev) {
                Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Previous")
            }
            FilledIconButton(onClick = onToday) {
                Icon(Icons.Filled.Today, contentDescription = "Today")
            }
            IconButton(onClick = onNext) {
                Icon(Icons.AutoMirrored.Filled.ArrowForward, contentDescription = "Next")
            }
        }

        androidx.compose.foundation.layout.Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(
                text = "${currentDate.month.name.lowercase().replaceFirstChar { it.uppercase() }} ${currentDate.year}",
                style = MaterialTheme.typography.titleLarge,
            )
            if (hebrewDate != null) {
                Text(
                    text = HebrewMonthNames.he(hebrewDate.monthId) + " " + hebrewDate.yearHe,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }

        Row(verticalAlignment = Alignment.CenterVertically) {
            IconButton(onClick = onOpenLocationPicker) {
                Icon(Icons.Filled.Place, contentDescription = "Location")
            }
            IconButton(onClick = onOpenSettings) {
                Icon(Icons.Filled.Settings, contentDescription = "Settings")
            }
            IconButton(onClick = { menuOpen = true }) {
                Icon(Icons.Filled.MoreVert, contentDescription = "More")
            }
            DropdownMenu(expanded = menuOpen, onDismissRequest = { menuOpen = false }) {
                DropdownMenuItem(
                    text = { Text("Daf Yomi") },
                    onClick = { menuOpen = false; onOpenDafYomi() },
                    leadingIcon = { Icon(Icons.AutoMirrored.Filled.MenuBook, contentDescription = null) },
                )
                DropdownMenuItem(
                    text = { Text("Export to calendar") },
                    onClick = { menuOpen = false; onOpenIcsExport() },
                    leadingIcon = { Icon(Icons.Filled.FileUpload, contentDescription = null) },
                )
            }
        }
    }
}
