package io.nexilis.alpha.ui.main

import Attachments
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
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.entities.Message
import io.nexilis.service.data.viewmodels.MessageViewModel

@Composable
fun Chat(
    navController: NavHostController,
    pin: String,
    me: Buddy
) {
    var textInput by rememberSaveable(stateSaver = TextFieldValue.Saver) {
        mutableStateOf(TextFieldValue("", TextRange(0, 7)))
    }
    val messageModel: MessageViewModel = hiltViewModel()
    val list by messageModel.getOpposite(pin).observeAsState()
    val state = rememberLazyListState()
    val context = LocalContext.current
    Column(
        modifier = Modifier
            .fillMaxSize()
            .onSizeChanged {

            }
            .background(color = MaterialTheme.colorScheme.surfaceContainer)
            .padding(start = 8.dp, end = 8.dp, bottom = 8.dp)
    ) {
        LazyColumn(
            modifier = Modifier.weight(1.0f),
            state = state,
            reverseLayout = true
        ) {
            items(count = list?.size ?: 0) {
                list?.let { l ->
                    val message = l[it]
                    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,
                                ) {
                                    Text(
                                        text = list?.get(it)?.message_text ?: "",
                                        style = MaterialTheme.typography.bodyMedium,
                                    )
                                }
                            } else {
                                LeftBubbleChat(
                                    modifier = Modifier.weight(weight = 0.9f, fill = false),
                                    message = message
                                ) {
                                    Text(
                                        text = list?.get(it)?.message_text ?: "",
                                        style = MaterialTheme.typography.bodyMedium,
                                    )
                                }
                                Spacer(modifier = Modifier.weight(0.1f))
                            }
                        }
                    }, colors = ListItemDefaults.colors(containerColor = Color.Transparent))
                }

            }
        }
        TextField(
            modifier = Modifier
                .fillMaxWidth()
                .graphicsLayer {
                    shape = CircleShape
                    clip = true
                }
                .imePadding(),
            value = textInput,
            onValueChange = { textInput = it },
            label = null,
            placeholder = {
                Text(text = "Typing here...", color = MaterialTheme.colorScheme.onSurface)
            },
            leadingIcon = {
                Attachments(
                    modifier = Modifier
                        .height(300.dp)
                        .fillMaxWidth()
                        .padding(start = 16.dp, end = 16.dp, bottom = 8.dp)
                        .graphicsLayer {
                            shape = RoundedCornerShape(16.dp)
                            clip = true
                        }
                        .background(MaterialTheme.colorScheme.surface)
                )
            },
            trailingIcon = {
                Row {
                    IconButton(onClick = {
                        if (pin.trim().isEmpty()) {
                            return@IconButton
                        }
                        if (textInput.text.trim().isEmpty()) {
                            return@IconButton
                        }
                        me.let {
                            messageModel.send(
                                context,
                                Message(
                                    message_id = System.nanoTime().toString(),
                                    f_pin = it.f_pin,
                                    l_pin = pin,
                                    message_scope_id = "3", // 3: chat, 15: sms, 16: email
                                    server_date = System.currentTimeMillis(),
                                    status = "1",
                                    message_text = textInput.text,
                                    opposite_pin = pin,
                                    f_display_name = it.first_name + " " + it.last_name,
                                    message_large_text = textInput.text,
                                    message_text_plain = textInput.text
                                )
                            )
                            textInput = TextFieldValue("")
                        }
                    }) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Filled.Send,
                            contentDescription = "",
                            tint = MaterialTheme.colorScheme.primary
                        )
                    }
                }
            },
            colors = TextFieldDefaults.colors(
                focusedContainerColor = MaterialTheme.colorScheme.surface,
                unfocusedContainerColor = MaterialTheme.colorScheme.surface,
                disabledContainerColor = MaterialTheme.colorScheme.surface,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent,
                disabledIndicatorColor = Color.Transparent,
                errorIndicatorColor = Color.Transparent,
            ),
            keyboardOptions = KeyboardOptions(KeyboardCapitalization.Sentences),
            maxLines = 3
        )
    }
}

