package com.fractalite.hermes.teldar.Marshaller;

import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.gekko_holding.webservice.Availability.AvailabilityResponse;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayInputStream;

@Component
public class UnmarshallingJAXB {

    private static final Logger logger = LoggerFactory.getLogger(UnmarshallingJAXB.class);

    public static <T> T printObject(Class<T> objectClass, String xml) {

        T rslt = null;
        SOAPMessage message = null;
        JAXBContext jaxbContext = null;
        Unmarshaller unmarshaller = null;

        try {
            message = MessageFactory.newInstance().createMessage(null,
                    new ByteArrayInputStream(xml.getBytes()));
            jaxbContext = JAXBContext.newInstance(objectClass);
            unmarshaller = jaxbContext.createUnmarshaller();
            if (message.getSOAPBody().getFirstChild().getLocalName().toLowerCase().equals(objectClass.getSimpleName().toLowerCase())) {
                rslt = (T) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
            } else {
                logger.error("Error in printObject function to set result");
//				throw new RuntimeException();
            }
            return rslt;
        } catch (Exception exception) {
            logger.error("Exception in printObject function" + exception.getMessage());
        }
        return null;
    }

    /* Availability Response Unmashaller. */
    public static AvailabilityResponse fromHotelAvailabilityResponse(String exchangeBody) {
    	AvailabilityResponse unmarshalledObject = printObject(AvailabilityResponse.class, exchangeBody);
        return unmarshalledObject;
    }
    
/*    public static GetHotelDetailsResponse fromGetHotelDetailsResponse(String exchangeBody) {
    	GetHotelDetailsResponse unmarshalledObject=printObject(GetHotelDetailsResponse.class, exchangeBody);
    	return unmarshalledObject;
    	
    }

    public static GetCitiesResponse fromGetCitiesResponse(String exchangeBody) {
        GetCitiesResponse unmarshalledObject = printObject(GetCitiesResponse.class, exchangeBody);
        return unmarshalledObject;
    }

    public static GetPreBookingInfoResponse fromPreBookingInfoResponse(String exchangeBody) {
        GetPreBookingInfoResponse unmarshalledObject = printObject(GetPreBookingInfoResponse.class, exchangeBody);
        return unmarshalledObject;
    }
    
    public static BookHotelResponse fromBookHotelResponse(String exchangeBody) {
    	BookHotelResponse unmarshalledObject=printObject(BookHotelResponse.class, exchangeBody);
    	return unmarshalledObject;
    	
    }

    public static CancelBookingSegmentResponse fromBookingCancellation(String exchangeBody) {
    	CancelBookingSegmentResponse unmarshalledObject=printObject(CancelBookingSegmentResponse.class, exchangeBody);
    	return unmarshalledObject;*/
    	
 //   }
    
}