<?php

namespace Planaday\Config\OptionGroups\General\Api;

use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DataFilters\InvalidFieldsException;
use Planaday\Utility\DataFilters\TypeValidator;
use Planaday\Utility\Debug\Log\Logging;

class ApiOptionsValidator {
	use Logging;

	/** @var TypeValidator */
	protected $typeValidator;

	/** @var LocalizationController */
	protected $translator;

	public function __construct( TypeValidator $typeValidator, LocalizationController $translator) {
		$this->typeValidator            = $typeValidator;
		$this->translator               = $translator;
	}

	/**
	 * @param array $data
	 * @param array|null $output if valid, clean data is returned here. if not, errors will be returned here
	 *
	 * @return bool true if valid, false if not
	 */
	public function validate( array $data, ?array &$output): bool {
		$this->logInfo('Validating api options');
		$errors = [];
		$tr = $this->translator;

		try {
			$data = $this->getSafeData( $data );
		} catch ( InvalidFieldsException $e ) {
			foreach ( $e->getInvalidFields() as $field => $fieldException ) {
				$this->logError('Invalid field: ' . $field . ' with error: ' . $fieldException->getMessage());
				$errors[ $field ] = $tr->translate(
					'Het ' . strtolower($field) . ' veld bij de api opties is met ongeldige data ingevuld.'
				);
			}

			$output = $errors;
			return false;
		}

		if (empty($data[ApiOptionsEnum::API_KEY] ?? null)){
			$errors[ApiOptionsEnum::API_KEY] = $tr->translate(
				'Api key is verplicht'
			);
		}

		if (empty($data[ApiOptionsEnum::DOMAIN] ?? null)){
			$errors[ApiOptionsEnum::DOMAIN] = $tr->translate(
				'Api domein is verplicht'
			);
		}

		if (isset($data[ApiOptionsEnum::REFRESH_INTERVAL_COURSES])){
			if (empty($data[ApiOptionsEnum::REFRESH_INTERVAL_COURSES])){
				$errors[ApiOptionsEnum::REFRESH_INTERVAL_COURSES] = $tr->translate(
					'Het verversingsinterval voor de cursussen is verplicht.'
				);
			} elseif ($data[ ApiOptionsEnum::REFRESH_INTERVAL_COURSES ] < 2){
				$errors[ApiOptionsEnum::REFRESH_INTERVAL_COURSES] = $tr->translate(
					'Het verversingsinterval voor de cursussen moet meer dan één uur zijn.'
				);
			}
		}

		if (isset($data[ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS])) {
			if ( empty( $data[ ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS ] ) ) {
				$errors[ ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS ] = $tr->translate(
					'Het verversingsinterval voor de dagdelen is verplicht.'
				);
			} elseif ($data[ ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS ] < 2){
				$errors[ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS] = $tr->translate(
					'Het verversingsinterval voor de dagdelen moet meer dan één uur zijn.'
				);
			}
		}

		if (isset($data[ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS])) {
			if ( empty( $data[ ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS ] ) ) {
				$errors[ ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS ] = $tr->translate(
					'Het verversingsinterval voor de locaties is verplicht.'
				);
			} elseif ($data[ ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS ] < 2){
				$errors[ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS] = $tr->translate(
					'Het verversingsinterval voor de locaties moet meer dan één uur zijn.'
				);
			}
		}

		if (isset($data[ApiOptionsEnum::REFRESH_SIZE_COURSES])) {
			if ( empty( $data[ ApiOptionsEnum::REFRESH_SIZE_COURSES ] ) ) {
				$errors[ ApiOptionsEnum::REFRESH_SIZE_COURSES ] = $tr->translate(
					'Het aantal maanden wat opgehaald moet worden is verplicht.'
				);
			}
		}

		if ( count( $errors ) > 0 ) {
			$output = $errors;
			$this->logWarning("data is not valid", $errors);
			return false;
		}

		$output = $data;
		return true;
	}

	/**
	 * @param array $data
	 *
	 * @return array
	 * @throws InvalidFieldsException
	 */
	private function getSafeData(array $data): array {
		$tv = $this->typeValidator;

		$string = [
			ApiOptionsEnum::API_KEY,
			ApiOptionsEnum::DOMAIN
		];

		$ints = [
			ApiOptionsEnum::REFRESH_INTERVAL_COURSES,
			ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS,
			ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS,
			ApiOptionsEnum::REFRESH_SIZE_COURSES,
		];

		return $tv->getSafeData($data, $string, [], [], $ints);
	}

}