package io.nexilis.alpha.ui.main

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.size
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.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

@Composable
fun Contact(navController: NavHostController, mainViewModel: MainViewModel) {
    SideEffect {
        mainViewModel.setTitle("Contact")
    }
    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 ->
            val buddy = all?.get(item)
            ListItem(
                modifier = Modifier.selectable(selected = selectedItem == item, onClick = {
                    selectedItem = item
                    navController.navigate(Screen.Chat.route + "/${buddy?.f_pin}") {
                        launchSingleTop = true
                        restoreState = true
                    }
                }),
                headlineContent = {
                    Text(
                        buddy?.first_name ?: "",
                        style = MaterialTheme.typography.titleSmall
                    )
                },
                supportingContent = {
                    Text(
                        "",
                        style = MaterialTheme.typography.bodySmall
                    )
                },
                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)
                    )
                }
            )
        }
    }
}