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

/**
 *
 * @author Lucian Sirbu
 */
class R01 extends \App\Worldspan\LineParser\Types\WorldspanBaseLine
{
	protected $extractRegexp = "/^(?P<rawData>(0|\*)1.+)/sm";

	protected $children = [
        /**
         * This record contains information on each segment in the itinerary included in the pricing record
         * This includes ticketable train segments like Via Rail and Britrail (but not Amtrak)
         * A new field (BP-) has been added to accommodate a new commission structure instituted by British Airways
         */

        ['key'=> 'RecordIdentifier',                  'type'=> 'Set',               'len'=> 1,          'val'=> ['0', '*'], 'req'=> false,],             // Record Identifier
        ['key'=> 'key',                               'type'=> 'Constant',          'len'=> 2,                    'val'=> '1\\', 'req'=> true,],              // Included air segment code
        ['key'=> 'ItinerarySegmentNumber',            'type'=> 'Integer',           'len'=> [1, 3],                         'req'=> false,],             // Itinerary segment number (see Note 6)
        ['key'=> '#018',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field Separator
        ['key'=> 'NumberOfIntermediateStops',         'type'=> 'StringRaw',            'len'=> 1,                                   'req'=> false,],             // Number of intermediate stops
        ['key'=> 'SharedDesignatorIndicator',         'type'=> 'StringRaw',            'len'=> 1,                                   'req'=> false,],             // Shared designator indicator
        ['key'=> 'CarrierCode',                       'type'=> 'StringRaw',            'len'=> 2,                                   'req'=> false,],             // Carrier code
        ['key'=> 'FlightNumber',                      'type'=> 'StringDelimitedByChar',   'len'=> 4,                    'val'=> '\\',  'req'=> true,],              // Flight number (See Note 8)
        ['key'=> '#023',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'ClassOfService',                    'type'=> 'StringDelimitedByChar',   'len'=> [0, 2],          'val'=> '\\',  'req'=> false,],             // Class of service
        ['key'=> '#025',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'OriginAirportCode',                 'type'=> 'Alphanumeric',      'len'=> 3,                                   'req'=> false,],             // Origin airport code
        ['key'=> 'DepartureDate',                     'type'=> 'Date',              'len'=> 5,     'fmt'=> 'DDMMM',              'req'=> false, 'ref'=>'BookingDateAndYear', 'onlyFuture'=>true,],             // Departure date (See Note 9)
        ['key'=> 'DepartureTime',                     'type'=> 'TimeOrStar',        'len'=> 5,     'fmt'=> 'HHMMP+',             'req'=> false,],             // Departure time (See Note 10)
        ['key'=> '#029',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'DestinationAirportCode',            'type'=> 'Alphanumeric',      'len'=> 3,                                   'req'=> false,],             // Destination airport code
        ['key'=> 'ArrivalDate',                       'type'=> 'Date',              'len'=> 5,     'fmt'=> 'DDMMM',              'req'=> false, 'ref'=>'BookingDateAndYear', 'onlyFuture'=>true,],             // Arrival date (See Note 9)
        ['key'=> 'ArrivalTime',                       'type'=> 'TimeOrStar',        'len'=> 5,     'fmt'=> 'HHMMP+',             'req'=> false,],             // Arrival time (See Note 10)
        ['key'=> '#033',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'NotValidBeforeDate',                'type'=> 'DateOrStar',        'len'=> 5,     'fmt'=> 'DDMMM',              'req'=> true, 'ref'=>'BookingDateAndYear', 'onlyFuture'=>true,],             // Not valid before date
        ['key'=> '#035',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'NotValidAfterDate',                 'type'=> 'DateOrStar',        'len'=> 5,     'fmt'=> 'DDMMM',              'req'=> true, 'ref'=>'BookingDateAndYear', 'onlyFuture'=>true,],             // Not valid after date
        ['key'=> '#037',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'BaggageRestriction',                'type'=> 'StringDelimitedByChar',   'len'=> [0, 3],          'val'=> '\\',  'req'=> false,],             // Baggage restriction
        ['key'=> '#039',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'TicketSegmentStatus',               'type'=> 'Alphanumeric',      'len'=> 2,                                   'req'=> false,],             // Ticket segment status (see Note 1)
        ['key'=> 'MealServiceCodes',                  'type'=> 'StringDelimitedByChar',   'len'=> [0, 42],         'val'=> '\\',  'req'=> false,],             // Meal service codes (See Note 2)
        ['key'=> 'TicketSegmentStopoverCode',         'type'=> 'Alphanumeric',      'len'=> 1,                                   'req'=> false,],             // Ticket segment stopover code (see Note 3)
        ['key'=> '#043',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'EquipmentType',                     'type'=> 'StringDelimitedByChar',   'len'=> [0, 3],          'val'=> '\\',  'req'=> false,],             // Equipment type
        ['key'=> '#045',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'FareBasisCodeTicketDesignator',     'type'=> 'StringDelimitedByChar',   'len'=> [0, 15],         'val'=> '\\',  'req'=> false,],             // Fare basis code/Ticket Designator
        ['key'=> '#047',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'SegmentMileage',                    'type'=> 'FloatRaw',             'len'=> [1, 5],                         'req'=> false,],             // Segment Mileage (default * if unknown)
        ['key'=> '#049',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'IntermediateStops',                 'type'=> 'StringDelimitedByChar',   'len'=> [0, 159],        'val'=> '\\',  'req'=> false,],             // Intermediate Stops (default * if none)
        ['key'=> '#051',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator
        ['key'=> 'AccumulatedElapsedFlightTime',      'type'=> 'FloatRaw',             'len'=> [1, 5],                         'req'=> false,],             // Accumulated Elapsed Flight Time
        ['key'=> '#053',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field separator

        ['key'=> 'complex:41',                        'type'=> 'Complex', 'merge'=>true,                                         'req'=> false, 'children'=> [
            ['key'=> 'key',                               'type'=> 'Set',          'len'=> 1,                         'val'=> ['@', '*',],  'req'=> true,],              // Segment Override Indicator
            ['key'=> '#055',                              'type'=> 'Constant',          'len'=> 1,                    'val'=> '\\',  'req'=> false,],             // Field separator
            ['key'=> 'DepartureAirportTerminalCode',      'type'=> 'StringDelimitedByChar',   'len'=> [0, 2],          'val'=> '\\',  'req'=> false,],             // Departure airport terminal code
            ['key'=> '#057',                              'type'=> 'Constant',          'len'=> 1,                    'val'=> '\\',  'req'=> false,],             // Field separator
            ['key'=> 'ArrivalAirportTerminalCode',        'type'=> 'StringDelimitedByChar',   'len'=> [0, 2],          'val'=> '\\',  'req'=> false,],             // Arrival airport terminal code
		]],
        ['key'=> '#059',                              'type'=> 'Separator',                                                      'req'=> false,],             // Field Separator
        ['key'=> 'key',                               'type'=> 'Constant',          'len'=> 3,                    'val'=> 'BP-', 'req'=> false,],             // Booking Payment field identifier
        ['key'=> 'BookingPaymentAmount',              'type'=> 'StringRaw',            'len'=> [0, 6],                         'req'=> false,],             // Booking Payment amount
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockAbstract::postExtract()
	 */
	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		if ($ret->extractedData['ClassOfService']) {
			$ret->extractedData['ClassOfService_']['code'] = $ret->extractedData['ClassOfService'];
			$ret->extractedData['ClassOfService_']['label'] = \App\Config\ServiceClassCodeRules::getInstance()->convertClassCodeToName($ret->extractedData['ClassOfService'], $ret->extractedData['CarrierCode']);
		}

		$ret->extractedData['MealServiceCodes_'] = [];
		if ($ret->extractedData['MealServiceCodes']) {
			$meals = array_filter(str_split($ret->extractedData['MealServiceCodes'], 1), function ($str) { return trim($str); });
			foreach ($meals as $meal) {
				$ret->extractedData['MealServiceCodes_'][] = [
					'code' => 	$meal,
					'label' => 	\App\Utils\BaseParser\Utils\MealCodeConvertor::getInstance()->convertMealCodeToName($meal, $ret->extractedData['CarrierCode']),
				];
			}
		}

		$ret->extractedData['DepartureAirportTerminalCode'] = trim($ret->extractedData['DepartureAirportTerminalCode']);
		$ret->extractedData['ArrivalAirportTerminalCode'] = trim($ret->extractedData['ArrivalAirportTerminalCode']);

		$ret->extractedData['AccumulatedElapsedFlightTime'] = sprintf("%s", str_replace(".", ":", sprintf("%.2f", $ret->extractedData['AccumulatedElapsedFlightTime'])));

		return $ret;
	}
}
