package gov.wisconsin.wijis.services.protectionorder;

import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.AddressType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.BinaryType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.DocumentType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.PersonNameType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.PersonPhysicalDetailsType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.RPersonType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.ResidenceType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.TelephoneNumberType;
import protectionorder.issue.gov.ojp.it.jxdm._3_0_3.TextType;
import protectionorder.issue.gov.wisconsin.wijis.specs.schemas.court_docs.protection_order.v1_1._2007_10_19.document.PersonAliasType;
import protectionorder.issue.gov.wisconsin.wijis.specs.schemas.court_docs.protection_order.v1_1._2007_10_19.document.ProtectionOrderSubjectType;
import protectionorder.issue.gov.wisconsin.wijis.specs.schemas.court_docs.protection_order.v1_1._2007_10_19.document.RestrictionsType;


public class ProtectionOrderServiceHelperMethodsIssue {
	
	public static void processProtectionOrderSubjects (List<ProtectionOrderSubjectType> protectionOrderSubjects)
	{
		if (protectionOrderSubjects == null)
		{
			System.out.println("No Protection Order Subjects to process");
			return;
		}	
		
		for (ProtectionOrderSubjectType subject : protectionOrderSubjects)
		{
			System.out.println("Person Subject ID: " + subject.getId());
			
			//START PersonName Processing
			System.out.println("Person Subject Name:");
			processPersonName(subject.getPersonName());
			//END PersonName
			
		
			//START PersonResidence processing
			System.out.println("Person Residence:");
			processPersonResidence(subject.getResidence());
			//END PersonResidence processing
			
			//START ContactTelephoneNumber
			System.out.println("Person Telephone Number:");
			processTelephoneNumber(subject.getPrimaryContactInformation().getContactTelephoneNumber());
			
			//END
			
			//START
			System.out.println("Person Birthdate:");
			processDate(subject.getPersonBirthDate().getValue());
			//END
			
            //START
			System.out.println("Subject Phyiscal Details:");
			processPersonPhysicalDetails(subject.getPersonPhysicalDetails());
			//END
			
			//START
			System.out.println("Person Alias Information:");
			processPersonAlias(subject.getPersonAlias());
			//END
			
			//START
			System.out.println("Restrictions Information:");
			processRestrictions(subject.getProtectionOrderRestrictions());
			//END
			
			//TODO LAKSHMI START HERE!
		}	
	}
	
	public static void processTelephoneNumber(TelephoneNumberType telephoneNumberList)
	{
		if (telephoneNumberList == null)
		{
			System.out.println("No Telephone Number to process");
			return;
		}			
		
		for (String telephoneNumber : telephoneNumberList.getTelephoneNumberFullID())
		{
			System.out.println("	Telephone Number: " + telephoneNumber);
		}
		
	}
	
	public static void processProtectionOrderRestrictedPerson(RPersonType restrictedPerson)
	{
		if (restrictedPerson == null)
		{
			System.out.println("No Restricted Person to process");
			return;
		}	
		
		//Process person name
		processPersonName(restrictedPerson.getPersonName());
		
		//Process person residence
		System.out.println("Restricted Person residence info:");
		processPersonResidence(restrictedPerson.getResidence());
		
		//Process person birth date
		System.out.println("Restricted Person birthdate:");
		processDate(restrictedPerson.getPersonBirthDate().getValue());
		
		//Process person physical details
		System.out.println("Restricted Person Physical Details:");
		//This field is an enumeration
		System.out.println("	Person Race Code: " + restrictedPerson.getPersonPhysicalDetails().getPersonRaceCode().getValue().toString());
		System.out.println("	Person Race Text: " + restrictedPerson.getPersonPhysicalDetails().getPersonRaceText().getValue());
		
		System.out.println("	Person Sex Code: " + restrictedPerson.getPersonPhysicalDetails().getPersonSexCode().getValue().toString());
		System.out.println("	Person Sex Type: " + restrictedPerson.getPersonPhysicalDetails().getPersonSexText().getValue());
		
	}
	
	public static void processPersonResidence(ResidenceType residence)
	{
		if (residence == null)
		{	
			System.out.println("No residence to process.");
		}	
		
		for (AddressType address : residence.getLocationAddress()) {
			System.out.println("	Location Street: " + address.getLocationStreet().getStreetFullText().getValue());
			
			for (TextType secondaryUnitText : address.getLocationSecondaryUnitText()) {
				System.out.println("	Secondary Unit Value: " + secondaryUnitText.getValue());				
			}
			
			System.out.println("	Location City Name: " + address.getLocationCityName().getValue());
			
			//This is an enumerated value
			System.out.println("	Location State: " + address.getLocationStateCodeUSPostalService().getValue().toString());
			System.out.println("	Location Postal Code ID: " + address.getLocationPostalCodeID().getID());
			
		}
		
	}

	
	/**
	 * This helper methods processes an XML Gregorian Calendar date 
	 * instance. It simply prints out the month day and year.
	 * @param date
	 */
	
	public static void processDate(XMLGregorianCalendar date)
	{
		if (date != null)
		{	
			System.out.println("Date Value: " + date.getMonth() + "/" + date.getDay() + "/" + date.getYear());
		}	
	}
	
	/**
	 * This helper method processes a person name and prints out all the elements and
	 * attributes in a person name.
	 * @param personName
	 */
	
