package com.example.myapplication.components

import ButtonNice
import Input
import TextNice
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.components.common.myInput
import com.example.myapplication.database.entities.User
import com.example.myapplication.viewModels.UserViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import myColor1
import myColor2
import myColor3

@Composable
fun Registration(
    navController: NavController,
    userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)
) {

    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current

    val mailState = remember { mutableStateOf("") }
    val loginState = remember { mutableStateOf("") }
    val passState = remember { mutableStateOf("") }
    val repeatPassState = remember { mutableStateOf("") }

    Column(modifier = Modifier
        .fillMaxSize()
        .padding(start = 10.dp, end = 10.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        TextNice(text = "Регистрация")
        myInput("Email", mailState)
        myInput("Логин", loginState)
        myInput("Пароль", passState)
        myInput("Повторите Пароль", repeatPassState)

        ButtonNice(text = "Регистрация", color = myColor1){
            coroutineScope.launch {
                if(passState.value != repeatPassState.value) {
                    val toast = Toast.makeText(context, "Пароли не совпадают", Toast.LENGTH_SHORT)
                    toast.show()
                    delay(500)
                    toast.cancel()
                    return@launch
                }

                userViewModel.insert(User(null, mailState.value, loginState.value, passState.value))
                navController.navigate("authorization")
            }
        }
        ButtonNice(text = "Авторизация", color = myColor2, onClickAction = {navController.navigate("authorization")})
    }
}