package view;

import backend.model.Questions;
import backend.service.QuestionsService;

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

public class EditQuestionDialog extends JDialog{
    private JTextField nameTextField;
    private JTextField descriptionTextField;
    private JTextField answerTextField;
    private JComboBox frequencyCombobox;
    private JButton saveButton;
    private JButton cancelButton;
    private int type;
    private Questions question;
    private QuestionsService questionsService = new QuestionsService();

    // 颜色常量定义
    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 EditQuestionDialog(int type, Questions question) {
        this.type = type;
        this.question = question;
        initView();
    }

    public void initListener(){
        saveButton.addActionListener(e->{
            String name = nameTextField.getText().trim();
            String description = descriptionTextField.getText().trim();
            String answer = answerTextField.getText().trim();
            if(name.equals("")|| description.equals("")|| answer.equals("")) {
                JOptionPane.showMessageDialog(this, "请填写完整！");
                return;
            }
            Questions questionsPo = new Questions();
            questionsPo.setName(name);
            questionsPo.setDescription(description);
            questionsPo.setAnswer(answer);
            Integer index=frequencyCombobox.getSelectedIndex();
            questionsPo.setFrequency(index==0 ? null:index);
            String message;
            boolean res;
            if(type==0) {
                res = questionsService.insert(questionsPo);
                message = "添加";
            }
            else{
                questionsPo.setId(question.getId());
                res = questionsService.update(questionsPo);
                message = "修改";
            }
            if(res)
                dispose();
            else
                JOptionPane.showMessageDialog(this, message+"失败");
        });
        cancelButton.addActionListener(e->{
            dispose();
        });
    }

    private void initView(){
        setSize(400, 350);
        setLocationRelativeTo(null);//居中
        setLayout(new BorderLayout());

        // 创建顶部标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        titlePanel.setBackground(PRIMARY_COLOR);
        JLabel titleLabel = new JLabel(type == 0 ? "添加题目" : "编辑题目");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        titleLabel.setForeground(Color.WHITE);
        titlePanel.add(titleLabel);
        add(titlePanel, BorderLayout.NORTH);

        // 添加主内容面板
        add(initPanel(), BorderLayout.CENTER);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setModal(true);//设置模态
        if(type==0)
            setTitle("添加题目");
        else{
            setTitle("编辑题目");
            nameTextField.setText(question.getName());
            descriptionTextField.setText(question.getDescription());
            answerTextField.setText(question.getAnswer());
            frequencyCombobox.setSelectedIndex(question.getFrequency() == null? 0:question.getFrequency());
        }
        //细节：要先初始化监听器，再初始化界面
        //初始化监听器
        initListener();
        //显示界面
        setVisible(true);
    }

    private JPanel initPanel(){
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBackground(BACKGROUND_COLOR);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        // 第一行 - 问题
        gbc.gridx = 0; gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(new JLabel("问题:"), gbc);

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

        // 第二行 - 描述
        gbc.gridx = 0; gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        mainPanel.add(new JLabel("描述:"), gbc);

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

        // 第三行 - 答案
        gbc.gridx = 0; gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        mainPanel.add(new JLabel("答案:"), gbc);

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

        // 第四行 - 频率
        gbc.gridx = 0; gbc.gridy = 3;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        mainPanel.add(new JLabel("频率:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        frequencyCombobox = new JComboBox<>(new String[]{"未指定","低频","中频","高频"});
        frequencyCombobox.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        frequencyCombobox.setPreferredSize(new Dimension(200, 25));
        mainPanel.add(frequencyCombobox, gbc);

        // 第五行 - 按钮
        gbc.gridx = 0; gbc.gridy = 4;
        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);

        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");

        // 设置按钮样式
        setButtonStyle(saveButton);
        setButtonStyle(cancelButton);

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        mainPanel.add(buttonPanel, gbc);

        // 添加边距
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        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());
    }

}
