package view;

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

public class MainMenuView extends JPanel {
    private final JLabel welcomeLabel = new JLabel("", SwingConstants.CENTER);
    private final JComboBox<String> typeComboBox = new JComboBox<>();
    private final JSpinner countSpinner = new JSpinner(new SpinnerNumberModel(10, 10, 30, 1));
    private final JButton startExamButton = new JButton("开始答题");
    private final JButton changePasswordButton = new JButton("修改密码");
    private final JButton logoutButton = new JButton("退出登录");

    public MainMenuView() {
        setLayout(new BorderLayout(20, 20));
        setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));

        welcomeLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 28));
        add(welcomeLabel, BorderLayout.NORTH);

        JPanel centerPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 10, 15, 10);

        gbc.gridx = 0; gbc.gridy = 0; addLabel(centerPanel, "选择题目难度:", gbc);
        gbc.gridx = 1; gbc.gridy = 0; addComponent(centerPanel, typeComboBox, gbc);

        gbc.gridx = 0; gbc.gridy = 1; addLabel(centerPanel, "选择题目数量:", gbc);
        gbc.gridx = 1; gbc.gridy = 1; addComponent(centerPanel, countSpinner, gbc);

        startExamButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));

        startExamButton.putClientProperty("JButton.buttonType", "roundRect");
        gbc.gridy = 2; gbc.gridwidth = 2; gbc.insets = new Insets(30, 10, 15, 10);
        addComponent(centerPanel, startExamButton, gbc);
        add(centerPanel, BorderLayout.CENTER);

        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 0));
        southPanel.add(changePasswordButton);
        southPanel.add(logoutButton);
        add(southPanel, BorderLayout.SOUTH);

        for (UserType type : UserType.values()) {
            typeComboBox.addItem(type.getDisplayName());
        }
    }

    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 void setWelcomeMessage(String message) { welcomeLabel.setText(message); }
    public UserType getSelectedType() {
        String displayName = (String) typeComboBox.getSelectedItem();
        for (UserType type : UserType.values()) {
            if (type.getDisplayName().equals(displayName)) return type;
        }
        return UserType.PRIMARY;
    }
    public int getQuestionCount() { return (int) countSpinner.getValue(); }
    public JButton getStartExamButton() { return startExamButton; }
    public JButton getChangePasswordButton() { return changePasswordButton; }
    public JButton getLogoutButton() { return logoutButton; }
}