package de.lazyzero.kkMulticopterFlashTool.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.codec.EncoderException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import de.lazyzero.kkMulticopterFlashTool.utils.xml.XMLElement;

public class XmlWriterFirmwares {

	private String uri;
	private XMLElement xmlData;
	private Vector<Firmware> firmwares;

	public XmlWriterFirmwares(String uri, Vector<Firmware> firmwares) {

		this.uri = uri + "test";
		this.firmwares = firmwares;

		try {
			xmlData = createXML();
		} catch (EncoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private XMLElement createXML() throws EncoderException {

		XMLElement xml = new XMLElement();
		xml.setName("firmwares");
		
		for (Firmware firmware : firmwares) {
			
//			<firmware name="TGY" value="2012-06-10">
			XMLElement firmwareE = new XMLElement();
			firmwareE.setName("firmware");
			firmwareE.setAttribute("name", firmware.getName());
			firmwareE.setAttribute("value", firmware.getVersion());
			
//			<author name="Simon Kirby" />
			XMLElement author = new XMLElement();
			author.setName("author");
			author.setAttribute("name", firmware.getAuthor());
			
//			<server name="mirrored by LazyZero" />
			XMLElement server = new XMLElement();
			server.setName("server");
			server.setAttribute("name", firmware.getServer());
			
//			<controller name="esc" target="1"/>
			XMLElement controller = new XMLElement();
			controller.setName("controller");
			controller.setAttribute("name", firmware.getController());
			controller.setAttribute("target", firmware.getTarget());
			
			// <svn url="svn://code.google.com/p/wii-esc/source/browse/#svn%2Frelease\low_side_pwm" name="bs_nfet.hex"/>
			XMLElement svn = new XMLElement();
			svn.setName("svn");
			svn.setAttribute("url", firmware.getSVN());
			svn.setAttribute("name", firmware.getSVNname());
			
//			<zipfile url="http://lazyzero.de/_media/modellbau/kkmulticopterflashtool/esc_firmware/tgy_2012-06-10_d8f53c2.zip" />
			XMLElement zipfile = new XMLElement();
			zipfile.setName("zipfile");
			zipfile.setAttribute("url", firmware.getZipURL());
			
//			<file url="file://tgy.hex" />
			XMLElement file = new XMLElement();
			file.setName("file");
			file.setAttribute("url", firmware.getFileURL());
			
//			<md5 value="4f94950200b77f2cb29d6055f8627f03" />
			XMLElement md5 = new XMLElement();
			md5.setName("md5");
			md5.setAttribute("value", firmware.getMD5());
			
//			<additionalOptions option="-e" />
			XMLElement additionalOptions = new XMLElement();
			additionalOptions.setName("additionalOptions");
			additionalOptions.setAttribute("option", firmware.getAdditionalOptions());
			
//			<commenturl url="http://tinyurl.com/7z9xdjq" />
			XMLElement commentURL = new XMLElement();
			commentURL.setName("commenturl");
			commentURL.setAttribute("url", firmware.getCommentURL());
			
//			<features value="5" />
			XMLElement feature = new XMLElement();
			feature.setName("feature");
			feature.setAttribute("value", firmware.getFeatures());
			
//		 <firmware name="TGY" value="2012-06-10">
//		    <author name="Simon Kirby" />
//		    <server name="mirrored by LazyZero" />
//		    <controller name="esc" target="1"/>
//		    <zipfile url="http://lazyzero.de/_media/modellbau/kkmulticopterflashtool/esc_firmware/tgy_2012-06-10_d8f53c2.zip" />
//		    <file url="file://tgy.hex" />
//		    <md5 value="4f94950200b77f2cb29d6055f8627f03" />
//		    <additionalOptions option="-e" />
//		    <commenturl url="http://tinyurl.com/7z9xdjq" />
//			<features value="5" />
//		  </firmware>
			
			
			firmwareE.addChild(author);
			firmwareE.addChild(server);
			firmwareE.addChild(controller);
			firmwareE.addChild(zipfile);
			firmwareE.addChild(file);
			firmwareE.addChild(md5);
			firmwareE.addChild(additionalOptions);
			firmwareE.addChild(commentURL);
			if (firmware.getFeatures() != -1) firmwareE.addChild(feature);
			
			xml.addChild(firmwareE);
			
		}

		return xml;
	}

	public void writeXmlFile() {

		byte[] buffer = xmlData.toString().getBytes();
		FileOutputStream fout;
		try {
			File file = new File(uri);
			if (!file.getParentFile().exists())
			    file.getParentFile().mkdirs();

			if (!file.exists()) {
				file.createNewFile();
			}
			fout = new FileOutputStream(file);
			fout.write(buffer);
			fout.flush();
			fout.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
