<?php
namespace App\Amadeus\StructureParser\Structures\Car;

class Itinerary extends \App\Amadeus\StructureParser\Parser\Itinerary
{

	protected function updateInternalSegmentData()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$indexType = $this->transaction->getIndexType();
		$index = $this->transaction->getIndex();
		$U = $parsedStructure['U'][$index->segment];

		if($indexType=='U' && $U['_type']=='08'){
			$seg = $this->updateInternalSegmentData_U_08();
			if($seg){
				return $seg;
			}
		}

		if($indexType=='U' && $U['_type']=='09'){
			$seg = $this->updateInternalSegmentData_U_09();
			if($seg){
				return $seg;
			}
		}

		return false;
	}


	/**
	 * Get data from a specific line
	 */
	protected function updateInternalSegmentData_U_08()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$item = new \App\Amadeus\StructureParser\Types\ItinerarySegment();

		$U = $parsedStructure['U'][$index->segment];

		$item->departure->suggest('city', 			$U['AIRPT']);
		$item->departure->suggest('city', 			$U['CDETRAN']);
		$item->destination->suggest('destination', 	$U['FREE_']['dropOff']);

		$match = $item->match([
			'airportCode' => 	[$U['AIRPT']],
			'airportName' => 	[$U['CDETRAN']],
			'cityCode' => 		[$U['AIRPT']],
			'cityName' => 		[$U['CDETRAN']],
		], false);
		$item->destination->airportCode = 	$match->airport??null;
		$item->destination->cityId = 		$match->city??null;
		$item->destination->countryCode = 	$match->country??null;

		// NOTE: based on some testcases from RO/AS. Extracting data from dropOff might not be ok for all cases, this was AS specific, as seen on some exmples
		$U['FREE_']['dropOff'] = trim(str_replace("AIRPORT", "", (string)$U['FREE_']['dropOff']));

		$match = $item->match([
			'airportCode' => 	[$U['FREE_']['dropOff']],
			'airportName' => 	[$U['FREE_']['dropOff']],
			'cityCode' => 		[$U['FREE_']['dropOff']],
			'cityName' => 		[$U['FREE_']['dropOff']],
		], false);
		$item->departure->airportCode = $match->airport??null;
		$item->departure->cityId = 		$match->city??null;
		$item->departure->countryCode = $match->country??null;


		// detect departure/arrival dates & hours
		$item->departure->date= 		$U['PICKUP'];
		$item->departure->time= 		$U['FREE_']['startDate'];
		$item->destination->date= 		$U['DROP'];
		$item->destination->time = 		$U['FREE_']['endDate'];

		// transport details
		$item->supplierCode = 			rtrim((string)$U['FREE_']['supplier']['code']);
		$item->segmentType = 			$U['_type'];

		$item->segmentStatus = 			$U['STATUS_']['code'];
		$item->segmentStatusInPNR = 	$U['PNRSTAT_']['code'];

		$this->itinerary->appendSegment($item);
		return true;
	}

	/**
	 * Get data from a specific line
	 */
	protected function updateInternalSegmentData_U_09()
	{
		$parsedStructure = $this->transaction->getParsedStructure();
		$index = $this->transaction->getIndex();

		$item = new \App\Amadeus\StructureParser\Types\ItinerarySegment();

		$U = $parsedStructure['U'][$index->segment];

		$item->departure->suggest('city', 	$U['AIRPT']);
		$item->departure->suggest('city', 	$U['CDETRAN']);

		$match = $item->match([
			'cityCode' => 		[$U['AIRPT']],
			'cityName' => 		[$U['CDETRAN']],
			'airportCode' => 	[$U['AIRPT']],
			'airportName' => 	[$U['CDETRAN']],
		], false);
		$item->departure->airportCode = $match->airport;
		$item->departure->cityId = 		$match->city;
		$item->departure->countryCode = $match->country;


		$item->destination->airportCode = 	$item->departure->airportCode;
		$item->destination->cityId = 		$item->departure->cityId;
		$item->destination->countryCode = 	$item->departure->countryCode;

		// detect departure/arrival dates & hours
		$item->departure->date= 		$U['PICKUP'];
		$item->departure->time= 		$U['ARROPT'];
		$item->destination->date= 		$U['DROP'];
		$item->destination->time = 		$U['RTTME'];

		// transport details
		$item->supplierCode = 			rtrim($U['AIRCDE']);
		$item->segmentType = 			$U['_type'];

		$item->segmentStatus = 			$U['STATUS_']['code'];
		$item->segmentStatusInPNR = 	$U['PNRSTAT_']['code'];

		$this->itinerary->appendSegment($item);
		return true;
	}
}
