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 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.Satellites;

public class AmsatListFile {
	
	FlatSatList amsatListFlat = new FlatSatList();
    AmsatList amsatList = null;

    /**
     * @param filein
     * @return
     */
    public AmsatList openAmsatList(String filein) {
        File fileIn = new File(filein);
        return openAmsatList(fileIn);
    }

    /**
     * @param filein
     * @return
     */
    public AmsatList openAmsatList(File filein) {


        JAXBContext jaxbContext = null;
        try {
            jaxbContext = JAXBContext
                    .newInstance("org.josast.AmsatList.generated");
        } catch (JAXBException e) {

            e.printStackTrace();
        }

        Unmarshaller unmarshaller = null;
        try {
            unmarshaller = jaxbContext.createUnmarshaller();
        } catch (JAXBException e) {

            e.printStackTrace();
        }

        try {
            amsatList = (AmsatList) unmarshaller.unmarshal(filein);
        } catch (JAXBException e) {

            e.printStackTrace();
        }
        
        setupAmsatListFlat();

        return amsatList;

    }

    private void setupAmsatListFlat() {
        List<Satellites> listSatellite = amsatList.getSatellites();
        ListIterator<Satellites> it = listSatellite.listIterator();
        while (it.hasNext()) {
            Satellites sat = it.next();
            AmsatListFlat amSatFlat = new AmsatListFlat(sat);
            amsatListFlat.put(amSatFlat.getSatelliteNoradId(),amSatFlat);
        }

		
	}

	/**
     * @param fichOutSring
     * @param amsatList
     */
    public void save(String fichOutSring, AmsatList amsatList) {
        File fichOut = new File(fichOutSring);
        JAXBContext jc = null;
        Marshaller mrs = null;
        try {
            jc = JAXBContext.newInstance("org.josast.AmsatList.generated");
        } catch (JAXBException e) {

            e.printStackTrace();
        }

        try {
            mrs = jc.createMarshaller();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mrs.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        } catch (PropertyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // indenter

        if (fichOut != null) {
            try {
                mrs.marshal(amsatList, new FileOutputStream(fichOut));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

	public FlatSatList getAmsatListFlat() {
		return amsatListFlat;
	}

	public void setAmsatListFlat(FlatSatList amsatListFlat) {
		this.amsatListFlat = amsatListFlat;
	}

	public AmsatList getAmsatList() {
		return amsatList;
	}

	public void setAmsatList(AmsatList amsatList) {
		this.amsatList = amsatList;
	}
    
    

}
