package view;

import controller.Controller;
import model.Language;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

public class MenuView extends JMenuBar implements Observer {

	private final JMenu fileMenu;
	private final JMenu editMenu;
	private final JMenu helpMenu;
	private final JMenuItem opnImg;
	private final JMenuItem exit;
	private final JMenuItem rnmImg;
	private final JMenuItem about;
	private Language language;

	public MenuView(final Controller controller) {
		super();

		fileMenu = new JMenu();
		editMenu = new JMenu();
		helpMenu = new JMenu();
		opnImg = new JMenuItem();
		exit = new JMenuItem();
		rnmImg = new JMenuItem();
		about = new JMenuItem();

		opnImg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new FileChooser(controller);
			}
		});
		exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFrame frame = ((JFrame) getParent().getParent().getParent());
				frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
			}
		});
		rnmImg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String str = JOptionPane.showInputDialog(language.getString("renameImage"));
				if (str == null || str.isEmpty())
					return;
				controller.imageRenamed(str);
			}
		});
		about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, language.getString("aboutContent"), language.getString("about"), JOptionPane.INFORMATION_MESSAGE);
			}
		});

		rnmImg.setEnabled(false);

		fileMenu.add(opnImg);
		fileMenu.add(exit);
		editMenu.add(rnmImg);
		helpMenu.add(about);

		add(fileMenu);
		add(editMenu);
		add(helpMenu);

		controller.init(this);
	}

	public void setLanguage(Language language) {
		if (this.language == null)
			this.language = language;
		fileMenu.setText(language.getString("menuFile"));
		editMenu.setText(language.getString("menuEdit"));
		helpMenu.setText(language.getString("menuHelp"));
		opnImg.setText(language.getString("menuFileOpen"));
		exit.setText(language.getString("menuFileExit"));
		rnmImg.setText(language.getString("menuEditRename"));
		about.setText(language.getString("about"));
	}

	@Override
	public void update(Observable o, Object arg) {
		String tmp = (String) arg;
		if (tmp.equals("language"))
			setLanguage((Language) o);
		if (tmp.equals("image"))
			rnmImg.setEnabled(true);
	}
}

@@ -497,24 +497,57 @@
 tem rnmImg;%0A
+%09private final JMenuItem delImg;%0A
 %09private fin
@@ -815,16 +815,44 @@
 Item();%0A
+%09%09delImg = new JMenuItem();%0A
 %09%09about 
@@ -1290,16 +1290,379 @@
 %7D%0A%09%09%7D);%0A
+%09%09delImg.addActionListener(new ActionListener() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09int val = JOptionPane.showConfirmDialog(null,%0A%09%09%09%09%09%09language.getString(%22deleteImageConfirm%22), language.getString(%22menuEditDelete%22),%0A%09%09%09%09%09%09JOptionPane.YES_NO_OPTION);%0A%09%09%09%09if (val == JOptionPane.YES_OPTION)%0A%09%09%09%09%09controller.imageDeleted();%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%09rnmImg
@@ -1809,16 +1809,22 @@
 tDialog(
+null, 
 language
@@ -1848,16 +1848,84 @@
 eImage%22)
+, language.getString(%22renameTitle%22), JOptionPane.INFORMATION_MESSAGE
 );%0A%09%09%09%09i
@@ -2290,16 +2290,44 @@
 (false);
+%0A%09%09delImg.setEnabled(false);
 %0A%0A%09%09file
@@ -2390,16 +2390,40 @@
 nmImg);%0A
+%09%09editMenu.add(delImg);%0A
 %09%09helpMe
@@ -2941,16 +2941,72 @@
 ame%22));%0A
+%09%09delImg.setText(language.getString(%22menuEditDelete%22));%0A
 %09%09about.
@@ -3217,24 +3217,26 @@
 ls(%22image%22))
+ %7B
 %0A%09%09%09rnmImg.s
@@ -3252,13 +3252,45 @@
 (true);%0A
+%09%09%09delImg.setEnabled(true);%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

add delete button and dialog titles