<?php

namespace App\Config;

/**
 * The class is used for managing the list of enables/disabled client services
 * @author Bogdan
 */
class ClientServices 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=CONFIG_FILE_SERVICE_DISABLEDCLIENTSERVICES;

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

		// get the list of disabled services grouped by location
		$disabledServiceList=[];

		// loop through each location
		foreach ($xml->location as $countryCriteriaList) {

			// scan each service code
			foreach ($countryCriteriaList as $disabledService) {
				$disabledServiceList[(string)$disabledService['code']][(string)$countryCriteriaList['code']] =true;
			}
		}

		// get the list with all services (activated and disabled) grouped after service code
		$result=[];
		// scan each service
		foreach (Services_Basic::getInstance()->getListLinear() as $serviceCode => $serviceDetails) {

			// scan each country criteria
			foreach ($CFG['countriesCriteria'] as $countryCriteria) {

				// disabled services
				if ( $disabledServiceList[$serviceCode][$countryCriteria] ) {
					$result[$serviceCode][$countryCriteria]=false;
				// enabled services
				} else {
					$result[$serviceCode][$countryCriteria]=true;
				}
			}
		}
		return $result;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::getAll_rawData_fileNotExists()
	 */
	protected function getAll_rawData_fileNotExists() {
		global $CFG;
		// get the list with all services (activated and disabled) grouped after service code
		$result=[];
		// scan each service
		foreach (Services_Basic::getInstance()->getListLinear() as $serviceCode => $serviceDetails) {

			// scan each country criteria
			foreach ($CFG['countriesCriteria'] as $countryCriteria) {
				$result[$serviceCode][$countryCriteria]=true;
			}
		}
		return $result;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\BaseAbstract::save_check()
	 */
	protected function save_check() {
		global $CFG;

		// get allowed service codes
		$allowedServices=Services_Basic::getInstance()->getListLinear();

		// scan each disabled service
		foreach ($this->itemsList as $serviceCode => $serviceList) {

			// check service code
			if ( !$allowedServices[$serviceCode] )
				$this->addErrorMessage('An invalid service code was requested: '.$serviceCode);

			// loop through each country criteria and check their disabled services
			foreach ($serviceList as $countryCriterion => $disabledServicesList) {
				// check location code
				if ( !in_array($countryCriterion, $CFG['countriesCriteria']) ) {
					$this->addErrorMessage('An invalid country criterion was requested: '.$countryCriterion);
				}
			}

		}
	}

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

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

		// get a list with disabled services grouped by country criteria
		$disabledServicesList=[];

		// loop through each location and set the new disabled service
		foreach ($CFG['countriesCriteria'] as $countryCriterion) {
			// loop through each service
			foreach (Services_Basic::getInstance()->getListLinear() as $serviceCode => $serviceDetails) {
				if ( !$this->itemsList[$serviceCode][$countryCriterion] ) {
					$disabledServicesList[$countryCriterion][$serviceCode]=true;
				}
			}
		}

		// save in XML file
		foreach ($disabledServicesList as $countryCriterion => $servicesList) {
			$locationTag=$xml->addChild('location');
			$locationTag['code'] = $countryCriterion;
			foreach ($servicesList as $serviceCode => $disabledService) {
				// if the field is false (not enabled) - save it into the XML
				$serviceTag = $locationTag->addChild('service');
				$serviceTag['code'] = $serviceCode;
			}
		}

		// return the simplexml object
		return $xml;
	}


	/**
	 * Verify if a service is active based on the service code and country criterion
	 * @param string $serviceCode
	 * @param string $criterion
	 * @return boolean
	 */
	public function isActiveOnCriterion($serviceCode, $criterion)
	{
		$serviceConfig = $this->get($serviceCode);
		return (bool)$serviceConfig[$criterion];
	}

	/**
	 * Verify if a service is active based on the service Code. If all country criteria are set to false, then it will return false.
	 * @param string $serviceCode
	 * @throws \Exception The exception is thrown if the service code is not valid
	 * @return boolean
	 */
	public function isActive($serviceCode)
	{

		if (!$this->get($serviceCode)) {
			throw new \Exception("The service code `{$serviceCode}` is not valid.");
		}

		$serviceConfig = $this->get($serviceCode);
		return (bool)array_filter($serviceConfig);
	}

}
