<?php
namespace App\Bsp;

class BspRow
{
	/**
	 * For files with multiple groups in them, define in what group this ticket takes part
	 * @var int - the index on the group in $filesDetails[]
	 */
	public $group = 0;

	/**
	 * Supplier code (001, 055, etc), numeric, used as tktCode in TINA
	 * @var string
	 */
	public $supplierCode3 = null;	/* $code3=055, 192 etc, codeAirline=1A, RO etc, $codeIata = 69210643 etc*/

	/**
	 * Supplier code (KZ, LH, etc), alphanumeric, used as codeAirline in TINA
	 * @var string
	 */
	public $supplierCode2 = null;	/* $code3=055, 192 etc, codeAirline=1A, RO etc, $codeIata = 69210643 etc*/

	/**
	 * Ticket number
	 * @var string
	 */
	public $ticketNumber = null;

	/**
	 * Ticket date
	 * @var string
	 */
	public $ticketDate = null;

	/**
	 * The trip type (international/domestic)
	 * @var string
	 */
	public $locationType = null;	// I=international, D=domestic, ''=unknown

	/**
	 * Supplier total price
	 * @var \App\Bsp\BspRowPrice
	 */
	public $priceSupplierCash = null;

	/**
	 * Supplier total price
	 * @var \App\Bsp\BspRowPrice
	 */
	public $priceSupplierCc = null;

	/**
	 * Tax total
	 * @var \App\Bsp\BspRowPriceTax
	 */
	public $priceTaxCash = null;

	/**
	 * Tax total
	 * @var \App\Bsp\BspRowPriceTax
	 */
	public $priceTaxCc = null;

	/**
	 * Commission percent
	 * @var float
	 */
	public $priceCommissionPercent = null;

	/**
	 * Commission
	 * @var \App\Bsp\BspRowPrice
	 */
	public $priceCommission = null;

	/**
	 * Commission
	 * @var \App\Bsp\BspRowPrice
	 */
	public $priceCommissionVat = null;	// also called taxOnCommision, it represents the VAT on commission?

	/**
	 * Commission supplementary
	 * This commission component is NOT included in the GDS prices. I'm not expecting to see this reflected in TINA
	 * @var \App\Bsp\BspRowPrice
	 */
	public $priceCommissionSupplementary = null;

	/**
	 * Commission
	 * @var \App\Bsp\BspRowPrice
	 */
	public $priceBalancePayable = null;

	/**
	 * The list of form of payments
	 * @var array:\App\Bsp\BspRowFormOfPaymentComponent
	 */
	public $formOfPayments = [];

	/**
	 * List of tickets in conjunction with the current one
	 * @var array
	 */
	public $conjunctions = [];

	/**
	 * The description as extracted from the file(may be multiline)
	 * @var string
	 */
	public $description = null;

	/**
	 * Extra info, contains CPUI for example, internal sections, etc
	 * Some info can be extracted from the description field
	 * @var \App\Bsp\BspRowExtraData
	 */
	public $extraData = null;

	/**
	 * Relations with other tickets
	 * @var \App\Bsp\BspRowRelations
	 */
	public $relations = null;

	/**
	 * Internal data, directly from the file parser
	 * @var mixed
	 */
	public $rawData = null;

	/**
	 * Build the current object
	 */
	public function __construct()
	{
		$this->priceSupplierCash = 	new BspRowPrice();
		$this->priceSupplierCc = 	new BspRowPrice();

		$this->priceTaxCash = 		new BspRowPriceTax();
		$this->priceTaxCc = 		new BspRowPriceTax();

		$this->priceCommission = 	new BspRowPrice();
		$this->priceCommissionVat = new BspRowPrice();
		$this->priceCommissionSupplementary = new BspRowPrice();
		$this->priceBalancePayable = new BspRowPrice();

		$this->extraData = new BspRowExtraData();
		$this->relations = new BspRowRelations();
	}

	/**
	 * Validate the current object
	 * @throws BspRowException
	 */
	public function validate()
	{
		if (!$this->ticketNumber) {
			throw new BspRowException(sprintf("Ticket number is not set"));
		}
		if (!$this->ticketDate) {
			throw new BspRowException(sprintf("Ticket date is not set, ticket %s", $this->ticketNumber));
		}
	}

	/**
	 * Add a specific form of payment
	 * @param string $type
	 * @return \App\Bsp\BspRowFormOfPaymentComponent
	 */
	public function addFormOfPayment($type)
	{
		$fop = new \App\Bsp\BspRowFormOfPaymentComponent();
		$fop->type = $type;
		$this->formOfPayments[]= $fop;
		return $fop;
	}

	/**
	 * Check if a certain form of payment is present
	 * @return bool
	 */
	public function isPaidWith($type)
	{
		foreach($this->formOfPayments as $fop) {
			 if ($fop->type==$type) {
			 	return true;
			 }
		}
		return false;
	}

	public function get($type)
	{

	}

	/**
	 * Invert the price sign. Used for refunds, where everything is negative
	 * @return \App\Bsp\BspRow
	 */
	public function invertSign()
	{
		$this->priceSupplierCash->invertSign();
		$this->priceSupplierCc->invertSign();
		$this->priceCommission->invertSign();
		$this->priceCommissionVat->invertSign();
		$this->priceCommissionSupplementary->invertSign();
		$this->priceBalancePayable->invertSign();

		$this->priceTaxCash->invertSign();
		$this->priceTaxCc->invertSign();

		return $this;
	}

	/**
	 * Get the name of the components that have a value defined
	 * @return array
	 */
	function getDefinedComponents()
	{
		$components = [
			'priceSupplierCash',
			'priceSupplierCc',
			'priceCommission',
			'priceCommissionVat',
			'priceCommissionSupplementary',
			'priceBalancePayable',
			'priceTaxCash',
			'priceTaxCc',
		];

		$ret = [];
		foreach ($components as $component) {
			if ($this->{$component}->amount!=0) {
				$ret[]= $component;
			}
		}

		return $ret;
	}

	/**
	 * Reformat internal values, making sure we dont hit the -0 bug, and that all the values are in the required interval
	 * @return \App\Utils\BaseParser\Structure\StructureParser\Types\Price
	 * @see \App\Utils\BaseParser\Structure\StructureParser\Types\Price
	 */
	public function clampPrices()
	{
		foreach ($this->getDefinedComponents() as $comp) {
			$this->{$comp}->clampValue();
		}

		return $this;
	}

	/**
	 * Get a custom var
	 * @param string $key
	 */
	public function __get($key)
	{
		$ret = null;
		switch ($key) {
			// case 'ticketPriceSum':
			// 	$ret = $this->priceSupplierCash->amount + $this->priceSupplierCc->amount;
			// break;
			// case 'taxPriceSum':
			// 	$ret = $this->priceTaxCash->amount + $this->priceTaxCc->amount;
			// break;

			case 'currency':
				$currencies = array_unique(array_filter([
					$this->priceBalancePayable->currency,
					$this->priceSupplierCash->currency,
					$this->priceSupplierCc->currency,
					$this->priceTaxCash->currency,
					$this->priceTaxCc->currency,
					$this->priceCommission->currency,
					$this->priceCommissionVat->currency,
				]));
				if (count($currencies)>1) {
					throw new BspRowException("This BSP row taxes contain more than one currency");
				}
				$ret = reset($currencies);
			break;
		}

		return $ret;
	}

	/**
	 * Setting custom vars is disabled
	 * @param string $key
	 * @param mixed $value
	 * @throws BspRowException
	 */
	public function __set($key, $value)
	{
		throw new BspRowException("Cannot set undefined property {$key}");
	}
}
