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

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

	protected $children = [
        /**
         * This record contains information on each segment in the itinerary that is not included in the ticket issued
         * This includes segments that would normally print on a ticket (eg air segments, train segments like Via Rail and Brit Rail)
         *   but not Amtrak and other non-air segments)
         */

        ['key'=> 'RecordIdentifier',                  'type'=> 'Set',                     'len'=> 1,          'val'=> ['0', '*'], 'req'=> false,],            // Record Identifier
        ['key'=> '#015',                              'type'=> 'Constant',                'len'=> 2,                    'val'=> '2\\', 'req'=> true,],             // "Not included" segment code
        ['key'=> 'ItinerarySegmentNumber',            'type'=> 'Integer',                 'len'=> [1, 3],                         'req'=> true,],             // Itinerary segment number (see Note 8)
        ['key'=> '#017',                              'type'=> 'Separator',                                                            'req'=> true,],             // Field Separator
        ['key'=> 'NumberOfIntermediateStops',         'type'=> 'StringRaw',               'len'=> 1,                                   'req'=> true,],             // Number of Intermediate Stops
        ['key'=> 'SharedDesignatorIndicator',         'type'=> 'StringRaw',               'len'=> 1,                                   'req'=> true,],             // Shared Designator indicator
        ['key'=> 'CarrierCode',                       'type'=> 'StringRaw',               'len'=> 2,                                   'req'=> true,],             // Carrier Code
        ['key'=> 'FlightNumber',                      'type'=> 'StringDelimitedByChar',   'len'=> 4,                    'val'=> '\\',  'req'=> true,],             // Flight Number (See Note 9)
        ['key'=> '#022',                              'type'=> 'Separator',                                                            'req'=> true,],             // Field Separator
        ['key'=> 'ClassOfService',                    'type'=> 'StringDelimitedByChar',   'len'=> [0, 2],          'val'=> '\\',  'req'=> true,],             // Class of Service
        ['key'=> '#024',                              'type'=> 'Separator',                                                            'req'=> true,],             // Field Separator
        ['key'=> 'OriginAirportCode',                 'type'=> 'Alphanumeric',            'len'=> 3,                                   'req'=> true,],             // Origin Airport code
        ['key'=> 'DepartureDate',                     'type'=> 'Date',                    'len'=> 5,     'fmt'=> 'DDMMM',              'req'=> true, 'ref'=>'BookingDateAndYear', 'onlyFuture'=>true,],             // Departure Date (See Note 10)
        ['key'=> 'DepartureTime',                     'type'=> 'Time',                    'len'=> 5,     'fmt'=> 'HHMMP+',             'req'=> true,],             // Departure Time (See Note 11)
        ['key'=> '#028',                              'type'=> 'Separator',                                                            'req'=> true,],             // Field Separator
        ['key'=> 'DestinationAirportCode',            'type'=> 'Alphanumeric',            'len'=> 3,                                   'req'=> true,],             // Destination Airport code
        ['key'=> 'ArrivalDate',                       'type'=> 'Date',                    'len'=> 5,     'fmt'=> 'DDMMM',              'req'=> true, 'ref'=>'BookingDateAndYear', 'onlyFuture'=>true,],             // Arrival Date (See Note 10)
        ['key'=> 'ArrivalTime',                       'type'=> 'Time',                    'len'=> 5,     'fmt'=> 'HHMMP+',             'req'=> true,],             // Arrival Time (See Note 11)
        ['key'=> '#033',                              'type'=> 'Constant',                'len'=> 5,               'val'=> '\\*\\*\\', 'req'=> true,],             // Asterisk

        ['key'=> 'complex:25',                        'type'=> 'Complex', 'merge'=>true,                                               'req'=> true, 'children'=> [
            ['key'=> '#037',                              'type'=> 'Constant',                'len'=> 2,                    'val'=> '*\\', 'req'=> true,],              // Asterisk
            ['key'=> 'StatusCode',                        'type'=> 'Alphanumeric',            'len'=> 2,                                   'req'=> false,],             // Status code (See Note 2)
        	#Array(            'key'=> 'MealServiceCodes',                  'type'=> 'Set',                     'len'=> Array(0, 42),         'val'=> [' ', 'B', 'K', 'L', 'D', 'S', 'O', 'H', 'M', 'R', 'C', 'F', 'P', 'Y', 'N', 'V', 'G', ], 'req'=> true, ),              // Meal Service codes (See Notes 3 and 4)
            #Array(            'key'=> 'StopoverCode',                      'type'=> 'Set',                     'len'=> 1,                    'val'=> Array('O', 'X', ' '),  'req'=> true, ),              // Stopover code (See Note 5)
            ['key'=> 'group#1',                           'type'=> 'StringDelimitedByChar',   'len'=> [0, 43],          'val'=> '\\', 'req'=> true,],             // Segment Mileage
            ['key'=> '#042',                              'type'=> 'Separator',                                                            'req'=> false,],             // Field separator
            ['key'=> 'EquipmentType',                     'type'=> 'StringDelimitedByChar',   'len'=> [0, 3],          'val'=> '\\',  'req'=> false,],             // Equipment Type (default * if unknown)
		]],

        ['key'=> '#044',                              'type'=> 'Separator',                                                            'req'=> false,],             // Field Separator

        ['key'=> 'complex:27',                        'type'=> 'Complex', 'merge'=>true,                                               'req'=> true, 'children'=> [
            ['key'=> '#045',                              'type'=> 'Constant',                'len'=> 2,                    'val'=> '*\\', 'req'=> true,],              // Asterisk
            ['key'=> 'SegmentMileage',                    'type'=> 'StringDelimitedByChar',   'len'=> [1, 5],          'val'=> '\\',  'req'=> false,],             // Segment Mileage
            ['key'=> '#048',                              'type'=> 'Separator',                                                            'req'=> false,],             // Field Separator
            ['key'=> 'IntermediateStops',                 'type'=> 'StringDelimitedByChar',   'len'=> [0, 159],        'val'=> '\\',  'req'=> false,],             // Intermediate Stops (default * if none)
            ['key'=> '#050',                              'type'=> 'Separator',                                                            'req'=> false,],             // Field Separator
            ['key'=> 'AccumulatedElapsedFlightTime',      'type'=> 'FloatRaw',                'len'=> [1, 5],                         'req'=> false,],             // Accumulated Elapsed Flight Time
		]],

        ['key'=> '#053',                              'type'=> 'Set',                     'len'=> 3,                    'val'=> ['\\@\\', '\\*\\',],  'req'=> true,], // Segment Override Indicator
        ['key'=> 'DepartureAirportTerminalCode',      'type'=> 'StringDelimitedByChar',   'len'=> [0, 2],          'val'=> '\\',  'req'=> false,],             // Departure airport terminal code
        ['key'=> '#056',                              'type'=> 'Separator',                                                            'req'=> false,],             // Field Separator
        ['key'=> 'ArrivalAirportTerminalCode',        'type'=> 'StringDelimitedByChar',   'len'=> [0, 2],          'val'=> '\\',  'req'=> false,],             // Arrival airport terminal code
        ['key'=> '#x01',                              'type'=> 'Separator',                                                            'req'=> false,],             // Field Separator
        ['key'=> 'unknown01',                         'type'=> 'StringRaw',                                                            'req'=> false,],             // Arrival airport terminal code
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockAbstract::postExtract()
	 */
	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		// remove constants
		unset($ret->extractedData['#053']);

		$ret->extractedData['AccumulatedElapsedFlightTime'] = sprintf("%s", str_replace(".", ":", sprintf("%.2f", $ret->extractedData['AccumulatedElapsedFlightTime'])));
		$ret->extractedData['SegmentMileage_'] = ($ret->extractedData['SegmentMileage']=='*'?null:(float)$ret->extractedData['SegmentMileage']);

		$ret->extractedData['MealServiceCodes'] = trim(substr($ret->extractedData['group#1'], 0, -1));
		$ret->extractedData['StopoverCode'] = substr($ret->extractedData['group#1'], -1);
		unset($ret->extractedData['group#1']);

		return $ret;
	}
}
