package com.example.login2

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.login2.ui.theme.Login2Theme

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.res.painterResource

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            Login2Theme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {

    Column() {
        Text(
            text = "Hello $name!",
            modifier = modifier
        )
        var isLoggedIn by remember { mutableStateOf(false) }

        if (isLoggedIn){
            gallery()
        } else {
            LoginScreen(onLoginSuccess = { isLoggedIn = true })
        }
    }
}


// TODO
@Composable
fun gallery() {
    Column() {
        Text("Welcome! You are logged in.")
        Image(
            painter = painterResource(R.drawable._0220426_071357),
            contentDescription = "kwiatek",
            modifier = Modifier
                .width(100.dp)
                .padding(5.dp)
        )
    }
}


@Composable
fun LoginScreen(onLoginSuccess: () -> Unit) {
    var username by remember { mutableStateOf("") }      // login
    var password by remember { mutableStateOf("") }      // hasło
    var errorMessage by remember { mutableStateOf("") }  // komunikat błędu

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "Login", style = MaterialTheme.typography.headlineMedium)

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

        OutlinedTextField(
            value = username,
            onValueChange = { username = it },
            label = { Text("Username") },
            modifier = Modifier.fillMaxWidth(),
            keyboardOptions = KeyboardOptions.Default.copy(
                imeAction = ImeAction.Next
            )
        )

        Spacer(modifier = Modifier.height(8.dp))  // pusta przestrzeń

        OutlinedTextField(
            value = password,
            onValueChange = { password = it },  // po wpisaniu znaków pole sie nimi uzupełni
            label = { Text("Password") },
            modifier = Modifier.fillMaxWidth(),
            visualTransformation = PasswordVisualTransformation(),  // chowanie znaków hasła
            keyboardOptions = KeyboardOptions.Default.copy(  // po enterze focus przechodzi do następnego elementu formularze
                imeAction = ImeAction.Done
            )
        )

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

        Button(
            onClick = {
                errorMessage = "" // Resetujemy komunikat o błędzie

                // Symulacja logowania
                if (username.isBlank() || password.isBlank()) {
                    errorMessage = "Wszystkie pola są wymagane"
                } else {
                    // Logika autoryzacji - przykładowa
                    if (username == "aaa" && password == "bbb") {
                        // Jeśli login się powiódł:
                        onLoginSuccess()
                    } else {
                        errorMessage = "Nieprawidłowy login lub hasło"
                    }
                }
            },
            modifier = Modifier.fillMaxWidth()
        ){
            Text("Login")
        }

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

        // tekst błedu logowania
        if (errorMessage.isNotBlank()) {
            Text(text = errorMessage, color = MaterialTheme.colorScheme.error)
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Login2Theme {
        Greeting("Android")
    }
}

source https://deepai.org/chat