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

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
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 dev.adriankuta.flights.ui.sharedui.Counter
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.todayIn

@Composable
internal fun PassengersOptions(
    uiState: HomeUiState.Success,
    onAdultCountChange: (Int) -> Unit,
    onTeenCountChange: (Int) -> Unit,
    onChildCountChange: (Int) -> Unit,
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(intrinsicSize = IntrinsicSize.Min),
        horizontalArrangement = Arrangement.spacedBy(8.dp),
    ) {
        Counter(
            value = uiState.passengers.adultCount,
            onValueChange = onAdultCountChange,
            label = "Adults",
            modifier = Modifier.weight(1f),
            minVal = 1,
        )

        VerticalDivider()

        Counter(
            value = uiState.passengers.teenCount,
            onValueChange = onTeenCountChange,
            label = "Teens",
            modifier = Modifier.weight(1f),
        )

        VerticalDivider()

        Counter(
            value = uiState.passengers.childCount,
            onValueChange = onChildCountChange,
            label = "Children",
            modifier = Modifier.weight(1f),
            maxVal = uiState.passengers.adultCount,
        )
    }
}

@PreviewDevices
@Composable
private fun PassengersOptionsPreview() {
    val samplePassengersState = PassengersState(
        adultCount = 2,
        teenCount = 1,
        childCount = 1,
    )

    val sampleUiState = HomeUiState.Success(
        originAirports = emptyList(),
        destinationAirports = emptyList(),
        selectedOriginAirport = null,
        selectedDestinationAirport = null,
        selectedDate = Clock.System.todayIn(TimeZone.currentSystemDefault()),
        passengers = samplePassengersState,
    )

    FlightsTheme {
        PassengersOptions(
            uiState = sampleUiState,
            onAdultCountChange = {},
            onTeenCountChange = {},
            onChildCountChange = {},
        )
    }
}
