package view;
import backend.model.enums.LoginStatus;
import backend.model.User;
import backend.service.impl.AuthService;
import backend.utils.impl.CaptchaUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LoginFrame extends JFrame{
    private JPanel mainPanel;//中间层面板
    private JTextField usernameTextField;//用户名输入框
    private JPasswordField passwordField;//密码输入框
    private JTextField captchaTextField;//验证码输入框
    private JLabel captchaLabel;//验证码标签
    private JButton loginButton;//登录按钮
    private JButton registerButton;//注册按钮
    private AuthService authService = new AuthService();
    private LoginStatus flag;

    public LoginStatus Flag(){
        return this.flag;
    }

    public LoginFrame() {
        //初始化界面
        initView();
        //注册监听器
        initListener();
        usernameTextField.setText("admin");
        passwordField.setText("123456");
        captchaTextField.setText(captchaLabel.getText());
    }
    private void initView(){
        //1.设置标题
        setTitle("登录");
        //2.设置点击关闭时的动作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //3.中间层容器
        add(initPanel());
        //4.不允许窗口改变大小
        setResizable(false);
        //5.自动调整窗口大小 居中显示
        pack();//自动调整窗口大小
        setLocationRelativeTo(null);//剧中
        //6.显示窗口
        setVisible(true);
    }

    private void initListener(){
        captchaLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                captchaLabel.setText(CaptchaUtils.generateCaptcha());
            }
        });

        loginButton.addActionListener(e -> {
            //点击登录按钮后的事件
            String username = usernameTextField.getText();
            String password = passwordField.getText();
            String captcha = captchaTextField.getText();
            //System.out.println("用户名" + username + " 密码" + password + " 验证码" + captcha);
            if(username.equals("")||password.equals("")||captcha.equals("")) {
                JOptionPane.showMessageDialog(this, "请填写完整信息！", "提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if(!CaptchaUtils.verifyCaptcha(captcha,captchaLabel.getText())){
                JOptionPane.showMessageDialog(this, LoginStatus.CAPTCHA_ERROR.getMsg(), "提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            User account = new User();
            account.setUsername(username);
            account.setPassword(password);
            LoginStatus loginStatus = authService.login(account);
            if(loginStatus == LoginStatus.SUCCESS) {
                //进入主界面
                new MainFrame();
                this.dispose();
            }
            else if(loginStatus == LoginStatus.USER_NOT_FOUND) {
                JOptionPane.showMessageDialog(this, LoginStatus.USER_NOT_FOUND.getMsg(), "提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            else if(loginStatus == LoginStatus.PASSWORD_ERROR) {
                JOptionPane.showMessageDialog(this, LoginStatus.PASSWORD_ERROR.getMsg(), "提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            else{
                JOptionPane.showMessageDialog(this, loginStatus.getMsg(), "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        registerButton.addActionListener(e -> {
            //点击注册按钮后的事件
           RegisterFrame registerFrame = new RegisterFrame();
           registerFrame.setVisible(true);
           registerFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        });
    }


    private JPanel initPanel(){
        mainPanel = new JPanel();
        GridLayout gridLayout = new GridLayout(6,1,10,10);
        mainPanel.setLayout(gridLayout);
        //第1层 空白面板 占用
        mainPanel.add(new JPanel());
        //第2层 账号面板
        mainPanel.add(initAcountPanel());
        //第3层 密码面板
        mainPanel.add(initPasswordPanel());
        //第4层 验证码面板
        mainPanel.add(initCaptchaPanel());
        //第5层 按钮面板
        mainPanel.add(initButtonPanel());
        //第6层 空白面板 占用
        return mainPanel;
    }

    private JPanel initAcountPanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1,4,10,0));
        JLabel label = new JLabel("用户名：");
        //让label居右
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        usernameTextField = new JTextField(15);
        panel.add(label);
        panel.add(usernameTextField);
        panel.add(new JPanel());
        return panel;
    }

    private JPanel initPasswordPanel(){
        JPanel panel = new JPanel();
        // 设置网格布局，1行4列，水平间距10像素
        panel.setLayout(new GridLayout(1,4,10,0));
        JLabel label = new JLabel("密    码：");
        // 设置标签文本右对齐
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        passwordField = new JPasswordField(15);
        panel.add(label);
        panel.add(passwordField);
        // 添加空白面板占位
        panel.add(new JPanel());
        return panel;
    }

    private JPanel initCaptchaPanel(){
        // 创建面板并设置网格布局
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1,4,10,0));

        // 创建验证码标签
        JLabel label = new JLabel("验证码：");
        label.setHorizontalAlignment(SwingConstants.RIGHT);

        // 创建验证码输入框和验证码显示标签
        captchaTextField = new JTextField(4);
        captchaLabel = new JLabel(CaptchaUtils.generateCaptcha());

        // 将组件添加到面板中
        panel.add(label);
        panel.add(captchaTextField);
        panel.add(captchaLabel);

        return panel;
    }

    private JPanel initEmailPanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1,4,10,0));
        JLabel label = new JLabel("邮    箱：");
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        JTextField emailTextField = new JTextField(15);
        panel.add(label);
        panel.add(emailTextField);
        panel.add(new JPanel());
        return panel;
    }

    private JPanel initButtonPanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1,4,10,0));
        loginButton = new JButton("登录");
        registerButton = new JButton("注册");
        panel.add(new JPanel());
        panel.add(loginButton);
        panel.add(registerButton);
        panel.add(new JPanel());
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            LoginFrame loginFrame = new LoginFrame();
            loginFrame.setVisible(true);
        });
    }
}




