package com.example.pmulabs.designElem.elem

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EmailTextField(email: String, onEmailChange: (String) -> Unit) {
    TextField(
        value = email,
        onValueChange = {
            onEmailChange(it)
        },
        colors = TextFieldDefaults.textFieldColors(
            containerColor = Color.White
        ),
        modifier = Modifier
            .requiredWidth(width = 269.dp)
            .requiredHeight(height = 53.dp)
            .shadow(shape = RoundedCornerShape(40.dp), elevation = 5.dp)
            .clip(shape = RoundedCornerShape(40.dp)),
        label = { Text("Email") },
        placeholder = { Text("Email") },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
        singleLine = true,
    )
}

fun isValidEmail(email: String): Boolean {
    val emailRegex = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+".toRegex()
    return email.matches(emailRegex)
}

@Composable
fun ValidateEmail(email: String, onEmailChange: (String) -> Unit) {

    Column (modifier = Modifier.padding(16.dp)) {
        EmailTextField(email = email, onEmailChange = { onEmailChange(it)})

        if (email.isNotEmpty()) {
            if (isValidEmail(email)) {
                Text(text = "Email is valid", color = Color(0xFF5D925E))
            } else {
                Text(text = "Email is not valid", color = Color(0xFF94474D))
            }
        }
    }
}