package com.msksbr.MainFrm;

import com.msksbr.MainFrm.MenuItemDiaog.*;
import com.msksbr.MainFrm.MenuItemDiaog.Adder.BookAdder;
import com.msksbr.MainFrm.MenuItemDiaog.Adder.RentAdder;
import com.msksbr.MainFrm.MenuItemDiaog.Adder.StudentAdder;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuBar extends JMenuBar {
    public JMenuItem exitItem;
    protected com.msksbr.MainFrm.MenuItemDiaog.aboutDIalog aboutDIalog;
    protected AuthorDIalog authorDIalog;

    public MenuBar() {
        // “文件“菜单
        JMenu fileMenu = new JMenu("文件");
        exitItem = new JMenuItem("退出");
        fileMenu.add(exitItem);

        // ”查询“菜单
        JMenu searchMenu = new JMenu("查询");
        JMenuItem bookSearch = new JMenuItem("查询图书信息");
        bookSearch.addActionListener(new ActionListener() {
                                         @Override
                                         public void actionPerformed(ActionEvent e) {
                                             new SearchDIalog("books");
                                         }
                                     }
        );
        JMenuItem studentSearch = new JMenuItem("查询学生信息");
        studentSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new SearchDIalog("students");
            }
        });
        JMenuItem rentSearch = new JMenuItem("查询借阅信息");
        rentSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new SearchDIalog("rents");
            }
        });
        searchMenu.add(bookSearch);
        searchMenu.add(studentSearch);
        searchMenu.add(rentSearch);

        // ”添加“菜单
        JMenu addMenu = new JMenu("添加");
        JMenuItem bookAdd = new JMenuItem("添加图书信息");
        bookAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new BookAdder();
            }
        });
        JMenuItem studentAdd = new JMenuItem("添加学生信息");
        studentAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new StudentAdder();
            }
        });
        JMenuItem rentAdd = new JMenuItem("添加借阅信息");
        rentAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new RentAdder();
            }
        });
        addMenu.add(bookAdd);
        addMenu.add(studentAdd);
        addMenu.add(rentAdd);

        // ”删除“菜单
        JMenu removeMenu = new JMenu("删除");
        JMenuItem bookRemove = new JMenuItem("删除图书信息");
        bookRemove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Remover("books");
            }
        });
        JMenuItem studentRemove = new JMenuItem("删除学生信息");
        studentRemove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Remover("students");
            }
        });
        JMenuItem rentRemove = new JMenuItem("删除借阅信息");
        rentRemove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Remover("rents");
            }
        });
        removeMenu.add(bookRemove);
        removeMenu.add(studentRemove);
        removeMenu.add(rentRemove);

        // ”关于“菜单
        JMenu aboutMenu = new JMenu("关于");
        JMenuItem about = new JMenuItem("关于软件");
        aboutDIalog = new aboutDIalog();
        about.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                aboutDIalog.setVisible(true);
            }
        });
        JMenuItem author = new JMenuItem("作者");
        authorDIalog = new AuthorDIalog();
        author.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                authorDIalog.setVisible(true);
            }
        });
        JMenuItem MITLicense = new JMenuItem("MIT许可证");
        MITLicense.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new MITDialog();
            }
        });
        JMenuItem thanksItem = new JMenuItem("鸣谢");
        thanksItem.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        new AcademyThanksDialog();
                    }
                }
        );
        aboutMenu.add(about);
        aboutMenu.add(author);
        aboutMenu.add(MITLicense);
        aboutMenu.add(thanksItem);

        //将菜单加入菜单栏
        add(fileMenu);
        add(searchMenu);
        add(addMenu);
        add(removeMenu);
        add(aboutMenu);
    }
}
