package be.business.connector.core.handlers;

import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.log4j.Logger;
import org.w3c.dom.NodeList;

public class InsurabilityHandler implements SOAPHandler<SOAPMessageContext> {
   private static final Logger LOG = Logger.getLogger(InsurabilityHandler.class);
   private static String insurability;
   private static String messageId;

   public Set<QName> getHeaders() {
      return null;
   }

   public void close(MessageContext arg0) {
   }

   public boolean handleFault(SOAPMessageContext c) {
      this.handleMessage(c);
      return true;
   }

   public boolean handleMessage(SOAPMessageContext c) {
      try {
         Boolean outboundProperty = (Boolean)c.get("javax.xml.ws.handler.message.outbound");
         if (!outboundProperty) {
            SOAPMessage msg = c.getMessage();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            msg.writeTo(out);
            Node elementsGetPrescriptionForExecutorResponse = (Node)msg.getSOAPBody().getChildElements().next();
            NodeList elements = elementsGetPrescriptionForExecutorResponse.getChildNodes();

            for(int i = 0; i < elements.getLength(); ++i) {
               org.w3c.dom.Node element = elements.item(i);
               if (element.getLocalName() != null && (element.getLocalName().equals("GetInsurabilityForPharmacistResponse") || element.getLocalName().equals("InsurabilityResponse"))) {
                  initMessageID(msg);
                  Transformer t = TransformerFactory.newInstance().newTransformer();
                  StringWriter sw = new StringWriter();
                  t.transform(new DOMSource(element), new StreamResult(sw));
                  insurability = sw.toString();
                  break;
               }
            }
         }
      } catch (Throwable var11) {
         LOG.warn("SOAPException when retrieving insurability the message", var11);
      }

      return true;
   }

   private static void initMessageID(SOAPMessage msg) throws SOAPException {
      SOAPPart part = msg.getSOAPPart();
      if (part != null) {
         SOAPEnvelope soapEnvelope = part.getEnvelope();
         if (soapEnvelope != null) {
            SOAPHeader header = soapEnvelope.getHeader();
            if (header != null && header.getChildElements().hasNext()) {
               Node elementsResponseHeader = (Node)header.getChildElements().next();
               if (elementsResponseHeader != null && elementsResponseHeader.getLocalName() != null && elementsResponseHeader.getLocalName().equals("MessageID")) {
                  NodeList elementsheader = elementsResponseHeader.getChildNodes();
                  org.w3c.dom.Node element = elementsheader.item(0);
                  messageId = element.getNodeValue();
                  LOG.info("MyCareNet returned a response with messageId: " + messageId);
               }
            }
         }
      }

   }

   public static void setMessageId(String messageId) {
      InsurabilityHandler.messageId = messageId;
   }

   public static String getMessageId() {
      return messageId;
   }

   public static void setInsurability(String insurability) {
      InsurabilityHandler.insurability = insurability;
   }

   public static String getInsurability() {
      return insurability;
   }
}
