<?php
namespace App\Amadeus\StructureParser\Types;

/**
 * Form of payment handler object.
 * It contains a list of \App\Amadeus\StructureParser\Types\FormOfPaymentComponent
 * @author Lucian Sirbu
 *
 */
class FormOfPayment
{
	/**
	 * The internal list of components
	 * @var array, \App\Amadeus\StructureParser\Types\FormOfPaymentComponent
	 */
	public $components = 		[];

	/**
	 * Insert a new component in the internal list
	 * @param \App\Amadeus\StructureParser\Types\FormOfPaymentComponent $component
	 */
	public function insert(\App\Amadeus\StructureParser\Types\FormOfPaymentComponent $component)
	{
		$this->components[] = $component;
	}

	/**
	 * Load the payments from the $PAYMNT list. Used internally by Amadeus structure parsers
	 * @param array $PAYMNT
	 */
	public function load_PAYMNT($PAYMNT)
	{
		if($PAYMNT['new']){
			foreach ($PAYMNT['new'] as $PAY) {
				// FormOfPaymentComponentGovernment
				if ($PAY['type']=='government') {
					$p = new \App\Amadeus\StructureParser\Types\FormOfPaymentComponentGovernment();
					$p->type = 				$PAY['type'];
					$p->accountCode = 		$PAY['accountCode'];
					$p->travelOrderNo = 	$PAY['travelOrderNo'];
					$p->travelOrderDateStr= $PAY['travelOrderDateStr'];
				}
				else {
					$p = new \App\Amadeus\StructureParser\Types\FormOfPaymentComponentCC();
					$p->type = 				$PAY['type'];
					$p->amount = 			$PAY['amount'];
					$p->currency = 			$PAY['currency'];
					$p->companyIndicator = 	$PAY['companyIndicator'];
					$p->number = 			$PAY['number'];
					$p->currency = 			$PAY['currency'];
					$p->approvalCode = 		$PAY['approvalCode'];
					$p->invoiceNo = 		$PAY['invoiceNo'];
					$p->setCcExpiryDate($PAY['expDate']);
				}

				$this->insert($p);
			}
		}
	}

	/**
	 * Get the a short human-readable notation for the forms of payments used in this transaction.
	 * Used mainly for compatibility with the old code or for displaying in the interface
	 * @return string
	 */
	public function getShortNotation()
	{
		$return = [];
		foreach($this->components as $comp){
			$return[] = $comp->type;
		}
		sort($return);
		$return = array_unique($return);
		return implode("+", $return);
	}

	/**
	 * Check if the form of payment contains a specific form of payment.
	 * Mainly used to check if it was paid with CC
	 * @param string $payment
	 * @return boolean
	 */
	public function isPaidWith($paymentType)
	{
		foreach($this->components as $comp){
			if($comp->type==$paymentType){
				return true;
			}
		}
		return false;
	}

	/**
	 * Get a list of payments by their type. An index can be specified to return only that specific item in the final list
	 * @return array:FormOfPaymentComponent
	 */
	public function getPaymentByType($paymentType, $index=null)
	{
		$return = [];
		foreach($this->components as $comp){
			if($comp->type==$paymentType){
				$return[] = $comp;
			}
		}

		if($index===null){
			return $return;
		}

		return $return[$index];
	}

	/**
	 * Check if the object has any data inserted
	 */
	public function hasData()
	{
		return (bool)$this->components;
	}
}
