package view;

import backend.model.Questions;

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

public class MainFrame extends JFrame{
    private JPanel mainPanel;
    private JMenuBar menuBar;
    private JMenu questionsMenu;
    private JMenu examMenu;
    private JMenu userMenu;
    protected static String role = LoginFrame.loginRole;
    protected static int userId = LoginFrame.userId;

    // 颜色常量定义
    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 MainFrame(){
        initView();
        initListener();
    }

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

    private void initListener(){
        if(role.equals("admin")) {
            questionsMenu.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    mainPanel.removeAll();
                    mainPanel.add(new QuestionsPanel());
                    mainPanel.revalidate();
                    mainPanel.repaint();
                }
            });
            userMenu.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    mainPanel.removeAll();
                    mainPanel.add(new UserPanel());
                    mainPanel.revalidate();
                    mainPanel.repaint();
                }
            });
        }
        examMenu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mainPanel.removeAll();
                mainPanel.add(new ExamPanel());
                mainPanel.revalidate();
                mainPanel.repaint();
            }
        });
    }

    private JPanel initPanel(){
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBackground(BACKGROUND_COLOR);
        setJMenuBar(initMenuBar());
        if(MainFrame.role.equals("admin")) {
            //默认为问题库界面
            mainPanel.add(new QuestionsPanel(), BorderLayout.CENTER);
        }
        else{
            //默认为考试界面
            mainPanel.add(new ExamPanel(), BorderLayout.CENTER);
        }
        return mainPanel;
    }

    private JMenuBar initMenuBar(){
        menuBar = new JMenuBar();
        menuBar.setBackground(PRIMARY_COLOR);

        if(role.equals("admin")) {
            questionsMenu = new JMenu("问题库管理");
            questionsMenu.setForeground(Color.WHITE);
            questionsMenu.setFont(new Font("微软雅黑", Font.BOLD, 14));
            menuBar.add(questionsMenu);

            userMenu = new JMenu("用户管理");
            userMenu.setForeground(Color.WHITE);
            userMenu.setFont(new Font("微软雅黑", Font.BOLD, 14));
            menuBar.add(userMenu);
        }

        examMenu = new JMenu("测试管理");
        examMenu.setForeground(Color.WHITE);
        examMenu.setFont(new Font("微软雅黑", Font.BOLD, 14));
        menuBar.add(examMenu);

        return menuBar;
    }

    public static void main(String[] args) {
        new MainFrame().initView();
    }
}
