package com.fractalite.amad_ndc.convertersRQ;

import java.math.BigDecimal;
import java.util.List;


import com.amadeus.xml._2010._06.travel_orderpayrq_v1.AMATravelOrderPayRQ;
import com.amadeus.xml._2010._06.travel_orderpayrq_v1.AmountType;
import com.amadeus.xml._2010._06.travel_orderpayrq_v1.OrderType;
import com.amadeus.xml._2010._06.travel_orderpayrq_v1.PaymentInfoType;
import com.amadeus.xml._2010._06.travel_orderpayrq_v1.PaymentMethodType;
import com.amadeus.xml._2010._06.travel_orderpayrq_v1.RequestType;
import com.fractalite.mercure.data.model.bo.Item;
import com.fractalite.mercure.data.model.bo.Order;
import com.fractalite.mercure.data.model.bo.Price;
import com.fractalite.mercure.data.model.bo.PriceScope;

public class AMA_TravelOrderPayRQ {
	
	
	public AMATravelOrderPayRQ buildAMA_TravelOrderPayRQ(Order buildRequest) {
		AMATravelOrderPayRQ ama_TravelOrderPayRQ=new AMATravelOrderPayRQ();
		
		ama_TravelOrderPayRQ.setRequest(buildRequest(buildRequest));
		
		return ama_TravelOrderPayRQ;
	}

	
	public RequestType buildRequest(Order order) {
		RequestType requestType=new RequestType();
		requestType.getPaymentInfo().add(buildPaymentInfo(order));
		requestType.setOrder(buildOrder(order));
		return requestType;
		
	}
	
	
	public PaymentInfoType buildPaymentInfo(Order order){
		PaymentInfoType paymentInfoType=new PaymentInfoType();
		AmountType amountType=new AmountType();
		amountType.setCurCode(order.getCurrency().getCurrencyCode());
		 int value=0;
		for(Item item : order.getItems())
		{
			
			if (item.getProductType().getCode().equals("amad_NDC")) {
				
				for(Price price:item.getPrices()) {
					if(price.getScope().equals(PriceScope.PURCHASE_TOTAL_PRICE)) {
						value=value+(int)price.getValue();
						
						
					}
					
				}
			
			
		    }
		 	
		}
		
		amountType.setValue(BigDecimal.valueOf(value));
		paymentInfoType.setAmount(amountType);
		
		PaymentMethodType paymentMethodType=new PaymentMethodType();
		paymentMethodType.getCash().setCashInd(true);
		paymentInfoType.setPaymentMethod(paymentMethodType);
		
		
		return paymentInfoType;
		
	}

	
	public OrderType buildOrder(Order order){
		OrderType orderType=new OrderType();
		
		for(Item item : order.getItems())
		{
			
			if (item.getProductType().getCode().equals("amad_NDC")) {
				orderType.setOrderID(item.getNumber());
				 for(String tag : item.getItinerary().get(0).getSpaceTimePoints().get(0).getTags()) {
					 if(tag.contains("Opérateur")) {
						// String[] parts = tag.split(",")[1]; 
						String comapany = tag.split(",")[1]; 
						orderType.setOwnerCode(comapany);
					 }
						 
				 }
			
			
		    }
		 	
		}
		
		return orderType;
		
	}
}
