package view;

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

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

public class RegisterFrame extends JFrame{
        private JPanel mainPanel;//中间层面板
        private JTextField usernameTextField;//用户名输入框
        private JPasswordField passwordField;//密码输入框
        private JPasswordField confirmPasswordField;//确认密码输入框
        private JTextField captchaTextField;//验证码输入框
        private JTextField emailTextField;//邮箱输入框
        private JLabel captchaLabel;//验证码标签
        private JButton registerButton;//注册按钮
        private JButton backButton;//返回按钮
        private AuthService authService = new AuthService();

        // 颜色常量定义
        private static final Color PRIMARY_COLOR = new Color(70, 130, 180);  // 钢蓝色
        private static final Color BACKGROUND_COLOR = new Color(248, 249, 250);  // 浅灰色背景
        private static final Color BORDER_COLOR = new Color(220, 220, 220);  // 边框颜色
        private static final Color HIGHLIGHT_COLOR = new Color(255, 255, 204);  // 高亮背景

        public RegisterFrame() {
            //初始化界面
            initView();
            //注册监听器
            initListener();
            usernameTextField.setText("admin");
            passwordField.setText("123456");
            captchaTextField.setText(captchaLabel.getText());
            emailTextField.setText("123456@qq.com");
            confirmPasswordField.setText(passwordField.getText());
        }

        private void initView(){
            //1.设置标题
            setTitle("中医药知识测试系统 - 注册");
            //2.设置点击关闭时的动作
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //3.设置窗口背景色
            getContentPane().setBackground(BACKGROUND_COLOR);
            //4.中间层容器
            add(initPanel());
            //5.不允许窗口改变大小
            setResizable(false);
            //6.设置窗口大小 居中显示
            setSize(400, 400);
            setLocationRelativeTo(null);
            //7.显示窗口
            setVisible(true);
        }

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

