package io.nexilis.alpha.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
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 androidx.navigation.compose.currentBackStackEntryAsState
import coil.compose.AsyncImage
import coil.request.ImageRequest
import io.nexilis.alpha.ui.screen.Screen
import io.nexilis.alpha.ui.screen.screenItems
import io.nexilis.service.data.viewmodels.BuddyViewModel

@Composable
fun Title(navController: NavHostController, navMainController: NavHostController) {
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val navMainBackStackEntry by navMainController.currentBackStackEntryAsState()
    val buddyViewModel: BuddyViewModel = hiltViewModel()
    var title = ""
    navBackStackEntry?.destination?.route?.let { route ->
        try {
            title = screenItems.first { it.route == route }.title
        } catch (ignored: Exception) {
        }
    }
    navMainBackStackEntry?.destination?.route?.let { route ->
        try {
            title = screenItems.first { it.route == route }.title
        } catch (ignored: Exception) {
        }
    }
    var isChat = false
    var pin = ""
    navMainBackStackEntry?.destination?.route?.let {
        isChat = it.startsWith("${Screen.Chat.route}/")
    }
    navMainBackStackEntry?.arguments?.getString("pin")
        ?.let {
            pin = it
        }
    if (isChat && pin.isNotEmpty()) {
        Row {
            val buddy by buddyViewModel.getBuddy(pin).observeAsState()
            buddy?.let {
                AsyncImage(
                    model = ImageRequest.Builder(LocalContext.current)
                        .data("https://digixplatform.com/filepalio/image/${it.image_id}")
                        .addHeader("Cookie", "PHPSESSID=123;MOBILE=123")
                        .crossfade(true)
                        .build(),
                    placeholder = rememberVectorPainter(image = Icons.Default.Person),
                    contentDescription = "",
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .align(Alignment.CenterVertically)
                        .size(48.dp)
                        .clip(CircleShape)
                        .background(Color.LightGray),
                    error = rememberVectorPainter(image = Icons.Default.Person),
                )
                Spacer(modifier = Modifier.width(8.dp))
                Text(
                    modifier = Modifier.align(Alignment.CenterVertically),
                    text = "${buddy?.first_name} ${it.last_name}".trim()
                )
            }
        }
    } else {
        Text(text = title)
    }
}