<?php
namespace App\Sabre\LineParser\Types\Line;

/**
 *
 * @author Lucian Sirbu
 */
class M3Air extends \App\Sabre\LineParser\Types\SabreBaseLine
{
	/**
	 * M3 – Itinerary Data Record – Air Itinerary Items
	 * The Air format is used for Air and AMTRAK rail Itinerary Items.
	 */

	/**
	 * Children elements
	 */
	protected $children = [
	    ['key'=>'IU3MID',    'type'=>'Constant',      'len'=>2,       'val'=>'M3',    'label'=>'IU3MID'],    // RECORD IDENTIFIER - "M3"
	    ['key'=>'IU3ITN',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3ITN'],    // ITINERARY ITEM NUMBER
	    ['key'=>'IU3PRC',    'type'=>'Constant',      'len'=>1,       'val'=>'1',     'label'=>'IU3PRC'],    // PRODUCT CODE
	    ['key'=>'IU3LNK',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3LNK'],    // LINK CODE
	    ['key'=>'IU3CRL',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3CRL'],    // CONTROL DATA
	    ['key'=>'IU3AAC',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3AAC'],    // ACTION ADVICE/SEGMENT STATUS CODES
	    ['key'=>'IU3DDT',    'type'=>'StringTrim',    'len'=>5,                       'label'=>'IU3DDT'],    // DEPARTURE DATE
	    ['key'=>'IU3PC2',    'type'=>'StringTrim',    'len'=>3,                       'label'=>'IU3PC2'],    // SECONDARY PRODUCT CODE, "AIR", "RAL"
	    ['key'=>'IU3BPI',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3BPI'],    // ISSUE A BOARDING PASS
	    ['key'=>'IU3DCC',    'type'=>'StringTrim',    'len'=>3,                       'label'=>'IU3DCC'],    // DEPARTURE CITY CODE
	    ['key'=>'IU3DCY',    'type'=>'StringTrim',    'len'=>17,                      'label'=>'IU3DCY'],    // DEPARTURE CITY NAME
	    ['key'=>'IU3ACC',    'type'=>'StringTrim',    'len'=>3,                       'label'=>'IU3ACC'],    // ARRIVAL CITY CODE
	    ['key'=>'IU3ACY',    'type'=>'StringTrim',    'len'=>17,                      'label'=>'IU3ACY'],    // ARRIVAL CITY NAME
	    ['key'=>'IU3CAR',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3CAR'],    // CARRIER CODE
	    ['key'=>'IU3FLT',    'type'=>'StringTrim',    'len'=>5,                       'label'=>'IU3FLT'],    // FLIGHT NUMBER
	    ['key'=>'IU3CLS',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3CLS'],    // CLASS OF SERVICE
	    ['key'=>'IU3DTM',    'type'=>'Time',          'len'=>5,                       'label'=>'IU3DTM'],    // DEPARTURE TIME
	    ['key'=>'IU3ATM',    'type'=>'Time',          'len'=>5,                       'label'=>'IU3ATM'],    // ARRIVAL TIME
	    ['key'=>'IU3ELT',    'type'=>'StringTrim',    'len'=>8,                       'label'=>'IU3ELT'],    // ELAPSED FLYING TIME
	    ['key'=>'IU3MLI',    'type'=>'StringTrim',    'len'=>4,                       'label'=>'IU3MLI'],    // MEAL SERVICE INDICATOR
	    ['key'=>'IU3SUP',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3SUP'],    // SUPPLEMENTAL INFORMATION
	    ['key'=>'IU3DCH',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3DCH'],    // FLIGHT ARRIVAL DATE CHANGE INDICATOR
	    ['key'=>'IU3NOS',    'type'=>'IntegerB',      'len'=>1,                       'label'=>'IU3NOS'],    // NUMBER OF STOPS
	    ['key'=>'IU3SCC',    'type'=>'StringTrim',    'len'=>18,                      'label'=>'IU3SCC'],    // STOPOVER CITY CODES
	    ['key'=>'IU3CRT',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3CRT'],    // CARRIER TYPE CODE
	    ['key'=>'IU3EQP',    'type'=>'StringTrim',    'len'=>3,                       'label'=>'IU3EQP'],    // EQUIPMENT TYPE CODE
	    ['key'=>'IU3ARM',    'type'=>'Integer',       'len'=>6,                       'label'=>'IU3ARM'],    // STATUTE MILES
	    ['key'=>'IU3AVM',    'type'=>'StringTrim',    'len'=>6,                       'label'=>'IU3AVM'],    // FREQUENT TRAVELER MILES
	    ['key'=>'IU3SCT',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3SCT'],    // PRE-RESERVED SEAT COUNTER
	    ['key'=>'IU3TRM',    'type'=>'StringTrim',    'len'=>26,                      'label'=>'IU3TRM'],    // DEPARTURE TERMINAL
	    ['key'=>'IU3GAT',    'type'=>'StringTrim',    'len'=>4,                       'label'=>'IU3GAT'],    // DEPARTURE GATE
	    ['key'=>'IU3TMA',    'type'=>'StringTrim',    'len'=>26,                      'label'=>'IU3TMA'],    // ARRIVAL TERMINAL
	    ['key'=>'IU3GAR',    'type'=>'StringTrim',    'len'=>4,                       'label'=>'IU3GAR'],    // ARRIVAL GATE
	    ['key'=>'IU3RTM',    'type'=>'StringTrim',    'len'=>5,                       'label'=>'IU3RTM'],    // REPORT TIME
	    ['key'=>'IU3COG',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3COG'],    // CHANGE OF GAUGE/FUNNLE FLIGHT COUNTER
	    ['key'=>'IU3CRN',    'type'=>'StringTrim',    'len'=>37,                      'label'=>'IU3CRN'],    // COMMUTER CARRIER NAME
	    ['key'=>'IU3TKT',    'type'=>'StringTrim',    'len'=>1,                       'label'=>'IU3TKT'],    // ITINERARY ITEM TICKETING INDICATOR
	    ['key'=>'IU3MCT',    'type'=>'StringTrim',    'len'=>2,                       'label'=>'IU3MCT'],    // SPECIAL MEAL REQUEST COUNTER
	    ['key'=>'IU3YER',    'type'=>'StringTrim',    'len'=>4,                       'label'=>'IU3YER'],    // FLIGHT DEPARTURE YEAR
	    ['key'=>'IU3OAL',    'type'=>'StringTrim',    'len'=>8,                       'label'=>'IU3OAL'],    // AIRLINE PNR LOCATOR
	    ['key'=>'NL',        'type'=>'NewLine',],
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockAbstract::postExtract()
	 */
	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		// extract IU3DDT_
		$dt = $this->createObject('Date', ['fmt'=>'YYYYDDMMM', 'ref'=> 'determinedFileDate',], $this);
		$dt->setRawData($ret->extractedData['IU3YER'].$ret->extractedData['IU3DDT']);
		$ret->extractedData['IU3DDT_'] = $dt->extract()->extractedData;

		switch ($ret->extractedData['IU3DCH']) {
			case '':
				$ret->extractedData['IU3ADT_'] = $ret->extractedData['IU3DDT_'];
			break;
			case '0':
				$ret->extractedData['IU3ADT_'] = $ret->extractedData['IU3DDT_'];
			break;
			case '1':
				$ret->extractedData['IU3ADT_'] = \Engine\DateTime::createFromIsoDate($ret->extractedData['IU3DDT_'])->modify("+1 day")->formatDateIso();
			break;
			case '2':
				$ret->extractedData['IU3ADT_'] = \Engine\DateTime::createFromIsoDate($ret->extractedData['IU3DDT_'])->modify("+2 day")->formatDateIso();
			break;
			case '3':
				$ret->extractedData['IU3ADT_'] = \Engine\DateTime::createFromIsoDate($ret->extractedData['IU3DDT_'])->modify("+3 day")->formatDateIso();
			break;
			case '4':
				$ret->extractedData['IU3ADT_'] = \Engine\DateTime::createFromIsoDate($ret->extractedData['IU3DDT_'])->modify("-1 day")->formatDateIso();
			break;
			case '5':
				$ret->extractedData['IU3ADT_'] = \Engine\DateTime::createFromIsoDate($ret->extractedData['IU3DDT_'])->modify("-2 day")->formatDateIso();
			break;
			default:
				throw new \App\Utils\BaseParser\TextParser\Exception\LineParser("Invalid value for the IU3DCH field: {$ret->extractedData['IU3DCH']}");
		}

		// handle IU3CLS
		if ($ret->extractedData['IU3CLS']) {
			$ret->extractedData['IU3CLS_']['code'] = $ret->extractedData['IU3CLS'];
			$ret->extractedData['IU3CLS_']['label'] = \App\Config\ServiceClassCodeRules::getInstance()->convertClassCodeToName($ret->extractedData['IU3CLS'], $ret->extractedData['IU3CAR']);
		}

		$IU3MLI = array_filter(str_split($ret->extractedData['IU3MLI'], 1));
		$ret->extractedData['IU3MLI_'] = [];
		foreach ($IU3MLI as $MLI) {
			$ret->extractedData['IU3MLI_'][] = [
				'code' => 	$MLI,
				'label' => 	\App\Utils\BaseParser\Utils\MealCodeConvertor::getInstance()->convertMealCodeToName($MLI, $ret->extractedData['IU3CAR']),
			];
		}

		$ret->extractedData['IU3ELT_'] = str_replace(".", ":", $ret->extractedData['IU3ELT']);

		if ($ret->extractedData['IU3SCC']) {
			$ret->extractedData['IU3SCC_'] = str_split($ret->extractedData['IU3SCC'], 3);
		}

		return $ret;
	}

}
