<?php

namespace App\Galileo\LineParser\Types;

/**
 * Data line
 * @author Lucian Sirbu
 */
class Data_A04 extends \App\Galileo\LineParser\Types\GalileoBase
{
	/**
	 * AIRLINE DATA SECTION
	 */

	/**
	 * Children elements
	 */
	protected $children = [
		['key' => 'A04SEC',  'type' => 'Constant',   'len' => 3,   'val' => 'A04',                                     'label' => 'SECTION LABEL "A04"'],
		['key' => 'A04ITN',  'type' => 'Integer',    'len' => 2,                                                       'label' => 'ITINERARY INDEX NUMBER'],
		['key' => 'A04CDE',  'type' => 'StringRaw',  'len' => 2,                                                       'label' => 'AIRLINE CODE'],
		['key' => 'A04NUM',  'type' => 'StringRaw',  'len' => 3,                                                       'label' => 'AIRLINE NUMBER'],
		['key' => 'A04NME',  'type' => 'StringTrim', 'len' => 12,                                                      'label' => 'AIRLINE NAME'],
		['key' => 'A04FLT',  'type' => 'StringTrim', 'len' => 4,                                                       'label' => 'FLIGHT NUMBER'],
		['key' => 'A04CLS',  'type' => 'StringRaw',  'len' => 2,                                                       'label' => 'CLASS OF SERVICE'],
		['key' => 'A04STS',  'type' => 'Letters',    'len' => 2,                                                       'label' => 'STATUS'],
		['key' => 'A04DTE',  'type' => 'DateDDMMM',  'len' => 5,   'onlyFuture' => true,   'ref' => 'T50DFT',          'label' => 'DEPARTURE DATE'],
		['key' => 'A04TME',  'type' => 'Time',       'len' => 5,                                                       'label' => 'DEPARTURE TIME'],
		['key' => 'A04ARV',  'type' => 'Time',       'len' => 5,                                                       'label' => 'ARRIVAL TIME'],
		['key' => 'A04IND',  'type' => 'Integer',    'len' => 1,                                                       'label' => 'NEXT DAY ARRIVAL INDICATOR'],
		['key' => 'A04OCI',                                                                                            'label' => 'ORIGIN CITY INFORMATION', 'children' => [
			['key' => 'A04OCC',  'type' => 'Letters',    'len' => 3,   'label' => 'ORIGIN CITY CODE'],
			['key' => 'A04OCN',  'type' => 'Location',   'len' => 13,  'label' => 'ORIGIN CITY NAME'],
		]],
		['key' => 'A04DCI',                                                                                            'label' => 'DESTINATION CITY INFORMATION', 'children' => [
			['key' => 'A04DCC',  'type' => 'Letters',    'len' => 3,   'label' => 'DESTINATION CITY CODE'],
			['key' => 'A04DCN',  'type' => 'Location',   'len' => 13,  'label' => 'DESTINATION CITY NAME'],
		]],
		['key' => 'A04DOM',  'type' => 'Letters',    'len' => 1,                                                       'label' => 'DOMESTIC/INTERNATIONAL INDICATOR'],
		['key' => 'A04SET',  'type' => 'Letters',    'len' => 1,                                                       'label' => 'SEAT INDICATOR'],
		['key' => 'A04SVC',  'type' => 'Letters',    'len' => 4,                                                       'label' => 'MEAL CODES'],
		['key' => 'A04STP',  'type' => 'Letters',    'len' => 1,                                                       'label' => 'STOPOVER INDICATORS'],
		['key' => 'A04STO',  'type' => 'Integer',    'len' => 1,                                                       'label' => 'NUMBER OF STOPS'],
		['key' => 'A04BAG',  'type' => 'StringRaw',  'len' => 3,                                                       'label' => 'BAGGAGE ALLOWANCE'],
		['key' => 'A04AIR',  'type' => 'StringTrim', 'len' => 4,                                                       'label' => 'TYPE OF AIRCRAFT'],
		['key' => 'A04DTR',  'type' => 'StringRaw',  'len' => 3,                                                       'label' => 'DEPARTURE TERMINAL'],
		['key' => 'A04MIL',  'type' => 'Integer',    'len' => 5,                                                       'label' => 'NAUTICAL MILES'],
		['key' => 'A04FCI',  'type' => 'Letters',    'len' => 1,                                                       'label' => 'FLIGHT COUPON INDICATOR'],
		['key' => 'A04SIC',  'type' => 'Integer',    'len' => 1,                                                       'label' => 'SEGMENT IDENTIFIER'],

		['key' => 'opt',     'type' => 'OrderedComposer', 'merge' => true, 'children' => [
			['key' => 'A04CGCc',   'header' => 'COG:',   'type' => 'StringRaw',  'len' => 22,  'label' => 'INTERMEDIATE CITY CODE'],
			//  [   'key'=>'A04CGC',    'header'=>'COG:',   'type'=>'Letters',    'len'=>3,   'label'=>'INTERMEDIATE CITY CODE' ],
			//  [   'key'=>'A04CGN',                        'type'=>'Letters',    'len'=>13,  'label'=>'INTERMEDIATE CITY NAME' ],
			//  [   'key'=>'A04CGD',                        'type'=>'Integer',    'len'=>1,   'label'=>'NEXT DAY ARRIVAL INDICATOR' ],
			//  [   'key'=>'A04CGT',                        'type'=>'StringRaw',  'len'=>5,   'label'=>'DEPARTURE TIME FROM INTERMEDIATE CITY' ],
			['key' => 'A04GRR',    'header' => 'GCR:',   'type' => 'StringRaw',  'len' => 6,   'label' => 'GROUP CONTROL RECORD LOCATOR'],
			['key' => 'A04ACC',    'header' => 'AC:',    'type' => 'Letters',    'len' => 12,  'label' => 'AFFILIATED CARRIER NAME'],
			['key' => 'A04FFM',    'header' => 'FF:',    'type' => 'Integer',    'len' => 5,   'label' => 'FREQUENT FLYER MILES'],
			['key' => 'A04TKT',    'header' => 'TK:',    'type' => 'StringRaw',  'len' => 1,   'label' => 'TICKETED INDICATOR'],
			['key' => 'A04JTM',    'header' => 'JT:',    'type' => 'StringRaw',  'len' => 5,   'label' => 'TOTAL DURATION OF JOURNEY TIME BY SEGMENT'],
			['key' => 'A04NML',    'header' => 'ANL:',   'type' => 'StringRaw',  'len' => 24,  'label' => 'AIRLINE NAME'],
			['key' => 'A04ACN',    'header' => 'ACL:',   'type' => 'StringTrim', 'len' => 24,  'label' => 'AFFILIATED CARRIER NAME'],
		]],
	];

	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		// handle A04DTE_A04IND
		$dt = \Engine\DateTime::createFromIsoDate($ret->extractedData['A04DTE']);
		if ($ret->extractedData['A04IND']) {
			switch ($ret->extractedData['A04IND']) {
				case 1:	// PREVIOUS DAY ARRIVAL
					$dt->sub(new \DateInterval('P1D'));
					break;

				case 2:	// SAME DAY ARRIVAL
					break;

				case 3:	// NEXT DAY ARRIVAL
					$dt->add(new \DateInterval('P1D'));
					break;

				case 4:	// 2 DAYS LATER ARRIVAL
					$dt->add(new \DateInterval('P2D'));
					break;

				default:
					throw new \Exception(sprintf("Invalid value for tag A04IND: '%s'", $ret->extractedData['A04IND']));
			}

			$ret->extractedData['A04DTE_A04IND'] = $dt->formatDateIso();
		}

