package Pages;

import Operations.AddBook;
import Store.Books;
import Store.Users;
import Util.BasicOperation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.List;

public class Home {
    static JList<String> stringJList = new JList<>();
    static DefaultListModel<String> model = new DefaultListModel<>();
    public static void main(Users users) throws SQLException {
        // 初始JFrame
        JFrame frame = new JFrame("首页");
        frame.setSize(800,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 初始JPanel
        JPanel panel = new JPanel();
        // 调用组件构造函数
        placeHomeComponents(frame,panel, users);
        // 将panel添加到frame
        frame.add(panel);
        // 设置可视化
        frame.setVisible(true);
    }

    private static void placeHomeComponents(JFrame frame,JPanel panel,Users users) throws SQLException {

        // 设置布局
        panel.setLayout(null);

        // 欢迎语
        String sentence = "欢迎"+users.getUsername()+(users.isAdmin()?"管理员":"用户")+"进入图书管理系统";
        JLabel welcome = new JLabel(sentence);
        welcome.setBounds(170,40,300,25);
        welcome.setFont(new Font("宋体",Font.PLAIN,16));
        panel.add(welcome);

        // JTable
        String[] columnNames = {"书名"};

        // 书库
        List books;
        BasicOperation basicOperation = new BasicOperation();
        books = basicOperation.select();
        for (Object o : books) {
            Books b = (Books)o;
            model.addElement(b.getName());
        }
        stringJList.setBounds(150,100,490,345);
        stringJList.setModel(model);
        panel.add(stringJList);

        // 返回按钮
        JButton callBack = new JButton("退出登录");
        callBack.setBounds(680,5,100,25);
        callBack.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                Login.main();
            }
        });
        panel.add(callBack);

        // 菜单栏
        if (users.isAdmin()) {

            JButton addBook = new JButton("添加书籍");
            addBook.setBounds(30,100,90,45);
            addBook.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    AddBook.main();
                }
            });
            panel.add(addBook);

            JButton deleteBook = new JButton("删除书籍");
            deleteBook.setBounds(30,200,90,45);
            panel.add(deleteBook);
            deleteBook.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 待解决
                    String book = stringJList.getSelectedValue();
                    BasicOperation basicOperation1 = new BasicOperation();
                    if (basicOperation1.deleteBook(book)) {
                        JOptionPane.showMessageDialog(panel,"删除成功");
                    } else {
                        JOptionPane.showMessageDialog(panel,"删除失败");
                    }
                }
            });

            JButton changeBook = new JButton("修改书籍");
            changeBook.setBounds(30,300,90,45);
            panel.add(changeBook);
            changeBook.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
//                    BasicOperation basicOperation1 = new BasicOperation();
//                    if (basicOperation1.changeBook()) {
//                        JOptionPane.showMessageDialog(panel,"修改成功，请重新刷新！");
//                    } else {
//                        JOptionPane.showMessageDialog(panel,"修改失败");
//                    }
                }
            });

            JButton searchBook = new JButton("查找书籍");
            searchBook.setBounds(30,400,90,45);
            panel.add(searchBook);

            JButton flush = new JButton("刷新");
            flush.setBounds(670,100,90,45);
            panel.add(flush);
            flush.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model = new DefaultListModel<>();
                    List books;
                    BasicOperation basicOperation = new BasicOperation();
                    try {
                        books = basicOperation.select();
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    for (Object o : books) {
                        Books b = (Books)o;
                        model.addElement(b.getName());
                    }
                    stringJList.setBounds(150,100,490,345);
                    stringJList.setModel(model);
                    panel.add(stringJList);
                }
            });

        } else {

            JButton addBook = new JButton("添加书籍");
            addBook.setBounds(30,180,90,45);
            panel.add(addBook);

            JButton searchBook = new JButton("查找书籍");
            searchBook.setBounds(30,320,90,45);
            panel.add(searchBook);

            JButton flush = new JButton("刷新");
            flush.setBounds(670,180,90,45);
            panel.add(flush);

        }
    }
}
