<?php

namespace App\ExportData\ExportFile;

class CurrencyRates extends GeneralAbstract
{

	/**
	 * General description of the entity.
	 * @var String
	 */
	protected $entityType = 'currencyExchange';

	/**
	 * General file name
	 * @var String
	 */
	protected $fileName = 'currencyExchange';

	/**
	 * The XML root tag name.
	 * @var String
	 */
	protected $rootTag = 'currencyExchangeRates';

	/**
	 * The allowed operation types - they are checked in the constructor.
	 * @var array
	 */
	protected $allowedOperationTypes = ['add', 'update', 'delete'];

	/**
	 * The date of the rates export.
	 * @var String
	 */
	protected $date;

	/**
	 * The market which indicates the reference currency.
	 * @var String
	 */
	protected $market;

	/**
	 * {@inheritDoc}
	 * @see \App\ExportData\ExportFile\GeneralAbstract::checkIfInactiveEntity()
	 */
	protected function checkIfInactiveEntity()
	{
		return !\App\Config\ExportData_ActiveEntities::getInstance()->get('currencyExchange');
	}

	/**
	 * Populates the date and market of the currency export.
	 * @param String $date
	 * @param String $market
	 */
	public function __construct($date, $market)
	{
		// check root tag
		if (!$this->rootTag) {
			throw new \Exception('The root tag was not set for the class `'.get_called_class().'`.');
		}

		$this->date = $date;
		$this->market = $market;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ExportData\ExportFile\GeneralAbstract::populateXml()
	 */
	protected function populateXml(\SimpleXMLElement $xml, \App\MessagesCollector $msgs)
	{

		// get global configs
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// CHECK INPUT ////////////////////////////////////////////////////////////////////////////
		// check date (if date not valid - return)
		if (!validDate($this->date) ) {
			$msgs->addError("The date of the currency exchange rates is not valid: `{$this->date}`.");
		}
		// check market
		if (!$this->market or !in_array($this->market, $CFG['currencies']['markets'])) {
			$msgs->addError("The market `{$this->market}` is not set/valid for currency exchange rate export.");
		}

		// GET THE RATES AND THE PARITIES /////////////////////////////////////////////////////////
		$ratesObj = \App\CurrencyExchange\Date::getInstance($this->date, $this->market);
		$rates = $ratesObj->getRates();

		// list of currencies types
		$currenciesTypesList=\App\Config\CurrencyTypes::getInstance()->getSimpleActive();

		// CREATE XML CONTENT /////////////////////////////////////////////////////////////////////

		// add main attributes
		$xml['date']=$this->date;
		$xml['market']=$this->market;
		$xml['exportTime']=date("Y-m-d\TH:i:s");

		// loop through currency types
		foreach($currenciesTypesList as $currTypeCode=>$currTypeName) {

			$currTypeObj = $xml->addChild('currencyType');

			// start currency type
			$currTypeObj['code'] = htmlUnclean($currTypeCode);
			$currTypeObj['name'] = htmlUnclean($currTypeName);

			// get currencies values
			$currenciesValues = $rates[$currTypeCode];

			// add each currency value
			if (is_array($currenciesValues)) {
				foreach ($currenciesValues as $currCode=>$value) {
					$currCodeObj = $currTypeObj->addChild('currency', $value);
					$currCodeObj['code'] = htmlUnclean($currCode);
				}
			}

		}

	}

}

