package dev.adriankuta.flights.ui.home.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.MenuAnchorType
import androidx.compose.material3.OutlinedTextField
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.Modifier
import dev.adriankuta.flights.domain.types.AirportInfo
import dev.adriankuta.flights.domain.types.City
import dev.adriankuta.flights.domain.types.Coordinates
import dev.adriankuta.flights.domain.types.Country
import dev.adriankuta.flights.domain.types.MacCity
import dev.adriankuta.flights.domain.types.Region
import dev.adriankuta.flights.ui.designsystem.theme.FlightsTheme
import dev.adriankuta.flights.ui.designsystem.theme.PreviewDevices

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AirportDropdown(
    label: String,
    airports: List<AirportInfo>,
    selectedAirport: AirportInfo?,
    onAirportSelect: (AirportInfo) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
) {
    var expanded by remember { mutableStateOf(false) }

    ExposedDropdownMenuBox(
        expanded = expanded,
        onExpandedChange = { if (enabled) expanded = it },
        modifier = modifier,
    ) {
        OutlinedTextField(
            value = selectedAirport?.let { "${it.name} (${it.code})" } ?: "",
            onValueChange = {},
            readOnly = true,
            label = { Text(label) },
            trailingIcon = {
                ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
            },
            colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors(),
            modifier = Modifier
                .fillMaxWidth()
                .menuAnchor(MenuAnchorType.PrimaryNotEditable, enabled),
            enabled = enabled,
        )

        ExposedDropdownMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
        ) {
            airports.forEach { airport ->
                DropdownMenuItem(
                    text = { Text("${airport.name} (${airport.code})") },
                    onClick = {
                        onAirportSelect(airport)
                        expanded = false
                    },
                )
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@PreviewDevices
@Composable
@Suppress("LongMethod")
private fun AirportDropdownPreview() {
    val sampleAirports = listOf(
        AirportInfo(
            code = "WAW",
            name = "Warsaw Chopin",
            seoName = "warsaw",
            isBase = true,
            timeZone = "Europe/Warsaw",
            city = City(
                code = "WAW",
                name = "Warsaw",
            ),
            macCity = MacCity(
                macCode = "WAW",
            ),
            region = Region(
                code = "PL",
                name = "Poland",
            ),
            country = Country(
                code = "PL",
                name = "Poland",
                currencyCode = "PLN",
            ),
            coordinates = Coordinates(
                latitude = 52.1657,
                longitude = 20.9671,
            ),
        ),
        AirportInfo(
            code = "LHR",
            name = "London Heathrow",
            seoName = "london",
            isBase = false,
            timeZone = "Europe/London",
            city = City(
                code = "LON",
                name = "London",
            ),
            macCity = MacCity(
                macCode = "LON",
            ),
            region = Region(
                code = "UK",
                name = "United Kingdom",
            ),
            country = Country(
                code = "UK",
                name = "United Kingdom",
                currencyCode = "GBP",
            ),
            coordinates = Coordinates(
                latitude = 51.4700,
                longitude = -0.4543,
            ),
        ),
    )

    FlightsTheme {
        AirportDropdown(
            label = "Departure Airport",
            airports = sampleAirports,
            selectedAirport = sampleAirports.first(),
            onAirportSelect = {},
        )
    }
}
