package com.msksbr.LoginFrm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.msksbr.MainFrm.MainFrm;
import com.msksbr.SQL.Connector;
import com.msksbr.images.EasterEgg;

public class LoginFrm extends JFrame {
    private final Connector connector;
    private final LoginMessangeExpection loginMessangeExpection;
    private JPanel panel1;
    private JPanel titlePanel;
    private JLabel titleLabel;
    private JPanel bodyPanel;
    private JPanel bottomPanel;
    private JButton commitButton;
    private JPanel labelPanel;
    private JPanel fieldPanel;
    private JPanel uLabelPanel;
    private JPanel pLabelPanel;
    private JLabel uLabel;
    private JLabel pLabel;
    private JTextField uField;
    private JPasswordField pField;
    private String username;
    private String password;

    public LoginFrm() {
        loginMessangeExpection = new LoginMessangeExpection();
        setIconImage(new ImageIcon("src/com/msksbr/images/mainicon.png").getImage());
        try {
            connector = new Connector();
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }

        setTitle("登录到图书管理系统");
        setContentPane(panel1);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        setSize(400, 250);
        setResizable(false);

        Dimension fieldDimension = new Dimension(250, 30);
        this.uField.setPreferredSize(fieldDimension);
        this.pField.setPreferredSize(fieldDimension);

        this.pField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    loginMessangeEntered();
                }
            }
        });

        this.commitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.commitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loginMessangeEntered();
            }
        });

        setLocation();
        setDialogLocation();
    }

    private void setLocation() {
        ScreenSize screenSize = new ScreenSize();
        setLocation(screenSize.width / 2 - getWidth() / 2, screenSize.height / 2 - getHeight() / 2);
    }

    private void setDialogLocation() {
        loginMessangeExpection.setLocation(getX() + getWidth() / 4, getY() + getHeight() / 4);
    }

    private void loginMessangeEntered() {
        username = uField.getText();
        password = new String(pField.getPassword());
        Boolean ISMessageTrue = isLoginMessageTrue();
        if (ISMessageTrue) {
            dispose();
            try {
                new MainFrm();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else {
            loginMessangeExpection.setVisible(true);
        }
    }

    private Boolean isLoginMessageTrue() {
        if (username.equals("java") && password.equals("beans")) {
            new EasterEgg();
            return false;
        } else {
            try {
                ResultSet resultSet = connector.executeQuery("SELECT * FROM users WHERE username = '" + username + "'");
                if (resultSet != null && resultSet.next()) { // 检查结果集是否不为空
                    if (resultSet.getString("password").equals(password)) {
                        return true;
                    }
                }
                return false;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
