package de.lazyzero.kkMulticopterFlashTool.gui;

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

import java.awt.BorderLayout;
import java.awt.Desktop;
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.net.URI;
import java.net.URISyntaxException;
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;
import de.lazyzero.kkMulticopterFlashTool.utils.SendLogFile;
import de.lazyzero.kkMulticopterFlashTool.utils.XmlReaderFirmwares;

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

	public kkMenu(KKMulticopterFlashTool parent, XmlReaderFirmwares firmwareReader) {
		this.parent = parent;
		this.firmwareReader = firmwareReader;
		
		initSetupMenu();
		initHelpMenu();
		initInfoMenu();
		initContributeMenu();
				
		bar = new JMenuBar();
		bar.add(setup);
		bar.add(contribute);
		bar.add(help);
		bar.add(info);
	}

	public JMenuBar getBar() {
		return bar;
	}
	
	private void initContributeMenu() {
		contribute = new JMenu(_("contribute"));
		contribute.setMnemonic(_("contribute.key").charAt(0));
		
		JMenuItem mailItem = new JMenuItem(_("contribute.mail"));
		mailItem.setMnemonic(_("contribute.mail.key").charAt(0));
		mailItem.setIcon(Icons.getIcon22(Icons.MAIL));
		contribute.add(mailItem);
		
		JMenuItem submitFirmwareItem = new JMenuItem(_("contribute.submitFirmware"));
		submitFirmwareItem.setMnemonic(_("contribute.submitFirmware.key").charAt(0));
		submitFirmwareItem.setIcon(Icons.getIcon22(Icons.MAIL));
		contribute.add(submitFirmwareItem);
		
		submitFirmwareItem.addActionListener(
				new ActionListener(){
					

					public void actionPerformed(ActionEvent e) {
						FirmwareCommitForm fcf = new FirmwareCommitForm(kkMenu.this.firmwareReader);
						fcf.sendMail();
					}
				}
		);
		
		mailItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						SendLogFile slf = new SendLogFile();
						slf.sendMail(KKMulticopterFlashTool.VERSION, KKMulticopterFlashTool.LOG_FILE);
					}
				}
		);
	}

	private void initHelpMenu() {
		help = new JMenu(_("help"));
		help.setMnemonic(_("help.key").charAt(0));
		
		
		JMenuItem KKhomeItem = new JMenuItem(_("help.kkhome"));
		KKhomeItem.setMnemonic(_("help.kkhome.key").charAt(0));
		KKhomeItem.setIcon(Icons.getIcon22(Icons.app));
		help.add(KKhomeItem);
		
		JMenuItem KRManualItem = new JMenuItem(_("help.krManual"));
		KRManualItem.setMnemonic(_("help.krManual.key").charAt(0));
		KRManualItem.setIcon(Icons.getIcon22(Icons.KR));
		help.add(KRManualItem);
		
		help.addSeparator();
		
		JMenuItem helpItem = new JMenuItem(_("help.help"));
		helpItem.setMnemonic(_("help.help.key").charAt(0));
		helpItem.setIcon(Icons.getIcon22(Icons.LZ));
		help.add(helpItem);
		
		
		
		
		
		KRManualItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						openURL("http://www.kkmulticopter.kr/index.html?modea=manual");
					}
				}
		);
		
		KKhomeItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						openURL("http://www.kkmulticopter.com/index.php?option=com_content&view=article&id=55&Itemid=57");
					}
				}
		);
		
		helpItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						openURL("http://www.lazyzero.de/en/modellbau/kkmulticopterflashtool");
					}
				}
		);
		
		
	}
	
	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.getIconFlag(Icons.ENGLISH));
		langSubMenu.add(langEngItem);
		JMenuItem langFreItem = new JMenuItem(_("settings.language.french"));
		langFreItem.setMnemonic(_("settings.language.french.key").charAt(0));
		langFreItem.setIcon(Icons.getIconFlag(Icons.FRENCH));
		langSubMenu.add(langFreItem);
		JMenuItem langDeuItem = new JMenuItem(_("settings.language.german"));
		langDeuItem.setMnemonic(_("settings.language.german.key").charAt(0));
		langDeuItem.setIcon(Icons.getIconFlag(Icons.GERMAN));
		langSubMenu.add(langDeuItem);
		JMenuItem langItalItem = new JMenuItem(_("settings.language.italian"));
		langItalItem.setMnemonic(_("settings.language.italian.key").charAt(0));
		langItalItem.setIcon(Icons.getIconFlag(Icons.ITALIAN));
		langSubMenu.add(langItalItem);
		JMenuItem langDutchItem = new JMenuItem(_("settings.language.dutch"));
		langDutchItem.setMnemonic(_("settings.language.dutch.key").charAt(0));
		langDutchItem.setIcon(Icons.getIconFlag(Icons.DUTCH));
		langSubMenu.add(langDutchItem);
		JMenuItem langChineseItem = new JMenuItem(_("settings.language.chinese"));
		langChineseItem.setMnemonic(_("settings.language.chinese.key").charAt(0));
		langChineseItem.setIcon(Icons.getIconFlag(Icons.CHINESE));
		langSubMenu.add(langChineseItem);
		JMenuItem langTurkishItem = new JMenuItem(_("settings.language.turkish"));
		langTurkishItem.setMnemonic(_("settings.language.turkish.key").charAt(0));
		langTurkishItem.setIcon(Icons.getIconFlag(Icons.TURKISH));
		langSubMenu.add(langTurkishItem);
		JMenuItem langFaroeseItem = new JMenuItem(_("settings.language.faroese"));
		langFaroeseItem.setMnemonic(_("settings.language.faroese.key").charAt(0));
		langFaroeseItem.setIcon(Icons.getIconFlag(Icons.FAROESE));
		langSubMenu.add(langFaroeseItem);
		JMenuItem langDanishItem = new JMenuItem(_("settings.language.danish"));
		langDanishItem.setMnemonic(_("settings.language.danish.key").charAt(0));
		langDanishItem.setIcon(Icons.getIconFlag(Icons.DANISH));
		langSubMenu.add(langDanishItem);
		JMenuItem langSwedishItem = new JMenuItem(_("settings.language.swedish"));
		langSwedishItem.setMnemonic(_("settings.language.swedish.key").charAt(0));
		langSwedishItem.setIcon(Icons.getIconFlag(Icons.SWEDISH));
		langSubMenu.add(langSwedishItem);
		JMenuItem langBrazilianItem = new JMenuItem(_("settings.language.brazilian"));
		langBrazilianItem.setMnemonic(_("settings.language.brazilian.key").charAt(0));
		langBrazilianItem.setIcon(Icons.getIconFlag(Icons.BRAZILIAN));
		langSubMenu.add(langBrazilianItem);
		JMenuItem langPolishItem = new JMenuItem(_("settings.language.polish"));
		langPolishItem.setMnemonic(_("settings.language.polish.key").charAt(0));
		langPolishItem.setIcon(Icons.getIconFlag(Icons.POLISH));
		langSubMenu.add(langPolishItem);
		JMenuItem langSpanishItem = new JMenuItem(_("settings.language.spanish"));
		langSpanishItem.setMnemonic(_("settings.language.spanish.key").charAt(0));
		langSpanishItem.setIcon(Icons.getIconFlag(Icons.SPANISH));
		langSubMenu.add(langSpanishItem);
		JMenuItem langRussianItem = new JMenuItem(_("settings.language.russian"));
		langRussianItem.setMnemonic(_("settings.language.russian.key").charAt(0));
		langRussianItem.setIcon(Icons.getIconFlag(Icons.RUSSIAN));
		langSubMenu.add(langRussianItem);
		JMenuItem langPortugeseItem = new JMenuItem(_("settings.language.portugese"));
		langPortugeseItem.setMnemonic(_("settings.language.portugese.key").charAt(0));
		langPortugeseItem.setIcon(Icons.getIconFlag(Icons.PORTUGESE));
		langSubMenu.add(langPortugeseItem);
		
		
		
		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);
							}
						}
					}
				}
			);
		
		langChineseItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("zh", "CN"))){
							parent.setLocale("zh","CN");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langTurkishItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("tr", "TR"))){
							parent.setLocale("tr", "TR");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langFaroeseItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("fo", "FO"))){
							parent.setLocale("fo", "FO");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langDanishItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("da", "DK"))){
							parent.setLocale("da", "DK");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langBrazilianItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("pt", "BR"))){
							parent.setLocale("pt", "BR");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langPolishItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("pl", "PL"))){
							parent.setLocale("pl", "PL");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langSwedishItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("se", "SE"))){
							parent.setLocale("se", "SE");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langSpanishItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("es", "ES"))){
							parent.setLocale("es", "ES");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langRussianItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("ru", "RU"))){
							parent.setLocale("ru", "RU");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
		langPortugeseItem.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if (!parent.getLocale().equals(new Locale("pt", "PT"))){
							parent.setLocale("pt", "PT");
							if (parent.restart()){
								System.exit(0);
							}
						}
					}
				}
			);
		
	}
	
	private void openURL(String url) {
		try {
			Desktop.getDesktop().browse(new URI(url));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
