package com.example.mystictale.resources.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Icon
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
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.example.mystictale.R
import com.example.mystictale.ui.theme.DarkPurple
import com.example.mystictale.ui.theme.Grey
import com.example.mystictale.ui.theme.OpenSans

@Composable
fun PasswordTextField(
    value: String,
    onValueChange: (String) -> Unit,
    placeholder: String,
    flag: Boolean
) {
    val checked = remember { mutableStateOf(false) }
    TextField(
        value = value,
        onValueChange = onValueChange,
        Modifier
            .fillMaxWidth(),
        trailingIcon = {
            IconToggleButton(
                checked = checked.value,
                onCheckedChange = { checked.value = it }) {
                Icon(
                    if (checked.value) {
                        painterResource(id = R.drawable.eye)
                    } else {
                        painterResource(id = R.drawable.close_eye)
                    },
                    contentDescription = "Скрыть пароль",
                    tint = Color(0XFF939396),
                    modifier = Modifier.width(20.dp)
                )
            }
        },
        isError = flag,
        keyboardOptions = KeyboardOptions(
            keyboardType = KeyboardType.Password
        ),
        visualTransformation = if (checked.value) VisualTransformation.None else PasswordVisualTransformation(),
        colors = TextFieldDefaults.colors(
            unfocusedTextColor = Color.White,
            unfocusedPlaceholderColor = Grey,
            unfocusedContainerColor = Color.Transparent,
            focusedTextColor = Color.White,
            focusedContainerColor = Color.Transparent,
            focusedPlaceholderColor = Grey ,
            focusedIndicatorColor = DarkPurple,
            unfocusedIndicatorColor = DarkPurple,
            errorContainerColor = Color.Transparent,
            errorTextColor = Color.White,
            errorPlaceholderColor = Grey,
        ),
        placeholder = { Text(text = placeholder) },
        maxLines = 1,
        textStyle = TextStyle(
            fontFamily = OpenSans,
            fontWeight = FontWeight.Light,
            fontSize = 15.sp
        )
    )
}