package gov.wisconsin.wijis.services.protectionorder;

import gov.wisconsin.wijis.services.protectionorder.NotificationResponseType;
import gov.wisconsin.wijis.services.protectionorder.ProtectionOrder;
import gov.wisconsin.wijis.services.protectionorder.SubmitFaultMessage;

import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.XMLGregorianCalendar;

import org.w3c.dom.Document;

import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.AddressType;
import protectionorder.issue.gov.wisconsin.wijis.specs.schemas.court_docs.protection_order.v1_1._2007_10_19.document.ProtectionOrderDocumentType;
import protectionorder.issue.gov.wisconsin.wijis.specs.schemas.court_docs.protection_order.v1_1._2007_10_19.document.ProtectionOrderType;

import com.sun.org.apache.xerces.internal.dom.ElementNSImpl;



public class ProtectionOrderServiceImpl implements ProtectionOrder {

	public NotificationResponseType notify(Object notifyParameters) {
		throw new UnsupportedOperationException("This service is not implemented in this distribution");
	}

	public Object submit(Object submitParameters) throws SubmitFaultMessage {

		JAXBContext JaxBContextLocalVar = null;
		JAXBElement<ProtectionOrderDocumentType> protectionOrderInputDocument = null;
		ProtectionOrderType protectionOrderInput = null;
		
		//Create a new JaxB instance and set up an unmarshaller
		try {
			JaxBContextLocalVar = JAXBContext.newInstance("protectionorder.issue.gov.wisconsin.wijis.specs.schemas.court_docs.protection_order.v1_1._2007_10_19.document");
			Unmarshaller unmarshaller = JaxBContextLocalVar.createUnmarshaller() ;
			
			//Unmarshall the input paramater into a ProtectionOrderInputDocument
			protectionOrderInputDocument = unmarshaller.unmarshal(((ElementNSImpl) submitParameters).getFirstChild(), 
	                ProtectionOrderDocumentType.class);
			
			System.out.println("Protection Order Document ID:" + protectionOrderInputDocument.getValue().getId());
			
	        //Get the ProtectionOrder out of the ProtectionOrder input document
			protectionOrderInput = new ProtectionOrderType();
			protectionOrderInput = protectionOrderInputDocument.getValue().getProtectionOrder();

	        //Get the ProtectionOrder ID attribute
	        System.out.println("Protection Order ID: " + protectionOrderInput.getId());

	        //START ProtectionOrder CourtOrderDesignatedLocation Processing
	        //Since this XML element allows for max occurrences greater than 1, JaxB maps it to a list
	        //The list is iterated in a for loop and all the elements are printed out.  This patters in repeated
	        //for other unbounded XML elements.
	        List<AddressType> addressTypeList = protectionOrderInput.getCourtOrderDesignatedLocation().getLocationAddress();
	        System.out.println("Court Order Designated Location:");
	        
	        for (AddressType addressType : addressTypeList) {
	        	System.out.println("County Name: " + addressType.getLocationCountyName().getValue());
	        	System.out.println("Location County Code: " + addressType.getLocationCountyCode().getValue());
	        }
	        //END ProtectionOrder CourtOrderDesignatedLocation

	        //START ProtectionOrder CourtOrderIssuingJudicialOfficial Processing
	        System.out.println("Judicial Official Name:");
	        //Multiple person names are processed throughout the code so a helper method processes the name
	        ProtectionOrderServiceHelperMethodsIssue.processPersonName(protectionOrderInput.getCourtOrderIssuingJudicialOfficial().getPersonName());
	        //END ProtectionOrder CourtOrderIssuingJudicialOfficial	        
	        
	        //START ProtectionOrder CourtOrderIssuingDate Processing
	        XMLGregorianCalendar issuingDate = protectionOrderInput.getCourtOrderIssuingDate().getValue();
	        System.out.println("Issuing Date:");
	        //Multiple dates are processed throughout the code a helper method processes the date 
	        ProtectionOrderServiceHelperMethodsIssue.processDate(issuingDate);
	        //END ProtectionOrder CourtOrderIssuingDate
	        
	        //START ProtectionOrderRestrictedPerson Processing
	        System.out.println("Restricted Person:");
	        ProtectionOrderServiceHelperMethodsIssue.processProtectionOrderRestrictedPerson(protectionOrderInput.getProtectionOrderRestrictedPerson());
	        //END ProtectionOrderRestrictedPerson
	        
	        //START CASE CaseDocketID Processing
	        System.out.println("Case Docket ID: " + protectionOrderInput.getCase().getCaseDocketID().getID());
	        //END CaseDocketID

	        //START ProtectionOrder CourtOrderExpirationDate Processing
	        XMLGregorianCalendar expirationDate = protectionOrderInput.getCourtOrderExpirationDate();
	        System.out.println("Expiration Date:");
	        //Multiple dates are processed throughout the code a helper method processes the date 
	        ProtectionOrderServiceHelperMethodsIssue.processDate(expirationDate);
	        //END ProtectionOrder CourtOrderExpirationDate
	        
	        //START ProtectionOrderStatus Processing
	        //This field is an enumerated value
	        System.out.println("Protection Order Status Code: " + protectionOrderInput.getProtectionOrderStatus().getProtectionOrderAction().toString());
	        System.out.println("Protection Order Status Value: " + protectionOrderInput.getProtectionOrderStatus().getProtectionOrderActionDescription().getValue());
	        //END ProtectionOrderStatus Processing
	        
	        //START ProtectionOrderClassCode Processing
	        System.out.println("Protection Order Class Code: " + protectionOrderInput.getProtectionOrderClassCode());
	        System.out.println("Protection Order Class Code Description: " + protectionOrderInput.getProtectionOrderClassCodeDescription().getValue());
	        //END ProtectionOrderClassCode Processing

	        //START ProtectionOrderCategory Processing
	        //This field is an enumerated value
	        System.out.println("Protection Order Category: " + protectionOrderInput.getProtectionOrderCategory().toString());
	        System.out.println("Protection Order Category Description: " + protectionOrderInput.getProtectionOrderCategoryDescription().getValue());
	        //End ProtectionOrderCategory Processing
	        
	        //START ProtectionOrderSubject Processing
	        ProtectionOrderServiceHelperMethodsIssue.processProtectionOrderSubjects(protectionOrderInput.getProtectionOrderSubject());
	        //END ProtectionOrderSubject Processing
	        
	        //START WarrantDocument
	        ProtectionOrderServiceHelperMethodsIssue.processProtectionOrderDocument(protectionOrderInput.getProtectionOrderDocument());
	        //END WarrantDocument
	        
		} catch (JAXBException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
            SubmitFaultMessage sfm = new SubmitFaultMessage("Message submission failed, please try again.",e);
		}
			
		//Create a Document that will hold the response document
		Document document = null;	
		
		//Call a helper method that helps creates the response
		document = ProtectionOrderServiceHelperMethodsResponse.createResponseMessage(protectionOrderInputDocument.getValue());
		
		return document.getDocumentElement();
	}

}

