package io.nexilis.alpha.ui.main

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.selection.selectable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import io.nexilis.alpha.ui.components.Person
import io.nexilis.alpha.ui.screen.Screen
import io.nexilis.service.data.viewmodels.BuddyViewModel

@Composable
fun Contact(navController: NavHostController) {
    val viewModel: BuddyViewModel = hiltViewModel()
    val all by viewModel.all.observeAsState()
    var selectedItem by remember { mutableStateOf(0) }
    LazyColumn {
        items(
            count = all?.size ?: 0,
            key = {
                all?.get(it)?.f_pin ?: 0
            }
        ) { item ->
            all?.get(item)?.let {
                ListItem(
                    modifier = Modifier.selectable(selected = selectedItem == item, onClick = {
                        selectedItem = item
                        navController.navigate(Screen.Chat.route + "/${it.f_pin}") {
                            launchSingleTop = true
                            restoreState = true
                        }
                    }),
                    headlineContent = {
                        Text(
                            it.first_name,
                            style = MaterialTheme.typography.titleSmall
                        )
                    },
                    supportingContent = {
                        Text(
                            "",
                            style = MaterialTheme.typography.bodySmall
                        )
                    },
                    leadingContent = {
                        Person(buddy = it)
                    }
                )
            }
        }
    }
}