package com.fractalite.amad_ndc.convertersRQ;

import java.util.List;
import java.util.ArrayList;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Date;
import java.util.GregorianCalendar;


import org.springframework.stereotype.Component;
import com.amadeus.xml._2010._06.travel_ordercreaterq_v1.*;
import com.fractalite.mercure.data.model.bo.Order;
import com.fractalite.mercure.data.model.common.Document;
import com.fractalite.mercure.data.model.person.NaturalPerson;
import com.fractalite.mercure.data.model.person.Person;
import com.fractalite.mercure.data.model.bo.Item;


@Component
public class AMA_TravelOrderCreateRQ {
	
	public AMATravelOrderCreateRQ buildAMA_TravelOrderCreateRQ(Order order) throws Exception {
		
		AMATravelOrderCreateRQ amaOrder=new AMATravelOrderCreateRQ();
		amaOrder.setParty(null);
		amaOrder.getRequest();
		
		RequestType requestType= new RequestType();
		requestType.setDataLists(null);
		requestType.getCreateOrder();
		DataListsType dataListsType=new DataListsType();
		dataListsType.setPaxList(buildListPax(order));
		
		PaxListType paxListType=new PaxListType();
		
		
		
		return null;
		
	}
	
	
	public CreateOrderType buildCreateOrder(Order order) {
		
		CreateOrderType createOrderType=new CreateOrderType();
		   for(Item item :order.getItems()) {
			   
				 if (item.getProductType().getCode().equals("amad_NDC")) {
					 SelectedOfferType selectedOfferType=new SelectedOfferType();
					 selectedOfferType.setOfferID(item.getRef());
					 selectedOfferType.setOwnerCode(item.getSupplier().getName());
					 createOrderType.getSelectedOffer().add(null);
					 
				 }
		   }
				 
		return null;
	}

	
	public PaxListType  buildListPax( Order order) throws Exception {
		List<PaxType> listPax = new ArrayList<PaxType>();
		PaxListType paxListType =new PaxListType();
	   for(Item item :order.getItems()) {
		   
			 if (item.getProductType().getCode().equals("amad_NDC")) {
				 
				    if(item.getBeneficiaries()!=null) {
						for(Person beneficiary : item.getBeneficiaries()) {
							PaxType pax=new PaxType(); 

				NaturalPerson person = (NaturalPerson) beneficiary;
				if(person.getRef().contains("ADT")) {

				    pax.setPTC("ADT");
				
				 }
				List<ContactInfoType> contactInfoTypeList =new ArrayList<ContactInfoType>();
			//	List<EmailAddressType> emailAddressTypeList =new ArrayList<EmailAddressType>();
				ContactInfoType contactInfoType=new ContactInfoType();
				EmailAddressType emailAddressType= new EmailAddressType();
				emailAddressType.setEmailAddressText(order.getCustomer().getEmails().get(0).getAddress());
				//emailAddressTypeList.add(emailAddressType);
				contactInfoType.getEmailAddress().add(emailAddressType);
				pax.getContactInfo().add(contactInfoType);
				pax.setBirthdate(DateToXMLGregorianCalendar(person.getDob()));
				
				IndividualType individualType=new IndividualType();
				individualType.setTitleName(null);
				individualType.setSurname(null);
				pax.setIndividual(null);
				
				for(Document doc:person.getDocuments()) {
					if(doc.getType().getName().contains("Passeport")) {
					IdentityDocType IdentityDoc= new IdentityDocType();
					IdentityDoc.setIdentityDocID(doc.getCode());
					IdentityDocTypeCodeType identityDocTypeCodeType= new IdentityDocTypeCodeType();
					identityDocTypeCodeType.setValue("PT");
					IdentityDoc.setIdentityDocTypeCode(identityDocTypeCodeType);
					CountryCodeType countryCodeType=new CountryCodeType();
					countryCodeType.setValue(doc.getCountry().getInternalCode());
					IdentityDoc.setIssuingCountryCode(countryCodeType);
					
				        
					IdentityDoc.setExpiryDate(DateToXMLGregorianCalendar(doc.getExpire()));
					
					pax.getIdentityDoc().add(IdentityDoc);
				  }	
				}
				
				 paxListType.getPax().add(pax);
				listPax.add(pax);
				
				
			  }

		   
	       }
		
		


	     }
	    }
	  
		return paxListType;

	 }
	    public static XMLGregorianCalendar DateToXMLGregorianCalendar(Date date) throws Exception {
	        GregorianCalendar gc = new GregorianCalendar();
	        gc.setTime(date);

	        return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
	    }	 

}
