@file:Suppress("HardCodedStringLiteral")
package eu.gaudian.translator.view.translation

import androidx.compose.foundation.horizontalScroll
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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.AppIcons

@Composable
fun InputCard(
    modifier: Modifier = Modifier,
    text: String,
    isTranslating: Boolean,
    onTextChange: (String) -> Unit,
    onPasteClick: () -> Unit,
    onClearClick: () -> Unit,
    onTranslateClick: () -> Unit,
    onHistoryClick: () -> Unit,
    showBackButton: Boolean,
    showForwardButton: Boolean,
    onBackClick: () -> Unit,
    onForwardClick: () -> Unit
) {
    val showClearButton = text.isNotEmpty()

    AppCard(
        modifier = modifier
            .fillMaxWidth().padding(0.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .verticalScroll(rememberScrollState())
        ) {
            OutlinedTextField(
                value = text,
                onValueChange = onTextChange,
                textStyle = MaterialTheme.typography.titleLarge.copy(color = MaterialTheme.colorScheme.onSurfaceVariant),
                placeholder = {
                    Text(
                        stringResource(R.string.text_enter_text_to_translate),
                        style = MaterialTheme.typography.titleLarge,
                    )
                },
                modifier = Modifier
                    .weight(1f)
                    .fillMaxWidth().padding(top = 0.dp),
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color.Transparent, unfocusedContainerColor = Color.Transparent,
                    disabledContainerColor = Color.Transparent,
                    cursorColor = MaterialTheme.colorScheme.primary,
                    focusedIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    disabledIndicatorColor = Color.Transparent
                ),
            )
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(0.dp),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ) {

                Row(
                    modifier = Modifier
                        .weight(1f)
                        .horizontalScroll(rememberScrollState()),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Start
                ) {
                    ActionBar(
                        contentHorizontalArrangement = Arrangement.Start,
                        modifier = Modifier.padding(0.dp)
                    ) {
                        if (showClearButton) {
                            IconButton(onClick = onClearClick) {
                                Icon(
                                    imageVector = AppIcons.Clear,
                                    contentDescription = stringResource(R.string.cd_clear_text),
                                    tint = MaterialTheme.colorScheme.primary
                                )
                            }
                        }
                        if (!showClearButton) {
                            IconButton(onClick = onPasteClick) {
                                Icon(
                                    imageVector = AppIcons.Paste,
                                    contentDescription = stringResource(R.string.cd_paste),
                                    tint = MaterialTheme.colorScheme.primary
                                )
                            }
                        }
                    }

                    Spacer(modifier = Modifier.width(4.dp))

                    ExpandableActionBar(modifier = Modifier.padding(0.dp)) {
                        IconButton(onClick = onHistoryClick) {
                            Icon(
                                imageVector = AppIcons.History,
                                contentDescription = stringResource(R.string.cd_translation_history),
                                tint = MaterialTheme.colorScheme.primary
                            )
                        }
                        IconButton(onClick = onBackClick, enabled = showBackButton) {
                            Icon(
                                imageVector = AppIcons.Undo,
                                contentDescription = "Back",
                                tint = if (showBackButton) MaterialTheme.colorScheme.primary else Color.Gray
                            )
                        }
                        IconButton(onClick = onForwardClick, enabled = showForwardButton) {
                            Icon(
                                imageVector = AppIcons.Redo,
                                contentDescription = "Forward",
                                tint = if (showForwardButton) MaterialTheme.colorScheme.primary else Color.Gray
                            )
                        }
                    }
                }

                // SAFETY SPACER: Ensures the menu never touches the FAB
                Spacer(Modifier.width(8.dp))

                // Primary Translate FAB stays prominent and anchored
                FloatingActionButton(
                    onClick = { if (!isTranslating) onTranslateClick() },
                    modifier = Modifier.size(56.dp),
                    shape = RoundedCornerShape(16.dp),
                    containerColor = MaterialTheme.colorScheme.primary,
                    contentColor = MaterialTheme.colorScheme.onPrimary
                ) {
                    if (isTranslating) {
                        CircularProgressIndicator(
                            modifier = Modifier.size(24.dp),
                            color = MaterialTheme.colorScheme.onPrimary,
                            strokeWidth = 2.dp
                        )
                    } else {
                        Icon(
                            AppIcons.Translate,
                            contentDescription = stringResource(R.string.label_translate)
                        )
                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun InputCardPreview() {
    var text by remember { mutableStateOf("") }
    InputCard(
        text = text,
        isTranslating = false,
        onTextChange = { },
        onPasteClick = { },
        onClearClick = { },
        onTranslateClick = { },
        onHistoryClick = { },
        showBackButton = true,
        showForwardButton = false,
        onBackClick = { },
        onForwardClick = { }
    )
}