package de.lazyzero.kkMulticopterFlashTool.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Vector;

import de.lazyzero.kkMulticopterFlashTool.KKMulticopterFlashTool;

public class FirmwareDownloadTest {
	private static URL firmwareRepositoryMirrorURL;
	private static URL firmwareRepositoryURL;
	private static XmlReaderFirmwares firmwareReader;
	private static Vector<Firmware> firmwares;

	public static void main (String[] args) {
		try {
			firmwareRepositoryURL = new URL("file://firmwares.xml");
//			firmwareRepositoryURL = new URL("http://www.lazyzero.de/_media/firmwares.xml");
			firmwareRepositoryMirrorURL = new URL("http://www.lazyzero.de/_media/firmwares.xml");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		firmwareReader = new XmlReaderFirmwares(firmwareRepositoryURL, firmwareRepositoryMirrorURL);
		firmwares = firmwareReader.getFirmwares();
		
		for (Iterator iterator = firmwares.iterator(); iterator.hasNext();) {
			Firmware firmware = (Firmware) iterator.next();
			
			String md5Calculated = "N/A";
			if (firmware.getZipURL() != null) {
				File f = downloadFile(firmware.getZipURL());
				if (null != f) {
					File fExtract = new File(firmware.getFileURL().toString().substring(7).trim());
					try {
						File file = Zip.unzip(f, fExtract);
						md5Calculated  = MD5.getMD5(file);
					} catch (Exception e) {
						System.err.println("File corrupt.");
					}
				}
			} else if (firmware.getFileURL() != null) {
				File file = downloadFile(firmware.getFileURL());
				if (null != file) {
					md5Calculated = MD5.getMD5(file);
				}
			}
			if (!firmware.getMD5().equals(md5Calculated)) System.out.println("MD5 mismatch: " + firmware.getMD5());
			System.out.println("----------------------------------------------------------------------------------------");
		}
	}
	
	private static File downloadFile(URL url){
		String filename = null;
		String tmpdir = "/tmp/kktest/";
		
		String urlPath = url.getPath();
		filename = urlPath.substring(urlPath.lastIndexOf("/")+1);
		System.out.println("Download file: "+filename);
		
        filename = tmpdir + filename;
        	
		File tmpFile = new File(filename);
		if ((new File(tmpdir)).mkdir()) System.out.println("tmpdir created");
		
		
		try {
			
			BufferedInputStream in = new BufferedInputStream(url.openStream());
			FileOutputStream fos = new FileOutputStream(tmpFile);
			BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
			byte[] data = new byte[1024];
			int x=0;
			while((x=in.read(data,0,1024))>=0) {
				bout.write(data,0,x);
			}
			bout.close();
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		System.out.println("Download finished: " + filename);
		return tmpFile;
	}
}
