package com.example.what_the_fantasy.ui.screens

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
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.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.what_the_fantasy.logs.LogsUsers
import com.example.what_the_fantasy.R
import com.example.what_the_fantasy.data.model.User
import com.example.what_the_fantasy.data.services.IServices
import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent
import com.example.what_the_fantasy.ui.components.SpaceHeightComponent
import com.example.what_the_fantasy.ui.components.TitlePageComponent
import com.example.what_the_fantasy.data.services.hashPassword
import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent
import com.example.what_the_fantasy.ui.states.AuthUserState
import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel

@Composable
fun LoginPage(navControllerSignUp: () -> Unit,
              navControllerProfil: (Int) -> Unit,
              authUserVM : AuthUserViewModel,
              authState : AuthUserState,
              initialierCurrentUser : (Int) -> Unit) {


    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background),
        contentAlignment = Alignment.Center
    ){
        Column(
            modifier = Modifier
                .fillMaxWidth(0.9f)
                .padding(20.dp)
                .clip(RoundedCornerShape(16.dp))
                .background(MaterialTheme.colorScheme.onPrimary)
                .padding(20.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            TitlePageComponent(R.string.titleLogin, MaterialTheme.colorScheme.primary)
            SpaceHeightComponent(20)

            ConnexionButtonLogin(authUserVM,
                IdentifiantTextField(R.string.IdentifiantLogin, authState.username){
                authUserVM.setUsername(it)
            }, PassWdTextField(R.string.PasswdLogin, authState.password){
                authUserVM.setPassword(it)
            }, R.string.ButtonLogin,18,navControllerProfil){
                initialierCurrentUser(it)
            }
            SpaceHeightComponent(16)
            CreateAccountButton(R.string.ButtonCreateLogin,12, MaterialTheme.colorScheme.primary, navControllerSignUp)
        }
    }
}


@Composable
fun IdentifiantTextField(textIdentifiantResId : Int, username : String, onValueChange: (String) -> Unit ) : String{
    val textIdentifiant = stringResource(id = textIdentifiantResId)

    Column(modifier = Modifier.padding(top = 16.dp)) {
        OutlinedTextField(
            value = username,
            onValueChange = onValueChange,
            label = { Text(textIdentifiant) },
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 8.dp),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
            maxLines = 1,
            shape = RoundedCornerShape(16.dp)
        )
    }
    return username;
}

@Composable
fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (String) -> Unit) : String{
    val textpasswd = stringResource(id = textpasswdResId)
    var passwordVisible by remember { mutableStateOf(false) }

    Column(modifier = Modifier.padding(top = 10.dp, bottom = 30.dp)) {
        OutlinedTextField(
            value = password,
            onValueChange = onValueChange,
            label = { Text(textpasswd) },
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 8.dp),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
            maxLines = 1,
            visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
            trailingIcon = {
                IconButton(onClick = { passwordVisible = !passwordVisible }) {
                    VisibleIconPasswordComponent(passwordVisible)
                }
            },
            shape = RoundedCornerShape(16.dp)
        )
    }
    return password;
}


@Composable
fun ConnexionButtonLogin(authUserVM : AuthUserViewModel, username : String, passwd : String, titleResId : Int, size : Int, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit){
    val title = stringResource(id = titleResId)
    var showError by remember { mutableStateOf(false) }
    Button(
        onClick = { showError = !authUserVM.validLogin(username, passwd, navController, initialierCurrentUser)
                  },
        colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.background),
        modifier = Modifier
            .fillMaxWidth(),
    ) {
        Text(title, fontSize = size.sp, color = MaterialTheme.colorScheme.onBackground)
    }

    if(showError){
        ErrorMessageProfileComponent(R.string.ErrorLogin)
    }
}



@Composable
fun CreateAccountButton(titleResId : Int, size : Int, color : Color, navController: () -> Unit) {
    val title = stringResource(id = titleResId)
    Text(
        text = title,
        fontSize = size.sp,
        color = MaterialTheme.colorScheme.onBackground,
        modifier = Modifier.clickable {
            navController()
        }
    )
}