package logacces;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LogAcces extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LogAcces();
        });
    }

    public LogAcces() {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Procedura di Loggin");
        getContentPane().setLayout(null);
        //getContentPane().setBackground(Color.WHITE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setVisible(true);

        JLabel lblTit = new JLabel();
        lblTit.setFont(new Font("Arial", Font.BOLD, 20));
        lblTit.setText("Inserisci le Tue Credenziali");
        lblTit.setBounds(30, 5, 300, 30);

        JLabel lblUsr = new JLabel();
        lblUsr.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUsr.setFont(new Font("Arial", Font.PLAIN, 16));
        lblUsr.setText("Inserisci Username:");
        lblUsr.setBounds(30, 48, 160, 30);

        JLabel lblPsw = new JLabel();
        lblPsw.setHorizontalAlignment(SwingConstants.RIGHT);
        lblPsw.setFont(new Font("Arial", Font.PLAIN, 16));
        lblPsw.setText("Inserisci Password:");
        lblPsw.setBounds(30, 78, 160, 30);

        JLabel lblOut = new JLabel();
        lblOut.setHorizontalAlignment(SwingConstants.CENTER);
        lblOut.setFont(new Font("Arial", Font.BOLD, 14));
        lblOut.setBounds(80, 180, 200, 40);

        JTextField jtxUsr = new JTextField("me@cersil.it");
        jtxUsr.setFont(new Font("Arial", Font.PLAIN, 14));        
        jtxUsr.setBounds(195, 50, 150, 30);

        JPasswordField jtxPsw = new JPasswordField("student");
        jtxPsw.setEchoChar('\u2022');
        jtxPsw.setFont(new Font("Arial", Font.PLAIN, 14));
        jtxPsw.setBounds(195, 80, 150, 30);

        JButton button = new JButton("Accedi");
        button.setBounds(265, 130, 80, 30);

        JCheckBox chkVedi = new JCheckBox("Vedi");
        chkVedi.setFont(new Font("Arial", Font.PLAIN, 14));
        chkVedi.setBounds(210, 125, 150, 30);
        chkVedi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Se il checkbox è selezionato, mostra la password
                if (chkVedi.isSelected()) {
                    jtxPsw.setEchoChar((char) 0); // Mostra la password
                } else {
                    jtxPsw.setEchoChar('\u2022'); // Nascondi la password con i puntini
                }
            }
        });

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Verifica delle credenziali
                String username = jtxUsr.getText();
                String password = new String(jtxPsw.getPassword());
                if (username.equals("me@cersil.it") && password.equals("student")) {
                    lblOut.setText("Accesso consentito");
                    lblOut.setForeground(Color.black);
                } else {
                    lblOut.setText("Accesso negato");
                    lblOut.setForeground(Color.RED);
                }
            }
        });

        add(lblTit);
        add(lblUsr);
        add(lblPsw);
        add(lblOut);
        add(jtxUsr);
        add(jtxPsw);
        add(button);
        add(chkVedi);
    }
}
