package io.nexilis.alpha.ui.main

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import io.nexilis.alpha.ui.components.ContentChat
import io.nexilis.alpha.ui.components.InputChat
import io.nexilis.alpha.ui.components.LeftBubbleChat
import io.nexilis.alpha.ui.components.RightBubbleChat
import io.nexilis.service.data.entities.Buddy
import io.nexilis.service.data.viewmodels.MessageViewModel

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun Chat(
    navController: NavHostController,
    contentPadding: PaddingValues,
    pin: String,
    me: Buddy
) {
    val messageModel: MessageViewModel = hiltViewModel()
    val list by messageModel.getOpposite(pin).observeAsState()
    val state = rememberLazyListState()
    Column(
        modifier = Modifier
            .fillMaxSize()
            .consumeWindowInsets(contentPadding)
            .imePadding()
            .imeNestedScroll()
    ) {
        LazyColumn(
            modifier = Modifier
                .weight(1f),
            state = state,
            reverseLayout = true
        ) {
            list?.let { l ->
                items(l) { message ->
                    ListItem(modifier = Modifier.fillMaxWidth(), headlineContent = {
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = if (message.f_pin == me.f_pin) Arrangement.End else Arrangement.Start
                        ) {
                            if (message.f_pin == me.f_pin) {
                                Spacer(modifier = Modifier.weight(0.1f))
                                RightBubbleChat(
                                    modifier = Modifier.weight(weight = 0.9f, fill = false),
                                    message = message,
                                ) {
                                    ContentChat(
                                        modifier = Modifier.fillMaxWidth(0.7f),
                                        message = message
                                    )
                                }
                            } else {
                                LeftBubbleChat(
                                    modifier = Modifier.weight(weight = 0.9f, fill = false),
                                    message = message
                                ) {
                                    ContentChat(
                                        modifier = Modifier.fillMaxWidth(0.7f),
                                        message = message
                                    )
                                }
                                Spacer(modifier = Modifier.weight(0.1f))
                            }
                        }
                    }, colors = ListItemDefaults.colors(containerColor = Color.Transparent))
                }
            }
        }
        InputChat(navController = navController, pin = pin, me = me)
    }
}

