package io.nexilis.alpha.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.InsertDriveFile
import androidx.compose.material.icons.filled.Headset
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.core.content.FileProvider
import coil.compose.rememberAsyncImagePainter
import io.nexilis.alpha.BuildConfig
import io.nexilis.service.core.DarkLimeGreen
import io.nexilis.service.core.Orange
import io.nexilis.service.data.entities.Message
import java.io.File

@Composable
fun ContentChat(modifier: Modifier, message: Message) {
    val context = LocalContext.current
    if (message.image_id.isNotEmpty()) {
        Column(modifier = Modifier.padding(2.dp)) {
            val file = File(context.filesDir, message.image_id)
            val uri = FileProvider.getUriForFile(
                context, BuildConfig.APPLICATION_ID + ".provider", file
            )
            Image(
                painter = rememberAsyncImagePainter(uri),
                contentDescription = "",
                modifier = modifier
                    .aspectRatio(3f / 4f)
                    .graphicsLayer {
                        shape = RoundedCornerShape(12.dp)
                        clip = true
                    },
                contentScale = ContentScale.Crop
            )
            if (message.message_text.isNotEmpty()) {
                Text(
                    text = message.message_text,
                    modifier = Modifier.padding(8.dp),
                    color = MaterialTheme.colorScheme.onSurface,
                    style = MaterialTheme.typography.bodyMedium,
                )
            }
        }
    } else if (message.file_id.isNotEmpty()) {
        Column(
            modifier = Modifier
                .padding(2.dp)
                .widthIn(max = 210.dp)
        ) {
            Row(modifier = Modifier
                .graphicsLayer {
                    shape = RoundedCornerShape(12.dp)
                    clip = true
                }
                .background(MaterialTheme.colorScheme.surface)) {
                Icon(
                    modifier = Modifier
                        .size(48.dp)
                        .padding(start = 8.dp)
                        .align(Alignment.CenterVertically),
                    imageVector = Icons.AutoMirrored.Filled.InsertDriveFile,
                    contentDescription = null,
                    tint = Color.DarkLimeGreen
                )
                Column(
                    modifier = Modifier.padding(
                        start = 4.dp,
                        top = 8.dp,
                        end = 8.dp,
                        bottom = 8.dp
                    )
                ) {
                    Text(
                        text = message.message_text.split("|").first(),
                        color = MaterialTheme.colorScheme.onSurface,
                        style = MaterialTheme.typography.bodyMedium,
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                    Text(
                        text = "11 KB • XLSX",
                        color = MaterialTheme.colorScheme.onSurface,
                        style = MaterialTheme.typography.labelSmall,
                    )
                }
            }
            val messageText = message.message_text.split("|").last()
            if (messageText.isNotEmpty()) {
                Text(
                    text = messageText,
                    modifier = Modifier.padding(8.dp),
                    color = MaterialTheme.colorScheme.onSurface,
                    style = MaterialTheme.typography.bodyMedium,
                )
            }
        }
    } else if (message.audio_id.isNotEmpty()) {
        Column(
            modifier = Modifier
                .padding(2.dp)
                .widthIn(max = 210.dp)
        ) {
            Row(modifier = Modifier
                .graphicsLayer {
                    shape = RoundedCornerShape(12.dp)
                    clip = true
                }
                .background(MaterialTheme.colorScheme.surface)) {
                Icon(
                    modifier = Modifier
                        .size(48.dp)
                        .padding(start = 8.dp)
                        .align(Alignment.CenterVertically),
                    imageVector = Icons.Default.Headset,
                    contentDescription = null,
                    tint = Color.Orange
                )
                Column {
                    Row(
                        modifier = Modifier.padding(
                            start = 4.dp,
                            top = 8.dp,
                            end = 8.dp,
                            bottom = 8.dp
                        )
                    ) {
                        Icon(
                            modifier = Modifier
                                .size(32.dp)
                                .align(Alignment.CenterVertically),
                            imageVector = Icons.Default.PlayArrow,
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary
                        )
                        LinearProgressIndicator(
                            progress = { 0.5f },
                            modifier = Modifier
                                .align(Alignment.CenterVertically)
                                .fillMaxWidth(),
                        )
                    }
                    Text(
                        modifier = Modifier.padding(
                            start = 4.dp,
                            top = 8.dp,
                            end = 8.dp,
                            bottom = 8.dp
                        ),
                        color = MaterialTheme.colorScheme.onSurface,
                        text = message.message_text.split("|").first(),
                        style = MaterialTheme.typography.labelSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
                }
            }
            val messageText = message.message_text.split("|").last()
            if (messageText.isNotEmpty()) {
                Text(
                    text = messageText,
                    modifier = Modifier.padding(8.dp),
                    color = MaterialTheme.colorScheme.onSurface,
                    style = MaterialTheme.typography.bodyMedium,
                )
            }
        }
    } else {
        Text(
            text = message.message_text,
            modifier = Modifier.padding(8.dp),
            color = MaterialTheme.colorScheme.onSurface,
            style = MaterialTheme.typography.bodyMedium,
        )
    }
}