package view;

import javax.swing.*;
import java.awt.*;

public class RegisterView extends JPanel {
    private final JTextField emailField = new JTextField(20);
    private final JTextField usernameField = new JTextField(20);
    private final JTextField codeField = new JTextField(10);
    private final JButton getCodeButton = new JButton("发送验证邮件");
    private final JButton registerButton = new JButton("下一步：设置密码");
    private final JButton backButton = new JButton("返回登录");

    public RegisterView() {
        setLayout(new GridBagLayout());
        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JLabel titleLabel = new JLabel("创建您的账号", SwingConstants.CENTER);
        titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 28));
        gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; formPanel.add(titleLabel, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0; gbc.gridy = 1; addLabel(formPanel, "邮箱地址:", gbc);
        gbc.gridx = 1; gbc.gridy = 1; addComponent(formPanel, emailField, gbc);

        gbc.gridx = 0; gbc.gridy = 2; addLabel(formPanel, "设置用户名:", gbc);
        gbc.gridx = 1; gbc.gridy = 2; addComponent(formPanel, usernameField, gbc);

        JPanel codePanel = new JPanel(new BorderLayout(10, 0));
        addComponent(codePanel, codeField, BorderLayout.CENTER);
        codePanel.add(getCodeButton, BorderLayout.EAST);

        gbc.gridx = 0; gbc.gridy = 3; addLabel(formPanel, "邮箱验证码:", gbc);
        gbc.gridx = 1; gbc.gridy = 3; formPanel.add(codePanel, gbc);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 0));
        registerButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        backButton.putClientProperty("FlatLaf.style", "buttonType: borderless");
        buttonPanel.add(registerButton);
        buttonPanel.add(backButton);
        gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2; gbc.insets = new Insets(20, 10, 10, 10);
        formPanel.add(buttonPanel, gbc);

        add(formPanel);
    }

    private void addLabel(JPanel p, String s, GridBagConstraints g) {
        JLabel label = new JLabel(s);
        label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        p.add(label, g);
    }
    private void addComponent(JPanel p, JComponent c, GridBagConstraints g) {
        c.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        p.add(c, g);
    }
    private void addComponent(JPanel p, JComponent c, String pos) {
        c.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        p.add(c, pos);
    }

    public String getEmail() { return emailField.getText().trim(); }
    public String getUsername() { return usernameField.getText().trim(); }
    public String getCode() { return codeField.getText().trim(); }
    public JButton getGetCodeButton() { return getCodeButton; }
    public JButton getRegisterButton() { return registerButton; }
    public JButton getBackButton() { return backButton; }
}