package net.buzzert.kordophonedroid.ui.messagelist

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.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.ElevatedButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import net.buzzert.kordophonedroid.R

data class AttachmentRowItem(
    val painter: Painter,
    val id: String,
)

@Composable
fun AttachmentRow(
    attachmentItems: List<AttachmentRowItem>,
    onClear: () -> Unit,
) {
    Divider()

    Row(
        modifier = Modifier
            .height(120.dp)
            .fillMaxWidth()
            .background(MaterialTheme.colors.onSurface.copy(0.08f))
            .padding(8.dp)
    ) {
        LazyRow {
            attachmentItems.forEach { attachmentItem ->
                item {
                    Image(
                        painter = attachmentItem.painter,
                        contentDescription = "attachment",
                        contentScale = ContentScale.Crop,
                        modifier = Modifier
                            .aspectRatio(1.0f)
                            .clip(RoundedCornerShape(4.dp))
                    )

                    Spacer(Modifier.width(4.dp))
                }
            }
        }

        Spacer(Modifier.weight(1f))

        ElevatedButton(
            onClick = onClear,
            colors = ButtonDefaults.elevatedButtonColors(
                containerColor = MaterialTheme.colors.background
            ),
            modifier = Modifier.align(Alignment.CenterVertically)
        ) {
            Text("Remove")
        }
    }
}

@Composable
fun MessageEntry(
    textFieldValue: TextFieldValue,
    attachmentItems: List<AttachmentRowItem> = listOf(),

    onAddAttachment: () -> Unit = {},
    onClearAttachments: () -> Unit = {},
    onTextChanged: (TextFieldValue) -> Unit,
    onSend: () -> Unit,
) {
    Column {
        if (attachmentItems.isNotEmpty()) {
            AttachmentRow(attachmentItems, onClear = onClearAttachments)
        }

        Row(
            modifier = Modifier
                .background(MaterialTheme.colors.onSurface.copy(alpha = 0.18f))
                .fillMaxWidth()
                .padding(vertical = 8.dp, horizontal = 4.dp)
                .imePadding()
                .navigationBarsPadding()
        ) {
            IconButton(
                onClick = onAddAttachment,
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.attach_file),
                    contentDescription = "Attach File"
                )
            }

            Spacer(Modifier.width(8.dp))

            Surface(
                shape = MaterialTheme.shapes.medium,
                modifier = Modifier
                    .weight(1f)
                    .align(Alignment.CenterVertically)
                    .shadow(3.dp)
            ) {
                BasicTextField(
                    value = textFieldValue,
                    onValueChange = { onTextChanged(it) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 8.dp, vertical = 8.dp),
                    cursorBrush = SolidColor(MaterialTheme.colors.onBackground),
                    textStyle = MaterialTheme.typography.body1.copy(MaterialTheme.colors.onBackground),
                    decorationBox = { textContent ->
                        if (textFieldValue.text.isEmpty()) {
                            Text(
                                text = "Message",
                                style = MaterialTheme.typography.body1.copy(
                                    color = MaterialTheme.colors.onSurface.copy(alpha = 0.4f)
                                )
                            )
                        }

                        textContent()
                    }
                )
            }

            Spacer(Modifier.width(8.dp))

            Button(
                onClick = onSend,
                enabled = (attachmentItems.isNotEmpty() || textFieldValue.text.isNotEmpty())
            ) {
                Text(text = "Send")
            }

            Spacer(Modifier.width(8.dp))
        }
    }
}
