package gui;

import model.Student;
import model.Teacher;
import service.StudentService;
import service.TeacherService;
import service.ServiceFactory;

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

public class LoginFrame extends JFrame {
    private JComboBox<String> userTypeComboBox;
    private JTextField idField;
    private JPasswordField passwordField;
    private JButton loginButton;
    
    private final StudentService studentService = ServiceFactory.getStudentService();
    private final TeacherService teacherService = ServiceFactory.getTeacherService();

    public LoginFrame() {
        setTitle("平安校园 - 登录");
        setSize(350, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板，使用BorderLayout
        JPanel mainPanel = new JPanel(new BorderLayout(20, 20));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));

        // 标题面板
        JPanel titlePanel = new JPanel();
        JLabel titleLabel = new JLabel("日常出校申请系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        titlePanel.add(titleLabel);

        // 登录表单面板
        JPanel formPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(8, 8, 8, 8);
        gbc.weightx = 1.0;

        // 用户类型
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel userTypeLabel = new JLabel("用户类型:");
        userTypeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(userTypeLabel, gbc);

        gbc.gridx = 1;
        userTypeComboBox = new JComboBox<>(new String[]{"学生", "教师"});
        userTypeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(userTypeComboBox, gbc);

        // ID输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel idLabel = new JLabel("ID:");
        idLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(idLabel, gbc);

        gbc.gridx = 1;
        idField = new JTextField(20);
        idField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(idField, gbc);

        // 密码输入框
        gbc.gridx = 0;
        gbc.gridy = 2;
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        passwordField = new JPasswordField(20);
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        formPanel.add(passwordField, gbc);

        // 登录按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        loginButton = new JButton("登录");
        loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        loginButton.setPreferredSize(new Dimension(100, 35));
        loginButton.setBackground(new Color(52, 152, 219));
//        loginButton.setForeground(Color.WHITE);
        loginButton.setFocusPainted(false);
        buttonPanel.add(loginButton);

        // 添加事件监听
        loginButton.addActionListener(e -> handleLogin());

        // 组装界面
        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(formPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    private void handleLogin() {
        String userType = (String) userTypeComboBox.getSelectedItem();
        String id = idField.getText().trim();
        String password = new String(passwordField.getPassword());

        if (id.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入ID和密码", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        if ("学生".equals(userType)) {
            Student student = studentService.login(id, password);
            if (student != null) {
                StudentMainFrame studentMainFrame = new StudentMainFrame(student);
                studentMainFrame.setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "无效的学号或密码", "错误", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            Teacher teacher = teacherService.login(id, password);
            if (teacher != null) {
                TeacherMainFrame teacherMainFrame = new TeacherMainFrame();
                teacherMainFrame.setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "无效的教师ID或密码", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        SwingUtilities.invokeLater(() -> {
            LoginFrame loginFrame = new LoginFrame();
            loginFrame.setVisible(true);
        });
    }
}
