package com.example.tomatoandpotatoapp.Screens

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


@Preview(showBackground = true)
@Composable
fun Registration() {

    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    val userEmail = remember { mutableStateOf("") }
    val userPassword = remember { mutableStateOf("") }

    val emailFlag = remember {
        mutableStateOf(false)
    }

    Box(modifier = Modifier.fillMaxSize()) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    brush = Brush.verticalGradient(
                        colors = listOf(
                            Color(0xFF666666).copy(alpha = 0.8f),
                            Color(0xFF000000).copy(alpha = 0.75f)
                        ),
                        startY = 0.0f,
                        endY = 1500.0f
                    )
                )
        )
        Column(modifier = Modifier.fillMaxSize()) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(75.dp),
                horizontalArrangement = Arrangement.Start
            ) {
                Button(
                    onClick = {
                        /*TODO*/
                    },
                    modifier = Modifier
                        .padding(start = 13.dp, top = 34.dp)
                        .width(32.dp)
                        .height(32.dp),
                    colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF78EE99)),
                    shape = RoundedCornerShape(10.dp),
                    contentPadding = PaddingValues(1.dp)
                ) {
                    Icon(
                        imageVector = Icons.Filled.KeyboardArrowLeft,
                        contentDescription = ""
                    )
                }

            }
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(vertical = 110.dp)
                    .pointerInput(Unit) {
                        detectTapGestures(onTap = {
                            keyboardController?.hide()
                            focusManager.clearFocus()
                        })
                    },
                verticalArrangement = Arrangement.SpaceBetween,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "Регистрация",
                    color = Color.White,
                    fontWeight = FontWeight.W700,
                    fontSize = 40.sp
                )

                Spacer(modifier = Modifier.height(150.dp))

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.Start
                ) {
                    if (emailFlag.value) {
                        Text(
                            text = "Введите логин: ",
                            color = MaterialTheme.colorScheme.error,
                            textAlign = TextAlign.Start,
                        )
                    }

                }
                TextField(
                    modifier = Modifier
                        .width(320.dp),
                    maxLines = 1,
                    singleLine = true,
                    shape = RoundedCornerShape(20.dp),
                    placeholder = {
                        Text(text = "Введите логин")
                    },
                    value = userEmail.value,

                    onValueChange = {
                        userEmail.value = it
                    }
                )

                TextField(
                    modifier = Modifier.width(320.dp),
                    shape = RoundedCornerShape(20.dp),
                    maxLines = 1,
                    singleLine = true,
                    placeholder = { Text(text = "Введите пароль") },
                    value = userPassword.value,
                    onValueChange = {
                        userPassword.value = it
                    }
                )

                Spacer(modifier = Modifier.height(15.dp))

                Button(
                    onClick = {
                        /*TODO*/
                    },
                    modifier = Modifier
                        .width(278.dp)
                        .height(45.dp),
                    shape = RoundedCornerShape(30),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(0xFF78EE99)
                    )
                )
                {
                    Text(text = "Регистрация",
                        fontWeight = FontWeight.W700,
                        fontSize = 20.sp,
                        color = Color.White
                    )
                }
            }
        }
    }


}