package com.example.libary

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.AbsoluteRoundedCornerShape
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.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.libary.theme.AccentColor
import com.example.libary.theme.BlackColor
import com.example.libary.theme.CaptionColor
import com.example.libary.theme.Error1Color
import com.example.libary.theme.InputBGColor
import com.example.libary.theme.InputStroke

/**
 * текстовое поле
 * modifier - модификаторы
 * value - значение
 * */
@Composable
fun Input(
    modifier: Modifier = Modifier,
    modifierColumn: Modifier = Modifier,
    value: String = "",
    placeholder: String = "",
    topText: String = "",
    errorText: String = "",
    pasMode: Boolean = false,
    onValueChange: (String) -> Unit,
) {
    var vis by remember { mutableStateOf(false) }

    Column(modifierColumn) {
        if (topText.isNotEmpty()) {
            Text(
                text = topText,
                color = Color(0xFF7E7E9A),
                fontWeight = FontWeight.W400,
                fontSize = 14.sp
            )

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

        OutlinedTextField(
            value = value,
            modifier = modifier,
            onValueChange = onValueChange,
            placeholder = {
                Text(
                    text = placeholder,
                    color = CaptionColor,
                    fontWeight = FontWeight.W400,
                    fontSize = 16.sp
                )
            },
            trailingIcon = {
                if (pasMode) {
                    if (vis) {
                        Image(
                            painter = painterResource(R.drawable.gl_2),
                            contentDescription = null,
                            modifier = Modifier.clickable {
                                vis = false
                            }
                        )
                    }else {
                        Image(
                            painter = painterResource(R.drawable.gl_1),
                            contentDescription = null,
                            modifier = Modifier.clickable {
                                vis = true
                            }
                        )
                    }
                }
            },
            shape = AbsoluteRoundedCornerShape(10.dp),
            colors = TextFieldDefaults.colors(
                cursorColor = AccentColor,
                focusedPlaceholderColor = CaptionColor,
                unfocusedPlaceholderColor = CaptionColor,

                focusedTextColor = BlackColor,
                unfocusedTextColor = BlackColor,

                focusedIndicatorColor = if (errorText.isNotEmpty()) Error1Color else AccentColor,
                unfocusedIndicatorColor = if (errorText.isNotEmpty()) Error1Color else InputStroke,

                focusedContainerColor = if (errorText.isNotEmpty()) Error1Color.copy(0.1f) else InputBGColor,
                unfocusedContainerColor = if (errorText.isNotEmpty()) Error1Color.copy(0.1f) else InputBGColor
            ),
            visualTransformation = if (!vis && pasMode) PasswordVisualTransformation('*') else VisualTransformation.None
        )

        if (errorText.isNotEmpty()) {
            Spacer(Modifier.height(8.dp))

            Text(
                text = errorText,
                color = Error1Color,
                fontSize = 14.sp,
                fontWeight = FontWeight.W400
            )
        }
    }
}

@Preview
@Composable
private fun InputPrev() {
    var value by remember { mutableStateOf("") }

    Input(
        value = value,
        placeholder = "Введите имя",
        topText = "Вве имя",
        pasMode = true,
        onValueChange = {
            value = it
        }
    )
}