package com.fractalite.amad_ndc.contollers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.amadeus.xml._2010._06.travel_offerpricerq_v1.AMATravelOfferPriceRQ;
import com.amadeus.xml.fmptbq_24_1_1a.FareMasterPricerTravelBoardSearch;
import com.fractalite.amad_ndc.convertersRQ.AMA_OfferPriceRQ;
import com.fractalite.amad_ndc.convertersRQ.Fare_MasterPricerTravelBoardSearchRQ;
import com.fractalite.amad_ndc.services.HeadersServices;
import com.fractalite.mercure.data.model.bo.Order;


@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api")
public class SearchController {
	private static final Logger LOG = LoggerFactory.getLogger(SearchController.class);
	public final Fare_MasterPricerTravelBoardSearchRQ fareMasterPricerTravelBoardSearchRQ;
	public final HeadersServices headersServices;
	public final AMA_OfferPriceRQ offerPriceRQ;
	public SearchController(Fare_MasterPricerTravelBoardSearchRQ fareMasterPricerTravelBoardSearchRQ,HeadersServices headersServices,AMA_OfferPriceRQ offerPriceRQ) {
		super();
		this.fareMasterPricerTravelBoardSearchRQ = fareMasterPricerTravelBoardSearchRQ;
		this.headersServices=headersServices;
		this.offerPriceRQ=offerPriceRQ;
	}
	
	
	   @PostMapping(path = "/offers", consumes = {"application/json;charset=UTF-8", "application/json"})
	    public String search(
	    		 @RequestParam(value = "cabin", required = false) String cabin, //ECONOMY,
	             @RequestParam(value = "ora", required = false, defaultValue = "YY") String ora,// company AF
	             @RequestParam(value = "Itinerary", required = false) String Itinerary,//CMNPAR20220626,PARCMN20220630
	             @RequestParam(value = "travellers", required = false) String travellers,//travellers=1:ADT,0:CHD,0:INF,0:YTH,0:YCD
	             @RequestParam(value = "baggage", required = false, defaultValue = "false") Boolean baggage,
	             @RequestParam(value = "direct", required = false, defaultValue = "false") Boolean direct,
	             @RequestParam(value = "rf", required = false, defaultValue = "false") Boolean rf,
	             @RequestParam(value = "ref", required = false) String ref,
	             @RequestParam(value = "sorteBy", required = false) String sorteBy,
	             @RequestParam(value = "flags", required = false) String flags,
	             @RequestParam(value = "corp", required = false) String corp,
	             @RequestParam(value = "index", required = false) String index,
	             @RequestParam(value = "split", required = false ) String split,
	             @RequestParam(value = "transactionId", required = false) String transactionId,
	             @RequestParam(value = "to" , required = false, defaultValue = "false" ) Boolean to,
	             @RequestParam(value = "sd", required = false , defaultValue = "false") Boolean sd) throws Exception {
		   
		   
		   
		   List<String> flagsRq = new ArrayList<>();
	       
	        if (flags != null) {
	            String[] flagsArray = flags.split(",");

	            boolean isDirect = Arrays.asList(flagsArray).contains("D");
	            boolean isRemboursable = Arrays.asList(flagsArray).contains("REF");
	            boolean isFlexible = Arrays.asList(flagsArray).contains("FLEX");
	            boolean withBag = Arrays.asList(flagsArray).contains("CBO");
	            boolean isTor = Arrays.asList(flagsArray).contains("to");
	            boolean isSd = Arrays.asList(flagsArray).contains("sd");
	            boolean isNego = Arrays.asList(flagsArray).contains("nego");

	            if (isNego) {
	                flagsRq.add("nego");
	            }



	            if (isSd) {
	                flagsRq.add("sd");
	            }

	            if (isDirect) {
	                flagsRq.add("direct");
	            }
	            if (isTor) {
	                flagsRq.add("to");
	            }

	            if (isRemboursable) {
	                flagsRq.add("rf");
	            }

	            if (withBag) {
	                flagsRq.add("bag");
	            }

	            if (isFlexible) {
	                flagsRq.add("flexible");
	            }
	        }
	        FareMasterPricerTravelBoardSearch request=fareMasterPricerTravelBoardSearchRQ.buildFareMasterPricerTravelBoardSearch();
	        headersServices.getFareMasterPricerTravelBoardSearchRQ(request);
	        
	  return null;
	       // return mercureOrder.toString();

	    }
	   
	   @PostMapping(path = "/offer", consumes = {"application/json;charset=UTF-8", "application/json"})
	    public String offer(
	    		 @RequestParam(value = "cabin", required = false) String cabin, //ECONOMY,
	             @RequestParam(value = "ora", required = false, defaultValue = "YY") String ora,// company AF
	             @RequestParam(value = "Itinerary", required = false) String Itinerary,//CMNPAR20220626,PARCMN20220630
	             @RequestParam(value = "travellers", required = false) String travellers,//travellers=1:ADT,0:CHD,0:INF,0:YTH,0:YCD
	             @RequestParam(value = "baggage", required = false, defaultValue = "false") Boolean baggage,
	             @RequestParam(value = "direct", required = false, defaultValue = "false") Boolean direct,
	             @RequestParam(value = "rf", required = false, defaultValue = "false") Boolean rf,
	             @RequestParam(value = "ref", required = false) String ref,
	             @RequestParam(value = "sorteBy", required = false) String sorteBy,
	             @RequestParam(value = "flags", required = false) String flags,
	             @RequestParam(value = "corp", required = false) String corp,
	             @RequestParam(value = "index", required = false) String index,
	             @RequestParam(value = "split", required = false ) String split,
	             @RequestParam(value = "transactionId", required = false) String transactionId,
	             @RequestParam(value = "to" , required = false, defaultValue = "false" ) Boolean to,
	             @RequestParam(value = "sd", required = false , defaultValue = "false") Boolean sd) throws Exception {
		   
		   
		   
		   List<String> flagsRq = new ArrayList<>();
	       
	        if (flags != null) {
	            String[] flagsArray = flags.split(",");

	            boolean isDirect = Arrays.asList(flagsArray).contains("D");
	            boolean isRemboursable = Arrays.asList(flagsArray).contains("REF");
	            boolean isFlexible = Arrays.asList(flagsArray).contains("FLEX");
	            boolean withBag = Arrays.asList(flagsArray).contains("CBO");
	            boolean isTor = Arrays.asList(flagsArray).contains("to");
	            boolean isSd = Arrays.asList(flagsArray).contains("sd");
	            boolean isNego = Arrays.asList(flagsArray).contains("nego");

	            if (isNego) {
	                flagsRq.add("nego");
	            }



	            if (isSd) {
	                flagsRq.add("sd");
	            }

	            if (isDirect) {
	                flagsRq.add("direct");
	            }
	            if (isTor) {
	                flagsRq.add("to");
	            }

	            if (isRemboursable) {
	                flagsRq.add("rf");
	            }

	            if (withBag) {
	                flagsRq.add("bag");
	            }

	            if (isFlexible) {
	                flagsRq.add("flexible");
	            }
	        }
	        AMATravelOfferPriceRQ request=offerPriceRQ.buildAMA_TravelOfferPriceRQ();
	        headersServices.getOfferPrice(request);
	        
	  return null;
	       // return mercureOrder.toString();

	    }
	

}
