package dev.adriankuta.flights.ui.home

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.adriankuta.flights.ui.designsystem.theme.FlightsTheme
import dev.adriankuta.flights.ui.designsystem.theme.PreviewDevices

@Composable
internal fun HomeScreen(
    viewModel: HomeScreenViewModel = hiltViewModel(),
) {
    val homeUiState by viewModel.uiState.collectAsStateWithLifecycle()

    HomeScreen(
        uiState = homeUiState,
    )
}

@Composable
private fun HomeScreen(
    uiState: HomeUiState,
    modifier: Modifier = Modifier,
) {
    LazyColumn(
        modifier = modifier,
    ) {
        when (uiState) {
            is HomeUiState.Error -> item { Text("Error") }
            HomeUiState.Loading -> item { Text("Loading") }
            is HomeUiState.Success -> items(uiState.airports) { airport ->
                Text(airport.name)
            }
        }
    }
}

@PreviewDevices
@Composable
private fun HomeScreenPreview() {
    FlightsTheme {
        HomeScreen(
            uiState = HomeUiState.Loading,
        )
    }
}
