package de.lazyzero.kkMulticopterFlashTool.gui;

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

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.concord.swing.SpringUtilities;

import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import de.lazyzero.kkMulticopterFlashTool.KKMulticopterFlashTool;
import de.lazyzero.kkMulticopterFlashTool.utils.Firmware;
import de.lazyzero.kkMulticopterFlashTool.utils.XmlReaderFirmwares;
import de.lazyzero.kkMulticopterFlashTool.utils.download.Download;
import de.lazyzero.kkMulticopterFlashTool.utils.download.DownloadsTableModel;
import de.lazyzero.kkMulticopterFlashTool.utils.download.ProgressRenderer;

public class FirmwareDownloadFrame extends JDialog implements ActionListener, ChangeListener{
	private static final long serialVersionUID = 1L;
	private Vector<Firmware> firmwares;
	private JPanel panel;
	private CellConstraints cc;
	private Dimension preferredSize = new Dimension(360, 500);
	private DownloadsTableModel downloadTableModel;
	private JTable table;
	private JPanel downloadsPanel;
	private JButton closeButton;
	private JButton downloadButton;
	private HashMap<String, Boolean> firmwareTyps;

	public FirmwareDownloadFrame(Vector<Firmware> firmwares) {
		this.firmwares = firmwares;
		initGUI();
		
		this.pack();
		this.setModal(true);
		this.setVisible(true);
	}

	private void initGUI() {
		panel = new JPanel();
		this.setPreferredSize(preferredSize);
		this.setMaximumSize(preferredSize);


		// create the CellContraints
		cc = new CellConstraints();

		// create the Layout for Panel this
		String panelColumns = "3dlu,fill:pref:grow,3dlu,pref,3dlu,pref,3dlu";
		String panelRows = "3dlu,pref:grow,3dlu,fill:pref,3dlu,pref,3dlu";
		FormLayout panelLayout = new FormLayout(panelColumns, panelRows);
		panel.setLayout(panelLayout);
		
		downloadTableModel = new DownloadsTableModel();
        table = new JTable(downloadTableModel);
        
        // Set up ProgressBar as renderer for progress column.
        ProgressRenderer renderer = new ProgressRenderer(0, 100);
        renderer.setStringPainted(true); // show progress text
        table.setDefaultRenderer(JProgressBar.class, renderer);
        table.setForeground(Color.WHITE);
        
        // Set table's row height large enough to fit JProgressBar.
        table.setRowHeight((int) renderer.getPreferredSize().getHeight());
        
        
        // Set up downloads panel.
        downloadsPanel = new JPanel();
        downloadsPanel.setPreferredSize(new Dimension(preferredSize.width, 200));
        downloadsPanel.setBorder(
                BorderFactory.createTitledBorder(_("downloads.downloads")));
        downloadsPanel.setLayout(new BorderLayout());
        downloadsPanel.add(new JScrollPane(table),
                BorderLayout.CENTER);
        
        closeButton = new JButton(_("close"));
        closeButton.addActionListener(this);
        
        downloadButton = new JButton(_("downloads.download"));
        downloadButton.addActionListener(this);
        
        panel.add(getFirmwareTypePanel(), cc.xyw(2, 2, 5));
        panel.add(downloadsPanel, cc.xyw(2, 4, 5));
        panel.add(downloadButton, cc.xy(4, 6));    
        panel.add(closeButton, cc.xy(6, 6));    
        
        this.add(panel, BorderLayout.CENTER);
		this.setTitle(_("downloads.title"));
		
		
	}

	private JPanel getFirmwareTypePanel() {
		JPanel ftp = new JPanel();
		SpringLayout sl = new SpringLayout();
		ftp.setLayout(sl);
		ftp.setBorder(new TitledBorder(_("firmware")));
		
		firmwareTyps = new HashMap<String,Boolean>();
		Enumeration<Firmware> en = firmwares.elements();
		while (en.hasMoreElements()) {
			Firmware firmware = (Firmware) en.nextElement();
			if (!firmwareTyps.containsKey(firmware.getName())) {
				firmwareTyps.put(firmware.getName(), false);
			}
		}
		
		//sort the entries
		SortedSet<String> sortedset= new TreeSet<String>(firmwareTyps.keySet());
		
		Iterator<String> it = sortedset.iterator();
		while (it.hasNext()) {
			String type = (String) it.next();
			JCheckBox cb = new JCheckBox(type);
			cb.setSelected(firmwareTyps.get(type));
			cb.addChangeListener(this);
			ftp.add(cb);
		}
		
		SpringUtilities.makeCompactGrid(ftp,
				(firmwareTyps.size()/2), 2, //rows, cols
				2, 2,        //initX, initY
				2, 2);       //xPad, yPad
		
		
		return ftp;
	}
	
	public static void main(String[] args) {
		KKMulticopterFlashTool kk = new KKMulticopterFlashTool();
		
		XmlReaderFirmwares reader = null;
		try {
			reader = new XmlReaderFirmwares(new URL("http://lazyzero.de/_media/firmwares.xml"),new URL("http://lazyzero.de/_media/firmwares.xml"));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		FirmwareDownloadFrame fdf = new FirmwareDownloadFrame(reader.getFirmwares());
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(closeButton)) {
			this.dispose();
		}
		if (e.getSource().equals(downloadButton)) {
			Vector<URL> downloadedURLs = new Vector<URL>();
			downloadTableModel.clearAllDownloads();
			Iterator<String> it = firmwareTyps.keySet().iterator();
			while (it.hasNext()) {
				String type = it.next();
				for (int i = 0; i < firmwares.size(); i++) {
					if (firmwares.get(i).getName().equals(type) && firmwareTyps.get(type) && !downloadedURLs.contains(firmwares.get(i).getFileURL())){
						downloadTableModel.addDownload(new Download((firmwares.get(i).getZipURL()!=null ? firmwares.get(i).getZipURL() : firmwares.get(i).getFileURL())));
						downloadedURLs.add(firmwares.get(i).getFileURL());
					}
				}				
			}
		}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if (firmwareTyps.containsKey(((JCheckBox)e.getSource()).getText())){
			firmwareTyps.put(((JCheckBox)e.getSource()).getText(),((JCheckBox)e.getSource()).isSelected());
		}
	}
}