	public static void processPersonName(PersonNameType personName)
	{
		if (personName == null)
		{
			System.out.println("No Person to process");
			return;
		}	
		
		System.out.println("	Given Name: " + personName.getPersonGivenName().getValue());
        for (TextType text : personName.getPersonMiddleName()) {
        	System.out.println("	Middle Name: " + text.getValue());
        }
        if (personName.getPersonSurName().getValue() != null)
        {	
        	System.out.println("	Sur Name: " + personName.getPersonSurName().getValue());
        }
        
        if (personName.getPersonSuffixName() != null)
        {	
        	System.out.println("	Suffix Name: " + personName.getPersonSuffixName().getValue());
        }	
	}
	
	/**
	 * This helper method processes a Subject Physical Details and prints out all the elements .
	 * @param personPhysicalDetails
	 */
	
	public static void processPersonPhysicalDetails(PersonPhysicalDetailsType personPhysicalDetails )
	{
		if (personPhysicalDetails == null)
		{
			System.out.println("No Subject Person Physical Details to process");
			return;
		}	
		
		if (personPhysicalDetails.getPersonHeightMeasure() != null)
    	{	
    		System.out.println("	Height: " + personPhysicalDetails.getPersonHeightMeasure().getValue().intValue());
    	}	

    	if (personPhysicalDetails.getPersonWeightMeasure() != null)
    	{	
    		System.out.println("	Weight: " + personPhysicalDetails.getPersonWeightMeasure().getValue().intValue());
    	}	
        
        //This is an enumerated value
        System.out.println("	Eye Color Code: " + personPhysicalDetails.getPersonEyeColorCode().getValue().toString());

        //This is the corresponding text value to the enumeration above
        System.out.println("	Eye Color: " + personPhysicalDetails.getPersonEyeColorText().getValue());

        //This is an enumerated value
        System.out.println("	Hair Color Code: " + personPhysicalDetails.getPersonHairColorCode().getValue().toString());

        //This is the corresponding text value to the enumeration above
        System.out.println("	Hair Color: " + personPhysicalDetails.getPersonHairColorText().getValue());
        
        //This is an enumerated value
        System.out.println("	Sex Code: " +personPhysicalDetails.getPersonSexCode().getValue().toString());

        //This is the corresponding text value to the enumeration above
        System.out.println("	Sex: " + personPhysicalDetails.getPersonSexText().getValue());
        
        //This is an enumerated value
        System.out.println("	Race Code: " + personPhysicalDetails.getPersonRaceCode().getValue().toString());

        //This is the corresponding text value to the enumeration above
        System.out.println("	Race: " + personPhysicalDetails.getPersonRaceText().getValue());
	}
	
	/**
	 * This helper method processes a Person Alias and prints out all the elements and
	 * attributes in a person name.
	 * @param personAliasTypes
	 */
	
	public static void processPersonAlias(List<PersonAliasType> personAliasTypes)
	{
		if (personAliasTypes == null)
		{
			System.out.println("No Person Alias to process");
			return;
		}
		for(PersonAliasType personAlias: personAliasTypes)
		{   
			if(personAlias.getAliasCode() != null)
			{
				System.out.println("	Code: " +personAlias.getAliasCode().value().toString());
			}
			
			if(personAlias.getAliasCodeDescription() != null)
			{
				System.out.println("	Code Description: " +personAlias.getAliasCodeDescription().getValue());
			}
        	processPersonName(personAlias.getPersonName());
		}
	}
	
	/**
	 * This helper method processes a Restrictions and prints out all the elements and
	 * attributes.
	 * @param restrictionType
	 */
	public static void processRestrictions(RestrictionsType  restrictionType)
	{
		if (restrictionType == null)
		{
			System.out.println("No Restriction to process");
			return;
		}
		if(restrictionType.getPersonFirearmProhibitedCode() != null)
		{
			System.out.println("	Firearm Prohibited Code: "+restrictionType.getPersonFirearmProhibitedCode().value().toString());
		}
		
		if(restrictionType.getMiscellaneousText() != null)
		{
			System.out.println("	Miscellaneous Text: "+restrictionType.getMiscellaneousText().getValue());
		}
	}
	
	/**
	 * This helper method processes ProtectionOrder Documents and prints out all the elements and 
	 * attributes in the ProtectionOrder Documents.  This element is a child of the ProtectionOrder Issue Type
	 * and should be not be confused with the main ProtectionOrder Issue Document.  The documents that
	 * are processed here are binary documents that are related to the ProtectionOrder.
	 * @param documentList
	 */
	public static void processProtectionOrderDocument(List<DocumentType> documentList)
	{
		for (DocumentType protectionOrderDocument : documentList) {
        	for (BinaryType binaryType : protectionOrderDocument.getDocumentBinary()) {
        		
        		System.out.println("Protection Order Document Binary Type:");
        		
        		System.out.println("Binary ID: " + binaryType.getBinaryID().getID().getValue());
        		
        		//Printing this to the console probably isn't helpful, 
        		//so here it is as a byte array which you can do whatever you like with 
        		byte[] base64BinaryVar = binaryType.getBinaryObjectBase64().getValue();
        		
        		System.out.println("Binary Description Text: " + binaryType.getBinaryDescriptionText().getValue());
        		System.out.println("Binary Reference ID: " + binaryType.getBinaryReferenceID().getID().getValue());
        		
        		
        	}
        }
	}
}
