package org.josast.ModuleSatelliteDatabase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;

import org.josast.AmsatList.generated.AmsatList;
import org.josast.AmsatList.generated.ObjectFactory;
import org.josast.AmsatList.generated.Satellites;

public class MergeAmsatListFile {

	private Logger log = Logger.getLogger(getClass().getName());
	AmsatList amsatList = null;
	List<Satellites> liste = null;

	/**
	 * create a new Amsat List structure in memory
	 */
	public MergeAmsatListFile() {
		ObjectFactory of = new ObjectFactory();
		amsatList = of.createAmsatList();
		liste = amsatList.getSatellites();
	}

	/**
	 * Save file
	 * 
	 * @param fichOutSring
	 */
	public void save(String fichOutSring) {
		File fichOut = new File(fichOutSring);
		String path = "org.josast.AmsatList.generated";
		JAXBContext jc = null;
		try {
			jc = JAXBContext.newInstance(path);
		} catch (JAXBException e) {
			log.severe("path not found : " + path);
			// e.printStackTrace();
		}
		Marshaller mrs = null;
		try {
			mrs = jc.createMarshaller();
		} catch (JAXBException e) {
			log.severe("Marshaller creation faile");
			// e.printStackTrace();
		}
		try {
			mrs.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		} catch (PropertyException e) {
			log.severe("Marshaller property exception");
			// e.printStackTrace();
		} // indenter

//		if (fichOut != null) {
			try {
				mrs.marshal(amsatList, new FileOutputStream(fichOut));
			} catch (FileNotFoundException e) {
				log.severe("File not found exeption");
//				e.printStackTrace();
			} catch (JAXBException e) {
				log.severe(e.toString());
//				e.printStackTrace();
			}
//		} 
//		else {
//			try {
//				mrs.marshal(amsatList, System.out);
//			} catch (JAXBException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}

	
	/**
	 * add data from file in memory (list)
	 * 
	 * @param filein
	 */
	public void appendSatellites(File filein) {
		log.info("Add file : " + filein.getName());
		JAXBContext jaxbContext = null;
		
		try {
			jaxbContext = JAXBContext.newInstance("org.josast.AmsatList.generated");

		} catch (JAXBException e) {
//			e.printStackTrace();
			log.severe("JAXBException "+e.toString());
		}

		Unmarshaller unmarshaller = null;
		try {
			unmarshaller = jaxbContext.createUnmarshaller();
//			unmarshaller.setValidating(true);
		} catch (JAXBException e) {
			log.severe("JAXBException "+e.toString());
			//e.printStackTrace();
		}

		AmsatList amsatliste = null;
		try {
			amsatliste = (AmsatList) unmarshaller.unmarshal(filein);
		} catch (JAXBException e) {
			log.severe("JAXBException "+e.toString());
			//e.printStackTrace();
		}

		// TODO revoir comment gérer l'append dans la liste
		
		List<Satellites> listSatellite = amsatliste.getSatellites();

		ListIterator<Satellites> it = listSatellite.listIterator();

		while (it.hasNext()) {
			Satellites sat = it.next();
			liste.add(sat);
			log.info("satellite ajouté :" + sat.getName());

		}

	}
	
	
	/**
	 * @param repository
	 * @param Fileout
	 * @return
	 */
	
	public int merge(String repository, String Fileout) {
		File repertoire = new File(repository);
		int i = 0;
		if (repertoire.isDirectory() == false) {
			log.severe(repository+"is not a repository");
//			System.err.println("not a repository");
			return -1;
		}
		File[] inputsfiles = repertoire.listFiles();
		if (inputsfiles == null) {
			log.severe(repository+"Repository empty");
//			System.err.println("Repository empty");
			return -1;
		} else {
			for (i = 0; i < inputsfiles.length; i++) {
//				System.out.println(inputsfiles[i]);
				appendSatellites(inputsfiles[i]);
			}
		}

//		save(Fileout);
		log.info("Nb Satellite" + i);
//		System.out.println("Nb Satellite" + i);

		return i;

	}
	
	
}
