package de.lazyzero.kkMulticopterFlashTool.gui;

import static lu.tudor.santec.i18n.Translatrix._;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import lu.tudor.santec.i18n.Translatrix;
import de.lazyzero.kkMulticopterFlashTool.KKMulticopterFlashTool;
import de.lazyzero.kkMulticopterFlashTool.utils.Icons;

public class kkMenu extends JMenuBar {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JMenu setup;
	private JMenu info;
	private JMenuBar bar;
	
	private KKMulticopterFlashTool parent;

	public kkMenu(KKMulticopterFlashTool parent) {
		this.parent = parent;
		
		initSetupMenu();
		initInfoMenu();
				
		bar = new JMenuBar();
		bar.add(setup);
		bar.add(info);
	}

	public JMenuBar getBar() {
		return bar;
	}
	
	private void initInfoMenu() {
		info = new JMenu(_("info"));
		info.setMnemonic(_("info.key").charAt(0));
		
		JMenuItem licenseItem = new JMenuItem(_("info.license"));
		licenseItem.setMnemonic(_("info.license.key").charAt(0));
		licenseItem.setIcon(Icons.getIcon22(Icons.LICENSE));
		info.add(licenseItem);
		
		JMenuItem aboutItem = new JMenuItem(_("info.about"));
		aboutItem.setMnemonic(_("info.about.key").charAt(0));
		aboutItem.setIcon(Icons.getIcon22(Icons.ABOUT));
		info.add(aboutItem);
		
		licenseItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						JLabel title = new JLabel("<HTML><H2>KKmulticopter Flash Tool " + KKMulticopterFlashTool.VERSION +"</H2></HTML>");
						title.setHorizontalAlignment(JLabel.CENTER);
						String licenseText = null;
						
						try {
							File licenseFile = new File("license.txt");
							if (System.getProperty("os.name").toLowerCase().contains("mac")) {
								String path = System.getProperty("java.library.path");
					        	path = path.substring(0, path.indexOf(":"));
								licenseFile = new File(path,"license.txt");
							}
							FileInputStream licenseIS = new FileInputStream(licenseFile);
							
							StringBuilder text = new StringBuilder();
						    String NL = System.getProperty("line.separator");
						    Scanner scanner = new Scanner(licenseIS);

						    while (scanner.hasNextLine()) {
						    	text.append(scanner.nextLine() + NL);
						    }
						    licenseText = text.toString();
						    
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						
						JTextArea license = new JTextArea(licenseText);
						JScrollPane main = new JScrollPane(license);
						
						JDialog about = new JDialog(parent);
						about.setIconImage(Icons.getIcon(Icons.app).getImage());
						about.add(title, BorderLayout.NORTH);
						about.add(main, BorderLayout.CENTER);
						about.setSize(new Dimension(540,400));
						about.setLocationRelativeTo(parent);
						about.setVisible(true);
					}
				}
		);
		
		aboutItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						JLabel icon = new JLabel(Icons.getIcon(Icons.app, 128));
						JLabel title = new JLabel("<HTML><H2>KKmulticopter Flash Tool " + KKMulticopterFlashTool.VERSION +"</H2></HTML>");
						title.setHorizontalAlignment(JLabel.CENTER);
						JLabel main = new JLabel(Translatrix.getTranslationString("about.message"));
						main.setHorizontalAlignment(JLabel.CENTER);
						
						JDialog about = new JDialog(parent);
						about.setIconImage(Icons.getIcon(Icons.app).getImage());
						about.add(title, BorderLayout.NORTH);
						about.add(icon, BorderLayout.WEST);
						about.add(main, BorderLayout.CENTER);
						about.setSize(new Dimension(480,240));
						about.setLocationRelativeTo(parent);
						about.setVisible(true);
					}
				}
			);
	}

	private void initSetupMenu() {
		setup = new JMenu(_("settings"));
		setup.setMnemonic(_("settings.key").charAt(0));
		
		final JCheckBoxMenuItem offlineMenuItem = new JCheckBoxMenuItem(_("settings.offline"));
		offlineMenuItem.setSelected(KKMulticopterFlashTool.isOfflineMode());
		setup.add(offlineMenuItem);
		
		final JMenuItem offlineDownloadMenuItem = new JMenuItem(_("settings.downloadoffline"));
		setup.add(offlineDownloadMenuItem);
		
		setup.addSeparator();
		
		JMenu langSubMenu = new JMenu(_("settings.language"));
		langSubMenu.setMnemonic(_("settings.language.key").charAt(0));
		langSubMenu.setIcon(Icons.getIcon22(Icons.LANGUAGE));
		setup.add(langSubMenu);
		JMenuItem langEngItem = new JMenuItem(_("settings.language.english"));
		langEngItem.setMnemonic(_("settings.language.english.key").charAt(0));
		langEngItem.setIcon(Icons.getIcon22(Icons.ENGLISH));
		langSubMenu.add(langEngItem);
		JMenuItem langFreItem = new JMenuItem(_("settings.language.french"));
		langFreItem.setMnemonic(_("settings.language.french.key").charAt(0));
		langFreItem.setIcon(Icons.getIcon22(Icons.FRENCH));
		langSubMenu.add(langFreItem);
		JMenuItem langDeuItem = new JMenuItem(_("settings.language.german"));
		langDeuItem.setMnemonic(_("settings.language.german.key").charAt(0));
		langDeuItem.setIcon(Icons.getIcon22(Icons.GERMAN));
		langSubMenu.add(langDeuItem);
		JMenuItem langItalItem = new JMenuItem(_("settings.language.italian"));
		langItalItem.setMnemonic(_("settings.language.italian.key").charAt(0));
		langItalItem.setIcon(Icons.getIcon22(Icons.ITALIAN));
		langSubMenu.add(langItalItem);
		JMenuItem langDutchItem = new JMenuItem(_("settings.language.dutch"));
		langDutchItem.setMnemonic(_("settings.language.dutch.key").charAt(0));
		langDutchItem.setIcon(Icons.getIcon22(Icons.DUTCH));
		langSubMenu.add(langDutchItem);
		
		offlineMenuItem.addActionListener(
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if (offlineMenuItem.isSelected()) {
							KKMulticopterFlashTool.setOfflineMode(true);
						} else {
							KKMulticopterFlashTool.setOfflineMode(false);
						}
						parent.firePropertyChange(KKMulticopterFlashTool.MODE_CHANGED, 0, 1);
					}
				}
			);
		
		offlineDownloadMenuItem.addActionListener(
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						FirmwareDownloadFrame fdf = new FirmwareDownloadFrame(parent.getFirmwares());
					}
				}
			);
		
		langDeuItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(Locale.GERMANY)){
							parent.setLocale("de", "DE");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langItalItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(Locale.ITALY)){
							parent.setLocale("it", "IT");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langEngItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(Locale.US)){
							parent.setLocale("en","US");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langFreItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(Locale.FRANCE)){
							parent.setLocale("fr","FR");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langDutchItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("nl", "NL"))){
							parent.setLocale("nl","NL");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
	}
}
