<?php

namespace App\Form\Input;

class Select_CreditCards extends \Engine\Form\Input\Select
{

	/**
	 * Indicates if the agency CCs are included in the selector.
	 * @var string
	 */
	protected $includeAgency = false;

	/**
	 * Indicates if the corporate CCs are included in the selector (by corporate ids).
	 * @var array
	 */
	protected $includeIdCorporates = [];

	/**
	 * Indicates if the customer CCs are included in the selector (by customer ids).
	 * @var array
	 */
	protected $includeIdCustomers = [];

	/**
	 * Indicates the manual CCs.
	 * @var array
	 */
	protected $includeIdManualCards = [];

	/**
	 *  Indicates if the new (manual) CC can be added.
	 * @var string
	 */
	protected $includeNewCard = false;

	/**
	 * Toggles agency CCs inside the selector.
	 */
	public function addAgencyCards($include = true)
	{
		$this->includeAgency = $include;
	}

	/**
	 * Sets the corporate id and will attempt to inject corporate CCs.
	 */
	public function addCorporatesCards(array $idCorporates)
	{
		$this->includeIdCorporates = $idCorporates;
	}

	/**
	 * Sets the customer id and will attempt to inject customer CCs.
	 */
	public function addCustomersCards(array $idCustomers)
	{
		$this->includeIdCustomers = $idCustomers;
	}

	/**
	 * Toggles manual CCs inside the selector.
	 */
	public function addManualCards(array $idCreditCards)
	{
		$this->includeIdManualCards = $idCreditCards;
	}

	public function addNewCard($include=true)
	{
		$this->includeNewCard = $include;
	}

	/**
	 * {@inheritDoc}
	 * @see \Engine\Form\Input\Select::determinePossibleValues()
	 */
	protected function determinePossibleValues()
	{

		$values = [];

		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		$creditCardsStorage = new \App\Utils\CreditCard\Storage();

		// include all agency CCs
		if ($this->includeAgency) {
			$agencyCcsObj = new \App\EntityList\AgencyCreditCards();
			$agencyCcsObj->setColumns(['name', 'idCreditCard']);
			$agencyCcs = $agencyCcsObj->exportRecords();
			if ($agencyCcs) {
				$values[ $_LANG['offers']['formOfCashing']['CcTypes']['agency'] ] = [];
				foreach ($agencyCcs as $agCcDet) {
					$ccDet = $creditCardsStorage->getCard($agCcDet['idCreditCard']);
					$values['Agency']['agency_'.$agCcDet['id']] = $agCcDet['name'].': '.($ccDet['company']?($ccDet['company'].' - '):'') . $ccDet['truncatedNumber'];
				}
			}
		}

		// include corporate cards
		if ($this->includeIdCorporates) {
			$corpCcsObj = new \App\EntityList\CorporateCreditCards();
			$corpCcsObj->setColumns(['idCreditCard', 'idCorporate']);
			$corpCcsObj->setFilter('idCorporate', 'IN', $this->includeIdCorporates);
			$corpCcsObj->setOrderBy('idCorporate', 'ASC');
			$corpCcs = $corpCcsObj->exportRecords();
			if ($corpCcs) {
				foreach ($corpCcs as $corpCcDet) {
					$cat = $_LANG['offers']['formOfCashing']['CcTypes']['corporate'].' - '.\Entity\Corporate::select($corpCcDet['idCorporate'])->alias;
					if (!array_key_exists($cat, $values)) {
						$values[$cat] = [];
					}
					$ccDet = $creditCardsStorage->getCard($corpCcDet['idCreditCard']);
					$values[$cat]['corporate_'.$corpCcDet['id']] = ($ccDet['company']?($ccDet['company'].' - '):'') . $ccDet['truncatedNumber'];
				}
			}
		}

		// include customers cards
		if ($this->includeIdCustomers) {
			$custCcsObj = new \App\EntityList\CustomerCreditCards();
			$custCcsObj->setColumns(['idCreditCard', 'idCustomer']);
			$custCcsObj->setFilter('idCustomer', 'IN', $this->includeIdCustomers);
			$custCcsObj->setOrderBy('idCustomer', 'ASC');
			$custCcs = $custCcsObj->exportRecords();
			if ($custCcs) {
				foreach ($custCcs as $custCcDet) {
					$cat = $_LANG['offers']['formOfCashing']['CcTypes']['customer'].' - '.\Entity\Customer::select($custCcDet['idCustomer'])->name;
					if (!array_key_exists($cat, $values)) {
						$values[$cat] = [];
					}
					$ccDet = $creditCardsStorage->getCard($custCcDet['idCreditCard']);
					$values[$cat]['customer_'.$custCcDet['id']] = ($ccDet['company']?($ccDet['company'].' - '):'') . $ccDet['truncatedNumber'];
				}
			}
		}

		// include customers cards
		if ($this->includeIdManualCards) {
			foreach ($this->includeIdManualCards as $idCreditCard) {
				$cat = $_LANG['offers']['formOfCashing']['CcTypes']['manual'];
				if (!array_key_exists($cat, $values)) {
					$values[$cat] = [];
				}
				$ccDet = $creditCardsStorage->getCard($idCreditCard);
				$values[$cat]['manual_'.$idCreditCard] = ($ccDet['company']?($ccDet['company'].' - '):'') . $ccDet['truncatedNumber'];
			}
		}

		// add manual (if needed)
		if ($this->includeNewCard) {
			$values['new'] = $_LANG['offers']['formOfCashing']['CcTypes']['new'];
		}

		return $values;
	}

}
