package io.nexilis.alpha.ui.components

import android.text.format.DateUtils
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccessTime
import androidx.compose.material.icons.filled.Done
import androidx.compose.material.icons.filled.DoneAll
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
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.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import io.nexilis.service.core.BlueSky
import io.nexilis.service.data.entities.Message
import io.nexilis.service.data.viewmodels.MessageStatusViewModel

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RightBubbleChat(
    modifier: Modifier,
    message: Message,
    messageStatusModel: MessageStatusViewModel = hiltViewModel(),
    content: @Composable BoxScope.() -> Unit
) {
    Column(
        modifier = modifier
    ) {
        Box(
            modifier = Modifier
                .align(Alignment.End)
                .graphicsLayer {
                    shape = RoundedCornerShape(12.dp)
                    clip = true
                }
                .combinedClickable(onLongClick = {}, onClick = {})
                .background(color = MaterialTheme.colorScheme.surfaceContainer),
            content = content
        )
        Row(
            modifier = Modifier
                .padding(3.dp)
                .align(Alignment.End)
        ) {
            Text(
                text = DateUtils.formatDateTime(
                    LocalContext.current,
                    message.server_date,
                    DateUtils.FORMAT_SHOW_TIME
                ),
                color = MaterialTheme.colorScheme.onSurface,
                style = MaterialTheme.typography.bodySmall,
            )
            Spacer(modifier = Modifier.width(6.dp))
            val messageStatus by messageStatusModel.get(message.message_id, message.opposite_pin)
                .observeAsState()
            Icon(
                imageVector = when (messageStatus?.status) {
                    1, 2 -> Icons.Default.Done
                    3 -> Icons.Default.DoneAll
                    4 -> Icons.Default.DoneAll
                    else -> Icons.Default.AccessTime
                },
                contentDescription = "",
                modifier = Modifier
                    .align(Alignment.Bottom)
                    .size(16.dp),
                tint = if (messageStatus?.status == 4) Color.BlueSky else Color.Gray
            )
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LeftBubbleChat(
    modifier: Modifier,
    message: Message,
    content: @Composable BoxScope.() -> Unit
) {
    Column(
        modifier = modifier
    ) {
        Box(
            modifier = Modifier
                .align(Alignment.Start)
                .graphicsLayer {
                    shape = RoundedCornerShape(12.dp)
                    clip = true
                }
                .combinedClickable(onLongClick = {}, onClick = {})
                .background(color = MaterialTheme.colorScheme.surfaceContainerLowest),
            content = content
        )
        Row(
            modifier = Modifier
                .padding(3.dp)
                .align(Alignment.End)
        ) {
            Text(
                text = DateUtils.formatDateTime(
                    LocalContext.current,
                    message.server_date,
                    DateUtils.FORMAT_SHOW_TIME
                ),
                color = MaterialTheme.colorScheme.onSurface,
                style = MaterialTheme.typography.bodySmall,
            )
        }
    }
}