<?php
namespace App\Amadeus\FileParser;

class Line_MFP extends \App\Amadeus\FileParser\Line_FPAbstract
{
	/**
	 * 4.142. MFP MCO/EMD Form of Payment
	 * see page 352(394)
	 *
	 * DETAILS
	 */

	// Ex: MFPINV;L8
	protected $structure_RO = [
		['name'=> 'MSGTAG',     'val' => 'MFP',   'type' => 'AN',      'req' => 'M',  'len' => 3,],                  // 1 : Message Tag (MFP)
		['name'=> 'PAYMNT',                       'type' => 'AN',],                  // 2 : Form of Payment
		['name'=> 'LINEASS',    'val' => 'L',     'type' => 'ASSOC:loose',            'len' => 1,],
		['name'=> 'PSGASS',     'val' => 'P',     'type' => 'ASSOC:loose',            'len' => 1,],
		['name'=> 'TSMASS',     'val' => 'D',     'type' => 'ASSOC:loose',            'len' => 1,],                  // 38 : TSM association identifier (constant value of 'D')
	];


	// Ex: MFPINV;;;D1
	protected $structure_EE_Nautilus = [
		['name'=> 'MSGTAG',     'val' => 'MFP',   'type' => 'AN',      'req' => 'M',  'len' => 3,],                  // 1 : Message Tag (MFP)
		['name'=> 'PAYMNT',                       'type' => 'AN',],                  // 2 : Form of Payment
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'TSMASS',     'val' => 'D',     'type' => 'ASSOC:loose',            'len' => 1,],                  // 38 : TSM association identifier (constant value of 'D')
	];

	// general field, as described in the doc
	// Ex from BG/PolyTours which doesn't comply with the docs(has 2 extra DELIMs): MFPCCAX000000000002001/1215/A151778;;;;;;;;;;;;;;;;;;;;D1
	protected $structure_full = [
		['name'=> 'MSGTAG',     'val' => 'MFP',   'type' => 'AN',      'req' => 'M',  'len' => 3,],                          // 1 : Message Tag (MFP)
		['name'=> 'PAYMNT',                       'type' => 'AN',],                                       // 2 : Form of Payment
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 3 : Delimiter (constant value of ';')
		['name'=> 'ECI',                          'type' => 'AN',],                         // 4 : Visa's Electronic Commerce Indicator / Mastercard's UCAF Collection indicator
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 5 : Delimiter (Constant value of ';')
		['name'=> 'XID',                          'type' => 'AN',],                                                          // 6 : Transaction ID
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 7 : Delimiter (Constant value of ';')
		['name'=> 'CCAV',                         'type' => 'AN',],                                                          // 8 : Visa's Cardholder Authentication Verification Value / Mastercard's UCAF Authentication data
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 9 : Delimiter (Constant value of ';')
		['name'=> 'CRYPCOMP',                     'type' => 'AN',                     'len' => 1,],                          // 10 : Computation method of electronic commerce indicator cryptogram
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 11 : Delimiter (Constant value of ';')
		['name'=> 'VADRES',                       'type' => 'AN',                     'len' => 6,],                          // 12 : Result of secured payment architecture VADS use
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 13 : Delimiter (Constant value of ';')
		['name'=> 'SECMODE',                      'type' => 'N',                      'len' => 2,],                          // 14 : Secured mode of modified transaction
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 15 : Delimiter (Constant value of ';')
		['name'=> 'SECTYPE',                      'type' => 'N',                      'len' => 2,],                          // 16 : Secured type of electronic transaction
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 17 : Delimiter (Constant value of ';')
		['name'=> 'TECHENV',                      'type' => 'N',                      'len' => 2,],                          // 18 : Technical and allowed enrironment
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 19 : Delimiter (Constant value of ';')
		['name'=> 'CUSTINST',                     'type' => 'AN',                     'len' => 1,],                          // 20 : Customer Instruction (Barclays Specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 21 : Delimiter (Constant value of ';')
		['name'=> 'ACI',                          'type' => 'AN',                     'len' => 1,],                          // 22 : Authorization Characteristics Indicator (for Visa transactions, US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 23 : Delimiter (Constant value of ';')
		['name'=> 'CAT',                          'type' => 'AN',                     'len' => 1,],                          // 24 : Cardholder-Activated Terminal Indicator (for MasterCard transactions, US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 25 : Delimiter (Constant value of ';')
		['name'=> 'BKNETREF',                     'type' => 'AN',                     'len_min' => 1,   'len_max' => 9,],    // 26 : Banknet Reference Number (for MasterCard transactions, US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 27 : Delimiter (Constant value of ';')
		['name'=> 'BKNETDTE',                     'type' => 'AN',                     'len' => 4,],                          // 28 : Banknet date (for MasterCard transactions, US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 29 : Delimiter (Constant value of ';')
		['name'=> 'VALCODE',                      'type' => 'AN',                     'len' => 4,],                          // 30 : Validation Code (for Visa transactions, US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 31 : Delimiter (Constant value of ';')
		['name'=> 'TRANSID',                      'type' => 'N',                      'len' => 15,],                         // 32 : Transaction identifier (for Visa transactions, US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],                          // 33 : Delimiter (Constant value of ';')
		['name'=> 'RESPCODE',                     'type' => 'AN',                     'len' => 2,],                          // 34 : Authorization response code (US Bank specific)
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],
		['name'=> 'LINEASS',    'val' => 'L',    'type' => 'ASSOC:loose',],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],
		['name'=> 'PSGASS',     'val' => 'P',    'type' => 'ASSOC:loose',],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],                          // 37 : Delimiter (Constant value of ';')
		['name'=> 'TSMID',      'val' => 'D',     'type' => 'AN',                     'len' => 1,],                          // 38 : TSM association identifier (constant value of 'D')
		['name'=> 'TSMNBR',                       'type' => 'N',       'req' => 'M',  'requires' => ['TSMID',],],    // 39 : TSM number
	];

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

		return false;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::parse()
	 */
	public function parse()
	{
		$parsed = false;

		$exception = null;

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

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

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

		if(!$parsed){
			throw new \App\Amadeus\FileParser\ParserException(sprintf("Unknown line structure for the MFP line: %s", $exception->getMessage()), null, $exception);
		}

		return $this;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::postParse()
	 */
	protected function postParse()
	{
		parent::postParse();

		$this->FP_autoParse();
	}
}

