package com.aiosman.riderpro.ui.composables

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.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.size
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
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.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.riderpro.R
import com.aiosman.riderpro.ui.modifiers.noRippleClickable

@Composable
fun TextInputField(
    modifier: Modifier = Modifier,
    text: String,
    onValueChange: (String) -> Unit,
    password: Boolean = false,
    label: String? = null,
    hint: String? = null,
    error: String? = null,
    enabled: Boolean = true
) {
    var showPassword by remember { mutableStateOf(!password) }
    var isFocused by remember { mutableStateOf(false) }
    Column(modifier = modifier) {
        label?.let {
            Text(it, color = Color(0xFFCCCCCC))
            Spacer(modifier = Modifier.height(16.dp))
        }
        Box(
            contentAlignment = Alignment.CenterStart
        ) {
            Row {
                BasicTextField(
                    value = text,
                    onValueChange = onValueChange,
                    modifier = Modifier
                        .weight(1f)
                        .onFocusChanged { focusState ->
                            isFocused = focusState.isFocused
                        },
                    textStyle = TextStyle(
                        fontSize = 16.sp,
                        fontWeight = FontWeight.W500,
                        color = Color(0xff333333)
                    ),
                    keyboardOptions = KeyboardOptions(
                        keyboardType = if (password) KeyboardType.Password else KeyboardType.Text
                    ),
                    visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
                    singleLine = true,
                    enabled = enabled
                )
                if (password) {
                    Image(
                        painter = painterResource(id = R.drawable.rider_pro_eye),
                        contentDescription = "Password",
                        modifier = Modifier
                            .size(24.dp)
                            .noRippleClickable {
                                showPassword = !showPassword
                            },
                        colorFilter = ColorFilter.tint(Color.Black)
                    )
                }
            }

            if (text.isEmpty()) {
                hint?.let {
                    Text(it, color = Color(0xFFCCCCCC), fontWeight = FontWeight.W600)
                }
            }
        }
        Spacer(modifier = Modifier.height(16.dp))
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(1.dp)
                .background(if (isFocused) Color(0xff333333) else Color(0xFFF5F5F5))

        )
        Spacer(modifier = Modifier.height(8.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 = Color(0xFFE53935), fontSize = 12.sp)
                    }
                }
            }
        }
    }
}