<?php
namespace App\Amadeus\FileParser;

class Line_AMS extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.152. AMS Structured Address for Mail
	 * see page 371(413)
	 *
	 * DETAILS
	 */
	protected $structure = [
		['name'=> 'MSGTAG',     'val' => 'AMS',   'type' => 'AN',      'req' => 'M',  'len' => 3,],                          // 1 : Message Tag (AMS)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],                          // 2 : Delimiter (Constant value of ';')
		['name'=> 'TYPEADRS',                     'type' => 'AN',                     'len' => 1,],                          // 3 : Type of address (H- Home, D- Delivery, M- Misc)
		['name'=> '_',                        	'type' => 'customCall', 'call'=> 'fields',],
		/*
		Array( 'name'=> 'COMPID',     'val' => ';CY-',  'type' => 'AN',                     'len' => 4,),                          // 5 : Company Tag (Constant value of CY-)
		Array( 'name'=> 'COMPANY',                      'type' => 'AN','requires' => Array('COMPID'), 'len_min' => 1,   'len_max' => 30,),   // 6 : Company Name
		Array( 'name'=> 'NAMEID',     'val' => ';NA-',  'type' => 'AN',                     'len' => 4,),                          // 8 : Name Tag (Constant value of NA-)
		Array( 'name'=> 'NAME',                         'type' => 'AN','requires' => Array('NAMEID'), 'len_min' => 1,   'len_max' => 30,),   // 9 : Name
		Array( 'name'=> 'ADRS1ID',    'val' => ';A1-',  'type' => 'AN',                     'len' => 4,),                          // 11 : Address Line 1 Tag (Constant value of A1-)
		Array( 'name'=> 'ADRS1',                        'type' => 'AN','requires' => Array('ADRS1ID'), 'len_min' => 1,   'len_max' => 50,),   // 12 : Address Line 1
		Array( 'name'=> 'ADRS2ID',    'val' => ';A2-',  'type' => 'AN',                     'len' => 4,),                          // 14 : Address Line 2 Tag (Constant value of A1-)
		Array( 'name'=> 'ADRS2',                        'type' => 'customCall', 'call'=> 'ADRS2',    'requires' => Array('ADRS2ID', ), 'len_min' => 1,   'len_max' => 50,),
		Array( 'name'=> 'POID',       'val' => ';PO-',  'type' => 'AN',                     'len' => 4,),                          // 17 : P.O Box Tag (Constant value of PO-)
		Array( 'name'=> 'POBOX',                        'type' => 'AN','requires' => Array('POID'), 'len_min' => 1,   'len_max' => 8,),    // 18 : P.O Box Number
		Array( 'name'=> 'ZPID',       'val' => ';ZP-',  'type' => 'AN',                     'len' => 4,),                          // 20 : Zip/Postal Code Tag (Constant value of ZP-)
		Array( 'name'=> 'ZP',                           'type' => 'AN','requires' => Array('ZPID'), 'len_min' => 1,   'len_max' => 20,),   // 21 : Zip/Postal Code
		Array( 'name'=> 'CIID',       'val' => ';CI-',  'type' => 'AN',                     'len' => 4,),                          // 23 : City Tag (Constant value of CI-)
		Array( 'name'=> 'CITY',                         'type' => 'AN','requires' => Array('CIID'), 'len_min' => 1,   'len_max' => 30,),   // 24 : City
		Array( 'name'=> 'STID',       'val' => ';ST-',  'type' => 'AN',                     'len' => 4,),                          // 26 : State Tag (Constant value of ST-)
		Array( 'name'=> 'STATE',                        'type' => 'AN','requires' => Array('STID'), 'len_min' => 1,   'len_max' => 25,),   // 27 : State
		Array( 'name'=> 'COID',       'val' => ';CO-',  'type' => 'AN',                     'len' => 4,),                          // 29 : Country Tag (Constant value of CO-)
		Array( 'name'=> 'COUNTRY',                      'type' => 'AN','requires' => Array('COID'), 'len_min' => 1,   'len_max' => 25,),   // 30 : Country
		*/
		['name'=> 'PSGASS',     'val' => ';P',    'type' => 'ASSOC',],
	];

	function parseFromStructure_v2_custom_fields($tree, $struct, $content, $index, $options){
		// do the actual matcing
		$matches = [];
		$regexp = "^"
			."(\;CY-(?P<COMPANY>[^;]*))?"
			."(\;NA-(?P<NAME>[^;]*))?"
			."(\;A1-(?P<ADRS1>.*?(?=\;(A2)-)))?"
			."(\;A2-(?P<ADRS2>.*?(?=\;(PO|ZP|CI|ST|CO)-)))?"
			."(\;PO-(?P<POBOX>[^;]*))?"
			."(\;ZP-(?P<ZP>[^;]*))?"
			."(\;CI-(?P<CITY>[^;]*))?"
			."(\;ST-(?P<STATE>[^;]*))?"
			."(\;CO-(?P<COUNTRY>[^;]*))?";

		preg_match("/{$regexp}/", substr($content, $index), $matches);

		if ($matches) {
			$tree['COMPANY'] = 	$matches['COMPANY'];
			$tree['NAME'] = 	$matches['NAME'];
			$tree['ADRS1'] = 	$matches['ADRS1'];
			$tree['ADRS2'] = 	$matches['ADRS2'];
			$tree['POBOX'] = 	$matches['POBOX'];
			$tree['ZP'] = 		$matches['ZP'];
			$tree['CITY'] = 	$matches['CITY'];
			$tree['STATE'] = 	$matches['STATE'];
			$tree['COUNTRY'] = 	$matches['COUNTRY'];

			$ret = [];
			$ret['index'] = $index+strlen($matches[0]);
			$ret['tree'] = $tree;
			$ret['skipStructIndexIncrement'] = true;
			return $ret;
		}

		return null;
	}

}
