package gui.menubar;

import gui.frames.MainFrame;
import gui.simulator.SimulatorDialogPane;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import agenda.Agenda;
import agenda.Database;
import agenda.Io;

@SuppressWarnings("serial")
public class MenuBar extends JMenuBar{

	private MainFrame mainFrame;

	public MenuBar(MainFrame frame){
		this.mainFrame = frame;
		initialize();
	}

	private void initialize(){
		setLayout(new FlowLayout(80));
		JMenu fileMenu = new JMenu("File");
		JMenu editMenu = new JMenu("Edit");
		JMenu viewMenu = new JMenu("View");
		JMenu helpMenu = new JMenu("Help");
		this.add(fileMenu);
		this.add(editMenu);
		this.add(viewMenu);
		this.add(helpMenu);

		JMenuItem newAction = new JMenuItem("New");
		JMenuItem openAction = new JMenuItem("Open");
		JMenuItem exitAction = new JMenuItem("Exit");
		JMenuItem cutAction = new JMenuItem("Cut");
		JMenuItem copyAction = new JMenuItem("Copy");
		JMenuItem pasteAction = new JMenuItem("Paste");
		JMenuItem saveAction = new JMenuItem("Save");
		JMenuItem helpAction = new JMenuItem("Help");
		JMenuItem uploadAction = new JMenuItem("Upload agenda to DB");
		//view 
		JMenuItem editorView = new JMenuItem("Editor");
		JMenuItem agendaView = new JMenuItem("Agenda");
		JMenuItem simulatorView = new JMenuItem("Simulator");
		JMenuItem tableView = new JMenuItem("Table");

		newAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		openAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		exitAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
		cutAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		copyAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		pasteAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
		saveAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
		uploadAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,ActionEvent.CTRL_MASK));

		fileMenu.add(newAction);
		newAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("You have clicked on the new action");
			}
		});
		fileMenu.add(openAction);
		openAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					Agenda agenda= Io.readIo();
					if(agenda != null){
						mainFrame.setAgenda(agenda);
						System.out.println("read");
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		fileMenu.add(saveAction);
		saveAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Io.writeIo(mainFrame.getAgenda());
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		fileMenu.add(uploadAction);
		uploadAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Database database = new Database();
				try {
					database.writeDatabase();
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("Database error");
				}
				System.out.println("You have clicked on the database action");
			}
		});
		fileMenu.addSeparator();
		fileMenu.add(exitAction);
		exitAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		editMenu.add(cutAction);
		cutAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("You have clicked on the cut action");
			}
		});
		editMenu.add(copyAction);
		copyAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("You have clicked on the copy action");
			}
		});
		editMenu.add(pasteAction);
		pasteAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("You have clicked on the paste action");
			}
		});

		//view
		viewMenu.add(editorView);
		editorView.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Editor View");
				mainFrame.changeView(MainFrame.Views.EDITOR);
			}
		});

		viewMenu.add(simulatorView);
		simulatorView.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Simulator View");
				new SimulatorDialogPane(mainFrame);
			}
		});

		viewMenu.add(agendaView);
		agendaView.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Agenda View");
				mainFrame.changeView(MainFrame.Views.AGENDA);

			}
		});

		viewMenu.add(tableView);
		tableView.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainFrame.changeView(MainFrame.Views.TABLE);
			}
		});
		helpMenu.add(helpAction);
		helpAction.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(mainFrame,"Deze applicatie is gemaakt door: "
						+ "\nJanco, Gillian, Tim, Davey en Jeroen"
						+ "\n\nIn opdracht voor: "
						+ "\n Ti Avans Breda"
						);
			}
		});

	}

}
