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 {

    /**
     * logger.
     */
    private Logger log = Logger.getLogger(getClass().getName());
    /**
     * Liste of satellite.
     */
    private AmsatList amsatList = null;
    /**
     * Liste of satellite.
     */
    private 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(final 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

        try {
            mrs.marshal(amsatList, new FileOutputStream(fichOut));
        } catch (FileNotFoundException e) {
            log.severe("File not found exeption");

        } catch (JAXBException e) {
            log.severe(e.toString());

        }
    }

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

        try {
            jaxbContext = JAXBContext
                    .newInstance("org.josast.AmsatList.generated");

        } catch (JAXBException e) {

            log.severe("JAXBException " + e.toString());
        }

        Unmarshaller unmarshaller = null;
        try {
            unmarshaller = jaxbContext.createUnmarshaller();
        } 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
     * @return number of satellite.
     */

    public int merge(final String repository) {
        File repertoire = new File(repository);
        int i = 0;
        if (!repertoire.isDirectory()) {
            log.severe(repository + "is not a repository");
            return -1;
        }
        File[] inputsfiles = repertoire.listFiles();
        if (inputsfiles == null) {
            log.severe(repository + "Repository empty");
            return -1;
        } else {
            for (i = 0; i < inputsfiles.length; i++) {
                appendSatellites(inputsfiles[i]);
            }
        }
        log.info("Nb Satellite" + i);

        return i;

    }

}
