package com.aiosman.ravenow.ui.composables.form

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
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.foundation.layout.widthIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.modifiers.noRippleClickable

/**
 * 水平布局的输入框
 */
@Composable
fun FormTextInput(
    modifier: Modifier = Modifier,
    value: String,
    label: String? = null,
    error: String? = null,
    hint: String? = null,
    background: Color? = null,
    onValueChange: (String) -> Unit
) {
    val AppColors = LocalAppTheme.current
    val focusRequester = remember { FocusRequester() }
    val keyboardController = LocalSoftwareKeyboardController.current

    Column(
        modifier = modifier
    ) {
        Row(
            modifier = Modifier.fillMaxWidth()
                .clip(RoundedCornerShape(25.dp))
                .background(background ?: AppColors.inputBackground)
                .let {
                    if (error != null) {
                        it.border(1.dp, AppColors.error, RoundedCornerShape(24.dp))
                    } else if (background != null && background == Color.White) {
                        // 如果传入白色背景，添加灰色边框
                        it.border(
                            1.dp,
                            Color(red = 124f / 255f, green = 116f / 255f, blue = 128f / 255f, alpha = 0.08f),
                            RoundedCornerShape(25.dp)
                        )
                    } else {
                        it
                    }
                }
                .padding(17.dp)
                .noRippleClickable {
                    focusRequester.requestFocus()
                    keyboardController?.show()
                },
            verticalAlignment = Alignment.CenterVertically
        ) {
            label?.let {
                Text(
                    text = it,
                    modifier = Modifier
                        .widthIn(100.dp),
                    style = TextStyle(
                        fontSize = 16.sp,
                        fontWeight = FontWeight.W600,
                        color = AppColors.text
                    )
                )
            }
            Box(
                modifier = Modifier
                    .weight(1f)
            ) {
                Icon(
                    painter = painterResource(id = R.mipmap.icons_infor_edit),
                    contentDescription = null,
                    modifier = Modifier
                        .size(16.dp)
                        .align(Alignment.TopStart),
                    tint = AppColors.text.copy(alpha = 0.4f)
                )
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Spacer(modifier = Modifier.width(24.dp))
                    Box(
                        modifier = Modifier.weight(1f)
                    ) {
                        if (value.isEmpty()) {
                            Text(
                                text = hint ?: "",
                                style = TextStyle(
                                    fontSize = 14.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = AppColors.inputHint
                                )
                            )
                        }

                        BasicTextField(
                            maxLines = 1,
                            value = value,
                            onValueChange = {
                                onValueChange(it)
                            },
                            modifier = Modifier
                                .focusRequester(focusRequester),
                            singleLine = true,
                            textStyle = TextStyle(
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Normal,
                                color = AppColors.text
                            ),
                            cursorBrush = SolidColor(AppColors.text),
                        )
                    }
                }
            }



        }
        Spacer(modifier = Modifier.height(4.dp))
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            AnimatedVisibility(
                visible = error != null,
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Image(
                        painter = painterResource(id = R.mipmap.rider_pro_input_error),
                        contentDescription = "Error",
                        modifier = Modifier.size(8.dp)
                    )
                    Spacer(modifier = Modifier.size(4.dp))
                    AnimatedContent(targetState = error) { targetError ->
                        Text(targetError ?: "", color = AppColors.error, fontSize = 12.sp)
                    }
                }
            }
        }
    }

}
