package com.garfiec.librechat.feature.chat.components

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.CallSplit
import androidx.compose.material.icons.automirrored.filled.VolumeUp
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Stop
import androidx.compose.material.icons.filled.ThumbDown
import androidx.compose.material.icons.filled.ThumbUp
import androidx.compose.material.icons.outlined.ThumbDown
import androidx.compose.material.icons.outlined.ThumbUp
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.unit.dp
import com.garfiec.librechat.core.model.Message
import librechat_mobile.feature.chat.generated.resources.Res
import librechat_mobile.feature.chat.generated.resources.*
import org.jetbrains.compose.resources.stringResource

/**
 * Shared action buttons row for message bubbles (copy, edit, regenerate, feedback, read aloud, fork).
 * Used by both Android and iOS MessageBubble implementations.
 */
@Composable
internal fun ActionButtons(
    isUser: Boolean,
    onFeedback: ((String?) -> Unit)?,
    currentFeedback: String?,
    onShowFeedbackDialog: () -> Unit,
    onCopy: (() -> Unit)?,
    onEdit: (() -> Unit)?,
    onRegenerate: (() -> Unit)?,
    onReadAloud: (() -> Unit)?,
    isReading: Boolean,
    onFork: (() -> Unit)?,
) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        // Feedback buttons (AI messages only)
        if (!isUser && onFeedback != null) {
            IconButton(
                onClick = { onFeedback(if (currentFeedback == "thumbsUp") null else "thumbsUp") },
                modifier = Modifier.size(40.dp),
            ) {
                Icon(
                    imageVector = if (currentFeedback == "thumbsUp") Icons.Filled.ThumbUp else Icons.Outlined.ThumbUp,
                    contentDescription = stringResource(Res.string.cd_thumbs_up),
                    modifier = Modifier.size(18.dp),
                    tint = if (currentFeedback == "thumbsUp") {
                        MaterialTheme.colorScheme.primary
                    } else {
                        MaterialTheme.colorScheme.onSurfaceVariant
                    },
                )
            }
            IconButton(
                onClick = {
                    if (currentFeedback == "thumbsDown") onFeedback(null) else onShowFeedbackDialog()
                },
                modifier = Modifier.size(40.dp),
            ) {
                Icon(
                    imageVector = if (currentFeedback == "thumbsDown") Icons.Filled.ThumbDown else Icons.Outlined.ThumbDown,
                    contentDescription = stringResource(Res.string.cd_thumbs_down),
                    modifier = Modifier.size(18.dp),
                    tint = if (currentFeedback == "thumbsDown") {
                        MaterialTheme.colorScheme.error
                    } else {
                        MaterialTheme.colorScheme.onSurfaceVariant
                    },
                )
            }
        }

        if (onCopy != null) {
            IconButton(onClick = onCopy, modifier = Modifier.size(40.dp)) {
                Icon(
                    imageVector = Icons.Default.ContentCopy,
                    contentDescription = stringResource(Res.string.cd_copy_message),
                    modifier = Modifier.size(18.dp),
                    tint = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }

        if (onEdit != null) {
            IconButton(onClick = onEdit, modifier = Modifier.size(40.dp)) {
                Icon(
                    imageVector = Icons.Default.Edit,
                    contentDescription = stringResource(Res.string.cd_edit_message),
                    modifier = Modifier.size(18.dp),
                    tint = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }

        if (!isUser && onRegenerate != null) {
            IconButton(onClick = onRegenerate, modifier = Modifier.size(40.dp)) {
                Icon(
                    imageVector = Icons.Default.Refresh,
                    contentDescription = stringResource(Res.string.cd_regenerate_response),
                    modifier = Modifier.size(18.dp),
                    tint = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }

        if (onReadAloud != null) {
            IconButton(onClick = onReadAloud, modifier = Modifier.size(40.dp)) {
                Icon(
                    imageVector = if (isReading) Icons.Default.Stop else Icons.AutoMirrored.Filled.VolumeUp,
                    contentDescription = if (isReading) {
                        stringResource(Res.string.cd_stop_reading)
                    } else {
                        stringResource(Res.string.cd_read_aloud)
                    },
                    modifier = Modifier.size(18.dp),
                    tint = if (isReading) {
                        MaterialTheme.colorScheme.primary
                    } else {
                        MaterialTheme.colorScheme.onSurfaceVariant
                    },
                )
            }
        }

        if (onFork != null) {
            IconButton(onClick = onFork, modifier = Modifier.size(40.dp)) {
                Icon(
                    imageVector = Icons.AutoMirrored.Filled.CallSplit,
                    contentDescription = stringResource(Res.string.cd_fork_conversation),
                    modifier = Modifier.size(18.dp),
                    tint = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        }
    }
}

/**
 * Shared message content rendering and action buttons, used by both layout styles on both platforms.
 * Renders: edit mode OR (files + content parts + markdown fallback) + action row with sibling nav.
 */
@Suppress("LongParameterList")
@Composable
internal fun MessageContentAndActions(
    message: Message,
    isUser: Boolean,
    isEditing: Boolean,
    editText: String,
    onEditTextChanged: ((String) -> Unit)?,
    onEditSaveAndSubmit: (() -> Unit)?,
    onEditSaveOnly: (() -> Unit)?,
    onEditCancel: (() -> Unit)?,
    baseUrl: String,
    fontSizeMultiplier: Float,
    useKatex: Boolean,
    showImageDescriptions: Boolean,
    searchQuery: String?,
    isSearchMatch: Boolean,
    isCurrentSearchMatch: Boolean,
    searchFocusedOccurrence: Int,
    onFocusedOccurrencePositioned: ((LayoutCoordinates) -> Unit)?,
    showActions: Boolean,
    siblingIndex: Int,
    siblingCount: Int,
    onSiblingNavigation: ((Int) -> Unit)?,
    onEdit: (() -> Unit)?,
    onRegenerate: (() -> Unit)?,
    onCopy: (() -> Unit)?,
    onFeedback: ((String?) -> Unit)?,
    onContinue: (() -> Unit)?,
    onReadAloud: (() -> Unit)?,
    onFork: (() -> Unit)?,
    isReading: Boolean,
    currentFeedback: String?,
    onShowFeedbackDialog: () -> Unit,
) {
    if (isEditing && onEditTextChanged != null && onEditSaveAndSubmit != null && onEditSaveOnly != null && onEditCancel != null) {
        InlineEditInput(
            text = editText,
            onTextChanged = onEditTextChanged,
            onSaveAndSubmit = onEditSaveAndSubmit,
            onSaveOnly = onEditSaveOnly,
            onCancel = onEditCancel,
        )
    } else {
        // Render attached files above message text (matches web app behavior)
        val messageFiles = message.files
        if (!messageFiles.isNullOrEmpty()) {
            MessageFiles(
                files = messageFiles,
                baseUrl = baseUrl,
                modifier = Modifier.padding(bottom = 4.dp),
            )
        }

        val contentParts = message.content
        if (!contentParts.isNullOrEmpty()) {
            contentParts.forEach { part ->
                ContentPartRenderer(
                    part = part,
                    baseUrl = baseUrl,
                    fontSizeMultiplier = fontSizeMultiplier,
                    useKatex = useKatex,
                    attachments = message.attachments.orEmpty(),
                    showImageDescriptions = showImageDescriptions,
                    searchQuery = if (isSearchMatch) searchQuery else null,
                    searchFocusedOccurrence = if (isCurrentSearchMatch) searchFocusedOccurrence else -1,
                    onFocusedOccurrencePositioned = if (isCurrentSearchMatch) onFocusedOccurrencePositioned else null,
                    modifier = Modifier.padding(vertical = 2.dp),
                )
            }
        } else if (message.text.isNotBlank()) {
            MarkdownContent(
                text = message.text,
                fontSizeMultiplier = fontSizeMultiplier,
                useKatex = useKatex,
                searchQuery = if (isSearchMatch) searchQuery else null,
                searchFocusedOccurrence = if (isCurrentSearchMatch) searchFocusedOccurrence else -1,
                onFocusedOccurrencePositioned = if (isCurrentSearchMatch) onFocusedOccurrencePositioned else null,
            )
        }
    }

    // Action row: sibling nav + message actions (tap-to-reveal)
    val hasActions = siblingCount > 1 || onEdit != null || onRegenerate != null ||
        onCopy != null || onFeedback != null || onContinue != null ||
        onReadAloud != null || onFork != null
    if (hasActions) {
        AnimatedVisibility(
            visible = showActions,
            enter = fadeIn(),
            exit = fadeOut(),
        ) {
            Column {
                Spacer(modifier = Modifier.height(4.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween,
                ) {
                    // Sibling navigator (left side)
                    if (siblingCount > 1 && onSiblingNavigation != null) {
                        SiblingNavigator(
                            siblingIndex = siblingIndex,
                            siblingCount = siblingCount,
                            onNavigate = onSiblingNavigation,
                        )
                    } else {
                        Spacer(modifier = Modifier.width(0.dp))
                    }

                    // Action buttons (right side)
                    ActionButtons(
                        isUser = isUser,
                        onFeedback = onFeedback,
                        currentFeedback = currentFeedback,
                        onShowFeedbackDialog = onShowFeedbackDialog,
                        onCopy = onCopy,
                        onEdit = onEdit,
                        onRegenerate = onRegenerate,
                        onReadAloud = onReadAloud,
                        isReading = isReading,
                        onFork = onFork,
                    )
                }
            }
        }
    }
}