		// handle A04SVC_
		$assoc = [
			'B' => 'Breakfast',
			'C' => 'Alcohol no cost',
			'D' => 'Dinner',
			'F' => 'Food Available for Purchase',
			'H' => 'Hot meal',
			'K' => 'Continental breakfast',
			'L' => 'Lunch',
			'M' => 'Meal',
			'O' => 'Cold meal',
			'P' => 'Alcohol to purchase',
			'R' => 'Refreshment',
			'S' => 'Snack or Brunch',
		];
		$ret->extractedData['A04SVC_'] = [];
		$arr = str_split($ret->extractedData['A04SVC']);
		foreach ($arr as $a) {
			$ret->extractedData['A04SVC_'][] = $assoc[$a];
		}

		$ret->extractedData['A04BAG_'] = $this->parse_tag_BAGGAGE($ret->extractedData['A04BAG']);

		// handle A04JTM
		if ($ret->extractedData['A04JTM']) {
			$ret->extractedData['A04JTM'] = str_replace(".", ":", $ret->extractedData['A04JTM']);
		}

		// handle A04CLS
		if ($ret->extractedData['A04CLS']) {
			$ret->extractedData['A04CLS_']['code'] = rtrim($ret->extractedData['A04CLS']);
			$ret->extractedData['A04CLS_']['label'] = \App\Config\ServiceClassCodeRules::getInstance()->convertClassCodeToName($ret->extractedData['A04CLS'], $ret->extractedData['A04CDE']);
		}

		return $ret;
	}
}
