package io.nexilis.alpha.ui.main

import androidx.compose.foundation.layout.*
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.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
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
import io.nexilis.service.data.viewmodels.MessageSummaryViewModel
import io.nexilis.service.data.viewmodels.MessageViewModel

@Composable
fun Chats(
    navController: NavHostController,
    contentPadding: PaddingValues = PaddingValues(0.dp)
) {
    val messageSummaryViewModel: MessageSummaryViewModel = hiltViewModel()
    val chats by messageSummaryViewModel.all.observeAsState()
    val messageViewModel: MessageViewModel = hiltViewModel()
    val buddyViewModel: BuddyViewModel = hiltViewModel()
    var selectedItem by remember { mutableIntStateOf(0) }
    LazyColumn(
        contentPadding = contentPadding,
    ) {
        items(
            count = chats?.size ?: 0,
            key = {
                chats?.get(it)?.l_pin ?: 0
            }
        ) { item ->
            chats?.get(item)?.let { chat ->
                val buddy by buddyViewModel.getBuddy(chat.l_pin).observeAsState()
                ListItem(
                    modifier = Modifier.selectable(selected = selectedItem == item, onClick = {
                        selectedItem = item
                        navController.navigate(Screen.Chat.route + "/${chat.l_pin}") {
                            launchSingleTop = true
                            restoreState = true
                        }
                    }),
                    headlineContent = {
                        buddy?.let {
                            Text(
                                "${it.first_name} ${it.last_name}".trim(),
                                style = MaterialTheme.typography.titleSmall
                            )
                        }
                    },
                    supportingContent = {
                        val message by messageViewModel.get(chat.message_id).observeAsState()
                        message?.let {
                            Text(
                                it.message_text,
                                style = MaterialTheme.typography.bodySmall,
                                overflow = TextOverflow.Ellipsis,
                                maxLines = 1
                            )
                        }
                    },
                    leadingContent = {
                        buddy?.let {
                            Person(buddy = it)
                        }
                    },
                    trailingContent = {
                        BadgedBox(
                            badge = {
                                Badge {
                                    Text(
                                        item.toString(),
                                        modifier = Modifier.semantics {
                                            contentDescription = "$item new notifications"
                                        }
                                    )
                                }
                            }) {
                        }
                    }
                )
            }
        }
    }
}