<?php
namespace App\Amadeus\FileParser;

class Line_ATF extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.91. ATF- Airline Ticketing Fees Line
	 * see page 253(295)
	 *
	 * This field is used to transmit Airline Service Fees data, also called OB fees.
	 */

	// old structure, defined in manual before V6.20
	protected $structure_V620 = [
		['name'=> 'MSGTAG',     'val' => 'ATF-',  'type' => 'AN',   'req' => 'M',                                   'len' => 4,],                          // 1 : Message Tag (ATF-)
		['name'=> 'CURFEE',                       'type' => 'AN',                                                   'len' => 3,],                          // 2 : Currency Code
		['name'=> 'FEETOTL',                      'type' => 'NF',                                                    'len' => 11,],                         // 3 : Fees Total Amount collected, taxes on Fees included
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 4 : Delimiter (Constant value of ';')
		['name'=> 'CURGDT',                       'type' => 'AN',   'req' => 'M',                                   'len' => 3,],                          // 5 : Currency Code
		['name'=> 'GRDTOTL',                      'type' => 'NF',    'req' => 'M',                                   'len' => 11,],                         // 6 : Grand Total Amount: Total Amount collected + Fees total amount collected, taxes on fees included.
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 7 : Delimiter (Constant value of ';')
		['name' => 'FEEFLD',                      'type' =>'composite',                                             'rep_min' =>1,    'rep_max' =>99,      // 8 AIRLINE TICKETING FEE FIELDIF NO DATA, NO BLANKS ARE IN BETWEEN DELIMITERS. IF DATA IS ENTERED BUT SOME FIELDS ARE NOT USED, THEY ARE BLANK FILLED. THIS IS A
		 'childs' => [
		   ['name'=> 'FEEC',                      'type' => 'AN',   'req' => 'M',                                   'len' => 2,],                          // 9 : Airline Ticketing Fee Code
		   ['name'=> 'FEESC',                     'type' => 'AN',   'req' => 'M',                                   'len' => 3,],                          // 10 : Airline Ticketing Fee Subcode
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 11 : Delimiter (Constant value of ';')
		   ['name'=> 'FEECN',                     'type' => 'AN',   'req' => 'M',                                   'len_min' => 1,   'len_max' => 10,],   // 12 : Airline Ticketing Fee Commercial name
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 13 : Delimiter (Constant value of ';')
		   ['name'=> 'FCURR',                     'type' => 'AN',   'req' => 'M',                                   'len' => 3,],                          // 14 : Currency Type Code
		   ['name'=> 'FEEA',                      'type' => 'NF',    'req' => 'M',                                   'len' => 11,],                         // 15 : Airline Ticketing Fee Amount (tax included)
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 16 : Delimiter (Constant value of ';')
		   ['name'=> 'FEETCUR',                   'type' => 'AN',                                                   'len' => 3,],                          // 17 : Tax on Fee Currency
		   ['name'=> 'FEETA',                     'type' => 'N',                                                    'len' => 11,],                         // 18 : Tax on Fee Amount
		   ['name'=> 'FEETC',                     'type' => 'AN',                  'requires' => ['FEETA'],   'len' => 2,],                          // 19 : Tax on Fee Code
		   ['name'=> 'DELIM',   'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 20 : Delimiter (Constant value of ';') (No delimiter follows the last Airline Service Fee Field Item)
		 ]],                                                                                                                                                      // 21 END OF COMPOSITE ELEMENT
	];

	// upgraded structure, defined in manual V6.23
	protected $structure_V623 = [
		['name'=> 'MSGTAG',     'val' => 'ATF-',  'type' => 'AN',   'req' => 'M',                                   'len' => 4,],                          // 1 : Message Tag (ATF-)
		['name'=> 'CURFEE',                       'type' => 'AN',                                                   'len' => 3,],                          // 2 : Currency Code
		['name'=> 'FEETOTL',                      'type' => 'NF',                                                   'len' => 11,],                         // 3 : Fees Total Amount collected, taxes on Fees included
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 4 : Delimiter (Constant value of ';')
		['name'=> 'CURGDT',                       'type' => 'AN',   'req' => 'M',                                   'len' => 3,],                          // 5 : Currency Code
		['name'=> 'GRDTOTL',                      'type' => 'NF',   'req' => 'M',                                   'len' => 11,],                         // 6 : Grand Total Amount: Total Amount collected + Fees total amount collected, taxes on fees included.
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 7 : Delimiter (Constant value of ';')
		['name' => 'FEEFLD',                      'type' =>'customCall', 'call'=> 'FEEFLD',                         'rep_min' =>1,    'rep_max' =>99,],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],
		['name'=> 'FEETTCUR',                     'type' => 'AN',   'req' => 'M',                                   'len' => 3,],                          // Tax on Fee total Currency
		['name'=> 'FEETTA',                       'type' => 'NF',   'req' => 'M',],                                    // Tax on Fee total Amount
	];

	// i created the new structure after reading MAIL:[AMA] ATF
	protected $structure_02 = [
		['name'=> 'MSGTAG',     'val' => 'ATF-',  'type' => 'AN',   'req' => 'M',                                   'len' => 4,],                          // 1 : Message Tag (ATF-)
		['name'=> 'CURFEE',                       'type' => 'AN',                                                   'len' => 3,],                          // 2 : Currency Code
		['name'=> 'FEETOTL',                      'type' => 'N',                                                    'len' => 11,],                         // 3 : Fees Total Amount collected, taxes on Fees included
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',   'req' => 'M',                                   'len' => 1,],                          // 4 : Delimiter (Constant value of ';')
		['name'=> 'CURGDT',                       'type' => 'AN',   'req' => 'M',                                   'len' => 3,],                          // 5 : Currency Code
		['name'=> 'GRDTOTL',                      'type' => 'N',    'req' => 'M',                                   'len' => 11,],                         // 6 : Grand Total Amount: Total Amount collected + Fees total amount collected, taxes on fees included.
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                                                   'len' => 1,],                          // 7 : Delimiter (Constant value of ';')

		['name'=> 'FCURR',                        'type' => 'AN',                                                   'len' => 3,],                          // 14 : Currency Type Code
		['name'=> 'FEEA',                         'type' => 'N',                                                    'len' => 11,],                         // 15 : Airline Ticketing Fee Amount (tax included)
	];


	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::isValid()
	 */
	public function isValid(){
		// use ['MSGTAG'] constant value to validate the whole line
		if(strpos($this->content, "ATF-")===0){
			return $this;
		}
		return false;
	}

	/**
	 * Custom-parse the FEEFLD field, because i cannot make the standard parser do its job correctly on these fields
	 * @return multitype:number unknown |NULL
	 */
	function parseFromStructure_v2_custom_FEEFLD($tree, $struct, $content, $index, $options){
		$regexp = "(?P<FEEC>[A-Z]{2})"
				."(?P<FEESC>[A-Z0-9]{3})"
				.";"
				."(?P<FEECN>[^;]+)?"
				.";"
				."(?P<FCURR>[A-Z]{3})"
				."(?P<FEEA>[0-9\.]*)[\s]*"
				.";"
				."("
					."(?P<FEETCUR>[A-Z]{3})"
					."(?P<FEETA>[0-9\.]*)[\s]*"
					.";"
				.")?"
				."("
					."(?P<FEETC>[A-Z]{2})[\s]*"
					.";"
				.")?"
				."";

		// do the actual matcing
		$matches = [];
		preg_match("/^{$regexp}/", substr($content, $index), $matches);

		if($matches){
			$tree['FEEC'] = $matches['FEEC'];
			$tree['FEESC'] = $matches['FEESC'];
			$tree['FEECN'] = $matches['FEECN'];

			$tree['FCURR'] = $matches['FCURR'];
			$tree['FEEA'] = (float)$matches['FEEA'];

			$tree['FEETCUR'] = $matches['FEETCUR'];
			$tree['FEETA'] = (float)$matches['FEETA'];

			$tree['FEETC'] = $matches['FEETC'];

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

		return null;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::parse()
	 */
	public function parse()
	{
		if(strlen($this->content)>50){
			$parsed = false;
			if(!$parsed){
				try{
					$this->tree = $this->parseFromStructure($this->structure_V620, $this->content);
					$parsed = true;
				}catch(\App\Amadeus\FileParser\ParserException $ex){}
			}

			if(!$parsed){
				try{
					$this->tree = $this->parseFromStructure($this->structure_V623, $this->content);
					$parsed = true;
				}catch(\App\Amadeus\FileParser\ParserException $ex){
				}
			}

			if(!$parsed){
				throw new \App\Amadeus\FileParser\ParserException("Cannot extract data from this ATF line");
			}
		}else{
			$this->tree = $this->parseFromStructure($this->structure_02, $this->content);
		}
		$this->postParse();
		return $this;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::postParse()
	 */
	protected function postParse()
	{
		parent::postParse();
		if($this->tree['FEEFLD']){
			$this->tree['FEEFLD'] = self::rtrimArr($this->tree['FEEFLD']);
		}
	}
}
