package io.nexilis.alpha.ui.main

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
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 coil.compose.AsyncImage
import coil.request.ImageRequest
import io.nexilis.alpha.ui.screen.Screen
import io.nexilis.alpha.ui.viewmodel.MainViewModel
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),
    mainViewModel: MainViewModel
) {
    SideEffect {
        mainViewModel.setTitle("Chats")
    }
    val messageSummaryViewModel: MessageSummaryViewModel = hiltViewModel()
    val chats by messageSummaryViewModel.all.observeAsState()
    val messageViewModel: MessageViewModel = hiltViewModel()
    val buddyViewModel: BuddyViewModel = hiltViewModel()
    var selectedItem by remember { mutableStateOf(0) }
    LazyColumn(
        contentPadding = contentPadding,
    ) {
        items(
            count = chats?.size ?: 0,
            key = {
                chats?.get(it)?.l_pin ?: 0
            }
        ) { item ->
            val chat = chats?.get(item)
            val message by messageViewModel.get(chat?.message_id ?: "").observeAsState()
            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 = {
                    Text(
                        "${buddy?.first_name ?: "Bot"} ${buddy?.last_name ?: ""}".trim(),
                        style = MaterialTheme.typography.titleSmall
                    )
                },
                supportingContent = {
                    Text(
                        message?.message_text ?: "Secondary text",
                        style = MaterialTheme.typography.bodySmall,
                        overflow = TextOverflow.Ellipsis,
                        maxLines = 1
                    )
                },
                leadingContent = {
                    AsyncImage(
                        model = ImageRequest.Builder(LocalContext.current)
                            .data("https://digixplatform.com/filepalio/image/${buddy?.image_id}")
                            .addHeader("Cookie", "PHPSESSID=123;MOBILE=123")
                            .crossfade(true)
                            .build(),
                        placeholder = rememberVectorPainter(image = Icons.Default.Person),
                        contentDescription = "",
                        contentScale = ContentScale.Crop,
                        modifier = Modifier
                            .size(48.dp)
                            .clip(CircleShape)
                            .background(Color.LightGray),
//                            .padding(8.dp),
                        error = rememberVectorPainter(image = Icons.Default.Person),
//                        colorFilter = ColorFilter.tint(Color.White)
                    )
                },
                trailingContent = {
                    BadgedBox(
                        badge = {
                            Badge {
                                Text(
                                    item.toString(),
                                    modifier = Modifier.semantics {
                                        contentDescription = "$item new notifications"
                                    }
                                )
                            }
                        }) {
                    }
                }
            )
        }
    }
}