<?php

namespace Uhura\Grossmarkt\Utility;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Class DatabaseUtility
 */
class DatabaseUtility
{
	public static function getQueryBuilder($table)
	{
		return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
	}

	/**
	 * @param bool $forTCA
	 * @return array
	 * @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
	 */
	public static function generateMarketPlanPositions(bool $forTCA = false, bool $routePlan = false) {
		$planSettings = ConfigurationUtility::getTyposcriptConfiguration('plugin.tx_grossmarkt.settings.variables');
		$items = [];
		$unusedPositions = [1,2,4,10,12,14,15,16,19,20,22,23,26,31];

		if(!empty($planSettings) && isset($planSettings['planPositionsCount']) && $planSettings['planPositionsCount']){
			(int)$planPositionsCount = $planSettings['planPositionsCount'];
			if($planPositionsCount > 0){
				for($i = 1; $i <= $planPositionsCount; $i++){
					if(!in_array($i, $unusedPositions)) {
						if ($i == 17 || $i == 30) {
							$items[$i . '-top'] = [
								'value' => $i . '-top',
								'label' => $i . '-top'
							];
							$items[$i . '-left'] = [
								'value' => $i . '-left',
								'label' => $i . '-left',
							];
							$items[$i . '-right'] = [
								'value' => $i . '-right',
								'label' => $i . '-right',
							];
							$items[$i . '-bottom'] = [
								'value' => $i . '-bottom',
								'label' => $i . '-bottom',
							];
							if ($i == 30) {
								$items[$i . '-bottom-left'] = [
									'value' => $i . '-bottom-left',
									'label' => $i . '-bottom-left',
								];
							}
						} elseif ($i == 29) {
							$items[$i . '-top'] = [
								'value' => $i . '-top',
								'label' => $i . '-top',
							];
							$items[$i . '-bottom'] = [
								'value' => $i . '-bottom',
								'label' => $i . '-bottom',
							];
						} elseif ($i == 6){
							$items[$i . '-exit-north'] = [
								'value' => $i . '-exit-north',
								'label' => $i . '-exit-north',
							];
						}else {
							$items[$i] = [
								'value' => (string)$i,
								'label' => (string)$i,
							];
						}
					}
				}
			}
		}
		$items['enter-exit-southwest'] = [
			'value' => 'enter-exit-southwest',
			'label' => 'enter-exit-southwest',
		];
//		debug($items);
//		die();
		return $items;
	}
}