package com.example.dtf.screens

import android.graphics.drawable.shapes.Shape
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.example.dtf.utils.ScreenPaths
import com.example.dtf.viewmodels.RegisterViewModel
import com.example.dtf.widgets.MyTextField

@Composable
fun RegisterScreen(navController: NavHostController) {
    val login = remember { mutableStateOf(TextFieldValue(""))}
    val password = remember { mutableStateOf(TextFieldValue(""))}
    val confirmPassword = remember { mutableStateOf(TextFieldValue(""))}

    val viewModel = hiltViewModel<RegisterViewModel>()
    val success = viewModel.successState.observeAsState().value

    if (success == true) {
        navController.navigate(ScreenPaths.Login.route) {
            popUpTo(ScreenPaths.Login.route) {
                inclusive = true
            }
        }
    }

    if (success == false) {
        AlertDialog(
            title = {
                Text(
                    text = "Ошибка регистрации",
                    fontWeight = FontWeight.Bold,
                    fontSize = 20.sp
                )
            },
            text = {
                Text(
                    text = "Проверьте корректность введенных данных",
                    fontWeight = FontWeight.Normal,
                    fontSize = 16.sp
                )
            },
            onDismissRequest = {
                viewModel.calmSuccessState()
            },
            buttons = {
                TextButton(
                    onClick = {
                        viewModel.calmSuccessState()
                    }
                ) {
                    Text("ОК")
                }
            }
        )
    }

    Row(
        horizontalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxSize()
    ) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth(0.7f).fillMaxHeight().padding(vertical=100.dp)
        ) {
            Text(
                text = "Регистрация",
                style = TextStyle(
                    fontSize = 40.sp,
                    fontWeight = FontWeight(400),
                    color = Color(0xFF000000),
                ),
                modifier = Modifier.fillMaxHeight(0.2f)
            )
            MyTextField(
                login,
                "Логин",
                {login.value = it},
                modifier = Modifier
                    .padding(bottom=30.dp)
                    .fillMaxWidth()
                    .heightIn(max=55.dp)
            )
            MyTextField(
                password,
                "Пароль",
                {password.value = it},
                modifier = Modifier
                    .padding(bottom=30.dp)
                    .fillMaxWidth()
                    .heightIn(max=55.dp)
            )
            MyTextField(
                confirmPassword,
                "Пароль повторно",
                {confirmPassword.value = it},
                modifier = Modifier
                    .padding(bottom=60.dp)
                    .fillMaxWidth()
                    .heightIn(max=55.dp)
            )
            Button(
                {
                    viewModel.register(login.value.text, password.value.text, confirmPassword.value.text)
                },
                colors = ButtonDefaults.buttonColors(Color(0xFF388DF2)),
                border = BorderStroke(1.dp, Color(0xFF0085FF)),
                shape = RoundedCornerShape(15.dp),
                modifier = Modifier.padding(bottom=20.dp).fillMaxWidth()
            ) {
                Text(
                    text = "Зарегистрироваться",
                    style = TextStyle(
                        fontSize = 24.sp,
                        fontWeight = FontWeight(400),
                        color = Color(0xFFFFFFFF),
                    )
                )
            }
            Text(
                text = "Уже есть аккаунт?",
                style = TextStyle(
                    fontSize = 16.sp,
                    fontWeight = FontWeight(400),
                    color = Color(0xFFB6B3B3),
                ),
                modifier = Modifier.clickable {
                    navController.navigate(ScreenPaths.Login.route)
                }
            )
        }
    }
}