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

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
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
import dev.adriankuta.flights.ui.home.HomeUiState
import dev.adriankuta.flights.ui.home.PassengersState
import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.plus
import kotlinx.datetime.todayIn

@Composable
internal fun SearchForm(
    uiState: HomeUiState.Success,
    onOriginAirportSelect: (AirportInfo) -> Unit,
    onDestinationAirportSelect: (AirportInfo) -> Unit,
    onDateSelect: (LocalDate) -> Unit,
    onAdultCountChange: (Int) -> Unit,
    onTeenCountChange: (Int) -> Unit,
    onChildCountChange: (Int) -> Unit,
    onSearch: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier) {
        AirportDropdown(
            label = "Origin Airport",
            airports = uiState.originAirports,
            selectedAirport = uiState.selectedOriginAirport,
            onAirportSelect = onOriginAirportSelect,
            modifier = Modifier.fillMaxWidth(),
        )

        Spacer(modifier = Modifier.height(16.dp))

        AirportDropdown(
            label = "Destination Airport",
            airports = uiState.destinationAirports,
            selectedAirport = uiState.selectedDestinationAirport,
            onAirportSelect = onDestinationAirportSelect,
            modifier = Modifier.fillMaxWidth(),
            enabled = uiState.selectedOriginAirport != null,
        )

        Spacer(modifier = Modifier.height(16.dp))

        DatePicker(
            label = "Departure Date",
            selectedDate = uiState.selectedDate,
            onDateSelect = onDateSelect,
            modifier = Modifier.fillMaxWidth(),
            enabled = uiState.selectedDestinationAirport != null,
        )

        Spacer(modifier = Modifier.height(16.dp))

        Text("Passengers")

        Spacer(modifier = Modifier.height(8.dp))

        PassengersOptions(uiState, onAdultCountChange, onTeenCountChange, onChildCountChange)

        Spacer(modifier = Modifier.height(24.dp))

        Button(
            onClick = onSearch,
            modifier = Modifier.fillMaxWidth(),
            enabled = uiState.selectedOriginAirport != null && uiState.selectedDestinationAirport != null,
        ) {
            Text("Search Flights")
        }
    }
}

@PreviewDevices
@Composable
@Suppress("LongMethod")
private fun SearchFormPreview() {
    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,
            ),
        ),
    )

    val samplePassengersState = PassengersState(
        adultCount = 2,
        teenCount = 1,
        childCount = 1,
    )

    val sampleUiState = HomeUiState.Success(
        originAirports = sampleAirports,
        destinationAirports = sampleAirports.drop(1),
        selectedOriginAirport = sampleAirports.first(),
        selectedDestinationAirport = sampleAirports.last(),
        selectedDate = Clock.System.todayIn(TimeZone.currentSystemDefault())
            .plus(7, DateTimeUnit.DAY),
        passengers = samplePassengersState,
    )

    FlightsTheme {
        SearchForm(
            uiState = sampleUiState,
            onOriginAirportSelect = {},
            onDestinationAirportSelect = {},
            onDateSelect = {},
            onAdultCountChange = {},
            onTeenCountChange = {},
            onChildCountChange = {},
            onSearch = {},
        )
    }
}
