<?php
namespace App\Config;

/**
 * The class is used for managing the values of autobalancing scoring criteria
 */
class AutobalancingScoringCriteria extends XmlBasicAbstract
{

	/**
	 * Stores the configuration file (relative to the "files/config/" dir). For config files stored
	 * inside subdirectories, the subdirectory is included (Ex: services/servicesList.xml).
	 * @var String
	 */
	protected $fileName = 'autobalancingScoringCriteria.xml';

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::getAll_rawData_extractDataFromFile()
	 */
	protected function getAll_rawData_extractDataFromFile(\SimpleXMLElement $xml)
	{

		// prepare the list of autobalancing scoring criteria
		$autobalancingScoringCriteria = [];

		foreach ($xml->criterion as $criterion) {
			foreach ($criterion->children() as $criterionKey=>$criterionValue) {
				$autobalancingScoringCriteria[(string)$criterion->attributes()['name']][$criterionKey] = (float)$criterionValue;
			}
		}

		return $autobalancingScoringCriteria;
	}

	/**
	 * Get default values for autobalancing scoring criteria
	 */
	public function getDefaultValuesForAutobalancingScoringCriteria()
	{
		return $this->getAll_rawData_fileNotExists();
	}



	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::getAll_rawData_fileNotExists()
	 */
	protected function getAll_rawData_fileNotExists()
	{

		return [
			'ticketNumber' => 					[ 'equal' => 350, 'notEqual' => -150],
			'externalReservationId' => 			[ 'equal' => 150, 'notEqual' => -80],
			'externalReservationDate' => 		[ 'equal' => 120, 'notEqual' => -20],
			'confirmationNumber' => 			[ 'equal' => 150, 'notEqual' => -80],
			'idService' =>						[ 'equal' => 400, 'notEqual' => -250],
			'reservationId' =>					[ 'equal' => 160, 'notEqual' => -120],
			'reservationNumber' =>				[ 'equal' => 180, 'notEqual' => -80],
			'reservationCode' =>				[ 'equal' => 180, 'notEqual' => -50],
			'idPassenger' =>					[ 'equal' => 120, 'notEqual' => -40],
			'checkIn' =>						[ 'equal' => 110, 'notEqual' => -20],
			'serviceCode' => 					[ 'equal' => 90, 'notEqual' => -70],
			'idSupplier' => 					[ 'equal' => 50, 'notEqual' => -50],
			'idSupplierService' =>				[ 'equal' => 100, 'notEqual' => -50],
			'countryCode' =>					[ 'equal' => 40, 'notEqual' => -80],
			'cityId' =>							[ 'equal' => 90, 'notEqual' => -20],
			'price'	=>							[ 'equal' => 240, 'notEqual' => -100, 'tolerance'=>0.07], // tolerance is the percent calculated
		];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\BaseAbstract::save_check()
	 */
	protected function save_check()
	{
		//Load Lang
		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		foreach ($this->getDefaultValuesForAutobalancingScoringCriteria() as $criterionName=>$criterionValues) {
			$criteriaName[] = $criterionName;
		}

		foreach ($this->itemsList as $autobalancingScoringCriteriaKey => $autobalancingScoringCriteriaValuesList) {
			//check if the criterion name is valid
			if (!in_array($autobalancingScoringCriteriaKey, $criteriaName)) {
				$this->addErrorMessage(sprintf($_LANG['Admin']['Config']['InvoiceBalances']['AutobalancingScoringCriteria']['process']['messages']['criterionName'], $autobalancingScoringCriteriaKey ));
			}
			foreach ($autobalancingScoringCriteriaValuesList as $scoringKey => $scoringValue) {
				//check if the scoring value is numeric
				if (!is_numeric($scoringValue)) {
					$this->addErrorMessage(sprintf($_LANG['Admin']['Config']['InvoiceBalances']['AutobalancingScoringCriteria']['process']['messages']['value'], $autobalancingScoringCriteriaKey,  $scoringKey));
				}

				//check if the tolerance > 0
				if (($autobalancingScoringCriteriaKey == 'tolerance') && !($scoringValue > 0)) {
					$this->addErrorMessage($_LANG['Admin']['Config']['InvoiceBalances']['AutobalancingScoringCriteria']['process']['messages']['greaterThanZero']);
				}
			}
		}

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::save_write_do_getXml()
	 */
	protected function save_write_do_getXml()
	{

 		// start composing the XML object
 		$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><autobalancingScoringCriteria />');

 		// get a list with autobalancing scoring criteria
 		$autobalancingScoringCriteriaList = self::getAdmin()->getAll();

		// save in XML file
 		foreach ($autobalancingScoringCriteriaList as $autobalancingScoringCriteriaKey => $autobalancingScoringCriteriaValuesList) {
 			$autobalancingScoringCriteriaTag = $xml->addChild('criterion');
 			$autobalancingScoringCriteriaTag->addAttribute('name', $autobalancingScoringCriteriaKey);
			foreach ($autobalancingScoringCriteriaValuesList as $scoringKey => $scoringValue) {
				$autobalancingScoringCriteriaTag->addChild($scoringKey , $scoringValue);
			}
		}

		// return the simplexml object
		return $xml;
	}

}
