package view;

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

public class PasswordSetView extends JPanel {
    private final JPasswordField passwordField = new JPasswordField(20);
    private final JPasswordField confirmPasswordField = new JPasswordField(20);
    private final JButton submitButton = new JButton("完成注册");

    public PasswordSetView() {
        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);

        JLabel hintLabel = new JLabel("密码要求: 6-10位，且必须包含大小写字母和数字。", SwingConstants.CENTER);
        hintLabel.setForeground(Color.GRAY);
        gbc.gridy = 1; formPanel.add(hintLabel, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0; gbc.gridy = 2; addLabel(formPanel, "输入密码:", gbc);
        gbc.gridx = 1; gbc.gridy = 2; addComponent(formPanel, passwordField, gbc);

        gbc.gridx = 0; gbc.gridy = 3; addLabel(formPanel, "确认密码:", gbc);
        gbc.gridx = 1; gbc.gridy = 3; addComponent(formPanel, confirmPasswordField, gbc);

        gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2; gbc.insets = new Insets(20, 10, 10, 10);
        submitButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        addComponent(formPanel, submitButton, gbc);

        add(formPanel);
    }

    private void addLabel(JPanel panel, String text, GridBagConstraints gbc) {
        JLabel label = new JLabel(text);
        label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        panel.add(label, gbc);
    }
    private void addComponent(JPanel panel, JComponent comp, GridBagConstraints gbc) {
        comp.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        panel.add(comp, gbc);
    }

    public String getPassword() { return new String(passwordField.getPassword()); }
    public String getConfirmPassword() { return new String(confirmPasswordField.getPassword()); }
    public JButton getSubmitButton() { return submitButton; }
}