/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.dnetlib.espas.util;

import static eu.dnetlib.espas.util.MetadataHandler.serializeNode;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.geotoolkit.gml.xml.v321.AbstractTimeObjectType;
import org.geotoolkit.xml.XML;
import org.w3c.dom.Node;

/**
 *
 * @author gathanas
 */
public class TemporalMetadataHandler extends MetadataHandler{

    private static Logger _logger = Logger.getLogger(TemporalMetadataHandler.class);

    private static GMLTemporalTransformationHandler gmlTempHandler;

    static {
        gmlTempHandler = new GMLTemporalTransformationHandler();
    }
    
    /** Asserts whether the provided extent node has temporal extensions.  
     */
    public static boolean hasTemporalExtent(Node extentContent) throws IOException, JAXBException{
        _logger.log(Priority.INFO, "hasTemporalExtent");
        
            String strCont = serializeNode(extentContent);
            _logger.log(Priority.INFO, "Content is: \n"+strCont );
            
            InputStream strInStream = new ByteArrayInputStream(strCont.getBytes());
            Object extent =  ((JAXBElement) XML.unmarshal(strInStream)).getValue();
            if(extent!=null && ((AbstractTimeObjectType)extent).getId()!=null && !((AbstractTimeObjectType)extent).getId().isEmpty())
                return true;            
        return false;
    }
    
    
    /** Extracts a serialized representation of the temporal extent start time. To extract this information an instance of the Geotoolkit TemporalExtent 
     * class is created and used for the unmarshalling and marshalling of the provided XML input. 
     */
    public static String getTemporalExtentStart(Node temporalExContent) throws IOException{
        _logger.log(Priority.INFO, "getTemporalExtentStart");
        String output = "";

        String strCont = serializeNode(temporalExContent);
            _logger.log(Priority.INFO,"Content is: \n"+strCont );

            if(temporalExContent!=null )
                output = gmlTempHandler.getTemporalStartTime(temporalExContent);
            
            return output;
    }
 
        
    /** Extracts a serialized representation of the end timestamp. To extract this information an instance of the Geotoolkit TemporalExtent 
     * class is created and used for the unmarshalling and marshalling of the provided XML input. 
     */
    public static String getTemporalExtentEnd(Node temporalExContent) throws IOException{
        _logger.log(Priority.INFO, "getTemporalExtentEnd");
        
        String output = "";

        String strCont = serializeNode(temporalExContent);
            _logger.log(Priority.INFO,"Content is: \n"+strCont );
            
        if (temporalExContent != null) {
                output = gmlTempHandler.getTemporalEndTime(temporalExContent);
        }
         return output;
        
    }

    public static String getTimeInstant(Node timeInstantContent) throws IOException {
        _logger.log(Priority.INFO, "getTimeInstant");
        
        String output = "";

        String strCont = serializeNode(timeInstantContent);
        _logger.log(Priority.INFO,"Content is: \n"+strCont );
        
        output = gmlTempHandler.getTimeInstant(timeInstantContent);

        return output;
    }

    public static String getTimePeriodEnd(Node timePeriodType) throws IOException {
        _logger.log(Priority.INFO, "getTimePeriodEnd");
        
        String output = "";

        String strCont = serializeNode(timePeriodType);
        _logger.log(Priority.INFO,"Content is: \n"+strCont );
        
        output = gmlTempHandler.getTimePeriodEnd(timePeriodType);

        return output;
    }

    public static String getTimePeriodStart(Node timePeriodType) throws IOException {
        _logger.log(Priority.INFO, "getTimePeriodStart");
        
        String output = "";

        String strCont = serializeNode(timePeriodType);
        _logger.log(Priority.INFO,"Content is: \n"+strCont );
        
        output = gmlTempHandler.getTimePeriodStart(timePeriodType);

        return output;
    }

}
