package com.example.dtf.screens

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.*
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.dtf.PreferencesManager
import com.example.dtf.utils.ScreenPaths
import com.example.dtf.viewmodels.LoginViewModel
import com.example.dtf.widgets.MyTextField

@Composable
fun LoginScreen(navController: NavHostController) {
    val sharedPref = PreferencesManager(LocalContext.current)

    val login = remember { mutableStateOf(TextFieldValue(""))}
    val password = remember { mutableStateOf(TextFieldValue(""))}

    val viewModel = hiltViewModel<LoginViewModel>()

    val success = viewModel.successState.observeAsState().value

    if (success == true) {
        navController.navigate(ScreenPaths.Posts.route) {
            popUpTo(ScreenPaths.Posts.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.3f)
            )
            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=60.dp)
                    .fillMaxWidth()
                    .heightIn(max=55.dp)
            )
            Button(
                {
                    viewModel.login(sharedPref, login.value.text, password.value.text)
                },
                colors = ButtonDefaults.buttonColors(Color(0xFF388DF2)),
                border = BorderStroke(1.dp, Color(0xFF0085FF)),
                shape = RoundedCornerShape(15.dp),
                modifier = Modifier.padding(bottom=20.dp).fillMaxWidth(0.5f)
            ) {
                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.Register.route) {
                        popUpTo(ScreenPaths.Register.route) {
                            inclusive = true
                        }
                    }
                }
            )
        }
    }
}