            registerButton.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;
                }
                if(!confirmPasswordField.getText().equals(password))
                    JOptionPane.showMessageDialog(this, LoginStatus.PASSWORD_NOT_MATCH.getMsg(), "提示", JOptionPane.INFORMATION_MESSAGE);
                User account = new User();
                account.setId();
                account.setUsername(username);
                account.setPassword(password);
                account.setEmail(emailTextField.getText());
                account.setRole("user");
                account.setCreatedAt();
                LoginStatus loginStatus = authService.register(account);
                if(loginStatus==LoginStatus.REGISTER_SUCCESS) {
                    this.dispose();
                }
                if(loginStatus == LoginStatus.SUCCESS) {
                    //返回注册界面
                    mainPanel.removeAll();
                    //关闭注册界面
                    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);
                }
            });
            backButton.addActionListener(e -> {
                //返回登录界面
                mainPanel.removeAll();
                dispose();
            });
        }


    private JPanel initPanel(){
            mainPanel = new JPanel(new BorderLayout());
            mainPanel.setBackground(BACKGROUND_COLOR);

            // 创建顶部标题面板
            JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            titlePanel.setBackground(PRIMARY_COLOR);
            JLabel titleLabel = new JLabel("用户注册");
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
            titleLabel.setForeground(Color.WHITE);
            titlePanel.add(titleLabel);
            mainPanel.add(titlePanel, BorderLayout.NORTH);

            // 创建中央注册表单面板
            JPanel formPanel = new JPanel(new GridBagLayout());
            formPanel.setBackground(BACKGROUND_COLOR);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(10, 10, 10, 10);

            // 第1行 - 标题
            gbc.gridx = 0; gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.anchor = GridBagConstraints.CENTER;
            JLabel formTitleLabel = new JLabel("创建新账户");
            formTitleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
            formTitleLabel.setForeground(PRIMARY_COLOR);
            formPanel.add(formTitleLabel, gbc);

            // 第2行 - 用户名
            gbc.gridx = 0; gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.anchor = GridBagConstraints.WEST;
            JLabel usernameLabel = new JLabel("用户名：");
            usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            formPanel.add(usernameLabel, gbc);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            usernameTextField = new JTextField(15);
            usernameTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            usernameTextField.setPreferredSize(new Dimension(200, 25));
            formPanel.add(usernameTextField, gbc);

            // 第3行 - 密码
            gbc.gridx = 0; gbc.gridy = 2;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            JLabel passwordLabel = new JLabel("密  码：");
            passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            formPanel.add(passwordLabel, gbc);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            passwordField = new JPasswordField(15);
            passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            passwordField.setPreferredSize(new Dimension(200, 25));
            formPanel.add(passwordField, gbc);

            // 第4行 - 确认密码
            gbc.gridx = 0; gbc.gridy = 3;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            JLabel confirmPasswordLabel = new JLabel("确认密码：");
            confirmPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            formPanel.add(confirmPasswordLabel, gbc);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            confirmPasswordField = new JPasswordField(15);
            confirmPasswordField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            confirmPasswordField.setPreferredSize(new Dimension(200, 25));
            formPanel.add(confirmPasswordField, gbc);

            // 第5行 - 邮箱
            gbc.gridx = 0; gbc.gridy = 4;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            JLabel emailLabel = new JLabel("邮  箱：");
            emailLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            formPanel.add(emailLabel, gbc);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            emailTextField = new JTextField(15);
            emailTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            emailTextField.setPreferredSize(new Dimension(200, 25));
            formPanel.add(emailTextField, gbc);

            // 第6行 - 验证码
            gbc.gridx = 0; gbc.gridy = 5;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            JLabel captchaLabel = new JLabel("验证码：");
            captchaLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            formPanel.add(captchaLabel, gbc);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            JPanel captchaPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
            captchaPanel.setBackground(BACKGROUND_COLOR);
            captchaTextField = new JTextField(8);
            captchaTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            captchaTextField.setPreferredSize(new Dimension(80, 25));
            captchaPanel.add(captchaTextField);

            this.captchaLabel = new JLabel(CaptchaUtils.generateCaptcha());
            this.captchaLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
            this.captchaLabel.setBackground(Color.WHITE);
            this.captchaLabel.setOpaque(true);
            this.captchaLabel.setBorder(BorderFactory.createLineBorder(BORDER_COLOR));
            this.captchaLabel.setPreferredSize(new Dimension(80, 25));
            this.captchaLabel.setHorizontalAlignment(JLabel.CENTER);
            captchaPanel.add(this.captchaLabel);

            formPanel.add(captchaPanel, gbc);

            // 第7行 - 按钮
            gbc.gridx = 0; gbc.gridy = 6;
            gbc.gridwidth = 2;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0.0;
            gbc.anchor = GridBagConstraints.CENTER;
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
            buttonPanel.setBackground(BACKGROUND_COLOR);

            registerButton = new JButton("注册");
            backButton = new JButton("返回");

            // 设置按钮样式
            setButtonStyle(registerButton);
            setButtonStyle(backButton);

            buttonPanel.add(registerButton);
            buttonPanel.add(backButton);

            formPanel.add(buttonPanel, gbc);

            // 添加边距
            formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            mainPanel.add(formPanel, BorderLayout.CENTER);

            return mainPanel;
        }

        // 设置按钮样式
        private void setButtonStyle(JButton button) {
            button.setFont(new Font("微软雅黑", Font.BOLD, 14));
            button.setPreferredSize(new Dimension(80, 30));
            button.setBackground(PRIMARY_COLOR);
            button.setForeground(Color.WHITE);
            button.setFocusPainted(false);
            button.setBorder(BorderFactory.createRaisedBevelBorder());
        }

        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 initConfirmPasswordPanel(){
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(1,4,10,0));
            JLabel label = new JLabel("确认密码：");
            label.setHorizontalAlignment(SwingConstants.RIGHT);
            confirmPasswordField = new JPasswordField(15);
            panel.add(label);
            panel.add(confirmPasswordField);
            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);
            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));
            registerButton = new JButton("注册");
            backButton = new JButton("返回");
            panel.add(new JPanel());
            panel.add(registerButton);
            panel.add(backButton);
            panel.add(new JPanel());
            return panel;
        }
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                view.RegisterFrame registerFrame = new view.RegisterFrame();
                registerFrame.setVisible(true);
            });
        }
}
