<?php

namespace App\Config;

class AgencyCostCenters extends XmlDynamicRecordsAbstract
{

	/**
	 * 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_AGENCYCOSTCENTERS;


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_list()
	 */
	protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml)
	{
		// gets the final list of items to be returned
		$return=[];
		foreach($xml->costCenter as $cc) {
			$return [ (String)$cc['code'] ] = [
				'name'=>(String)$cc, 'disabled'=>(Int)$cc['disabled']
			];
		}
		return $return;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_default()
	 */
	protected function getAll_rawData_extractDataFromFile_default(\SimpleXMLElement $xml)
	{
		return (String)$xml['default'];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_mandatory()
	 */
	protected function getAll_rawData_extractDataFromFile_mandatory(\SimpleXMLElement $xml)
	{
		return false;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::itemCheck()
	 */
	protected function itemCheck($code, $itemDetails)
	{
		// check itemDetails
		if (!is_string($itemDetails['name'])) {
			throw new \Exception('Agency cost center name must be set as a string.');
		}
		// check code
		if (!$code) {
			$this->addErrorMessage('The agency cost center code was not defined.');
		}
		// check code
		if (!ctype_alnum($code)) {
			$this->addErrorMessage('The agency cost center code must contain only alphanumeric chars.');
		}
		// check name
		if (!$itemDetails['name']) {
			$this->addErrorMessage('The `agency cost center name` was not defined.');
		}
		// check code length
		if (mb_strlen($code)>20) {
			$this->addErrorMessage('The `agency cost center` code length must be smaller than 20 chars.');
		}
		// check name length
		if (mb_strlen($itemDetails['name'])>50) {
			$this->addErrorMessage('The `agency cost center` length must be smaller than 50 chars.');
		}

		// check if it contains special chars
		if (htmlClean($itemDetails['name']) != $itemDetails['name']){
			$this->addErrorMessage('The `agency cost center name` cannot contain html special characters.');
		}

		// check that the name does not already exist
		foreach($this->itemsList as $currCode=>$currDetails) {
			if ($currCode!=$code and mb_strtoupper($itemDetails['name'])==mb_strtoupper($currDetails['name'])) {
				$this->addErrorMessage("This agency cost center `{$currDetails['name']}` already exists.");
			}
		}
	}


	/**
	 * Check that the default item is not disabled.
	 * @see \App\Config\BaseAbstract::save_check()
	 */
	protected function save_check()
	{
		if ($this->defaultItem and $this->itemsList[$this->defaultItem]['disabled']) {
			$this->addErrorMessage("You cannot disable the default cost center.");
		}
	}


	/**
	 * (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"?><agencyCostCenters/>');

		// LOOP THROUGH EACH SERVICE CODE AVAILABLE IN THE APP ------------------------------------
		foreach($this->itemsList as $ccCode=>$ccDetails) {
			$servObj = $xml->addChild('costCenter', $ccDetails['name']);
			$servObj['code'] = $ccCode;
			if ($ccDetails['disabled']) {
				$servObj['disabled'] = 1;
			}
		}

		// set the default flag
		$xml['default']=$this->defaultItem;

		// return the simplexml object
		return $xml;

	}


	/**
	 * Returns the list of active cost centers
	 * @return Array - associative array of code=>name
	 */
	public function getListActive()
	{
		$ccs = [];
		foreach($this->getAll() as $ccCode=>$ccDet) {
			if (!$ccDet['disabled']) {
				$ccs[$ccCode] = $ccDet['name'];
			}
		}
		return $ccs;

	}

	/**
	 * Returns the name of an agency cost center (by code).
	 * @param String $code
	 * @return String
	 */
	public function getName($code)
	{
		return $this->get($code)['name'];
	}

	/**
	 * Indicates if an item (by code) is active/inactive.
	 * @param String $code
	 * @return boolean
	 */
	public function isActive($code)
	{
		$details = $this->get($code);
		return !$details['disabled'];
	}

}