<?php
namespace Modules\Admin\Config\Services\Basic;

class Model extends \App\Mvc\Model_ListingAbstract
{

	/**
	 * Get list with service and group
	 * @return Array
	 */
	private function getServicesList()
	{
		$servicesCfg = \App\Config\Services_Basic::getAdmin();
		$generalServicesList = [];
		$groupPosition = 1;

		// scan each group
		foreach ($servicesCfg->getAll() as $categoryCode => $details) {
			$moveUpGroup = true;

			// check if group has the first position in list
			if ($groupPosition == 1) {
				$moveUpGroup = false;
			}

			// check if this group has the last position in list
			if ($groupPosition == count($servicesCfg->getAll())) {
				$moveDownGroup = false;
			}
			else {
				$moveDownGroup = true;
			}

			// set service position
			$servicePosition = 1;

			// get information from group
			if ($details['type'] == 'group') {
				$generalServicesList[] = [
					'id' =>				$categoryCode,
					'groupCode' => 		$categoryCode,
					'groupName' => 		$details['name'],
					'serviceType' => 	'',
					'serviceName' => 	'',
					'groupPosition' =>	$groupPosition,
					'canMoveUp' =>		$moveUpGroup,
					'canMoveDown' =>	$moveDownGroup,
					'type' =>			'group',
				];

				// add a service in the general list
				if ($details['list']) {
					foreach ($details['list'] as $serviceType => $serviceDetails) {
						$moveUpService = true;
						// check if service has the first position in list
						if ($servicePosition == 1) {
							$moveUpService = false;
						}

						// check if this group has last position
						if ($servicePosition == count($details['list'])) {
							$moveDownService = false;
						}
						else {
							$moveDownService = true;
						}

						$generalServicesList[] = [
							'id' =>				$serviceType,
							'groupCode' => 		$categoryCode,
							'groupName' => 		$details['name'],
							'serviceType' => 	$serviceType,
							'serviceName' => 	$serviceDetails['name'],
							'groupPosition' =>	$groupPosition,
							'servicePosition' =>$servicePosition,
							'canMoveUp'		=> 	$moveUpService,
							'canMoveDown'	=> 	$moveDownService,
							'type' =>			'service',
						];

						$servicePosition++;
					}
				}
			}
			// service without a group associated
			else{
				$generalServicesList[] = [
					'id' =>				$categoryCode,
					'groupCode' 	=> 	'',
					'groupName'		=> 	'',
					'serviceType' 	=> 	$categoryCode,
					'serviceName' 	=> 	$details['name'],
					'groupPosition' =>	$groupPosition,
					'servicePosition' =>$servicePosition,
					'canMoveUp'		=> 	$moveUpService,
					'canMoveDown'	=> 	$moveDownService,
					'type' =>			'service',
				];
			}

			$groupPosition++;
		}

		return $generalServicesList;
	}

	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{

		$filters = $listingParams->getFilters();

		$servicesList = [];

		$generalServicesList = $this->getServicesList();

		if ($filters['service']) {
			foreach ($generalServicesList as $service) {
				if ($service['type'] == 'service' and stripos($service['serviceName'], $filters['service'])!==false) {
					$servicesList[] = $service;
				}
			}
		}
		else {
			$servicesList = $generalServicesList;
		}

		$this->totalRecordsCount = count($servicesList);
		$this->recordsList = $servicesList;

	}


	/**
	 * Check if a element can be moved up
	 * @param String $code
	 */
	public function canMoveUp($code, \App\Config\Services_Basic $serviceCfg)
	{
		// get list with all services
		$generalServicesList = $this->getServicesList();

		foreach ($generalServicesList as $details) {
			if ( $details['groupCode'] == $code ||  $details['serviceType'] == $code) {
				return $details['canMoveUp'];
			}
		}
		return false;
	}

	public function canMoveDown($code, \App\Config\Services_Basic $serviceCfg)
	{
		// get list with all services
		$generalServicesList = $this->getServicesList();

		foreach ($generalServicesList as $details) {
			if ( $details['groupCode'] == $code ||  $details['serviceType'] == $code) {
				return $details['canMoveDown'];
			}
		}
		return false;
	}

	public function isGroup($code, \App\Config\Services_Basic $serviceCfg)
	{
		$generalServicesList = $this->getServicesList();

		foreach ($generalServicesList as $details) {
			if ( $details['groupCode'] == $code){
				return false;
			}
			if ( $details['serviceType'] == $code) {
				return true;
			}
		}
		return false;
	}
}