<?php

namespace Planaday\Config\OptionGroups\Booking\Submission\Redirecting;

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

class RedirectOptionsValidator {
	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 {
		$errors = [];
		$tr = $this->translator;

		try {
			$data = $this->getSafeData( $data );
		} catch ( InvalidFieldsException $e ) {
			foreach ( $e->getInvalidFields() as $field => $fieldException ) {
				$errors[ $field ] = $tr->translate(
					'Het ' . strtolower($field) . ' veld bij de redirect instellingen is met ongeldige data ingevuld.'
				);
			}

			$output = $errors;
			return false;
		}

		if ($data[RedirectOptionsEnum::REDIRECT_AFTER_SUCCESS] ?? false){
			if(empty($data[RedirectOptionsEnum::REDIRECT_SUCCESS_PAGE] ?? false)){
				$errors[RedirectOptionsEnum::REDIRECT_AFTER_SUCCESS] = [false];
				$errors[RedirectOptionsEnum::REDIRECT_SUCCESS_PAGE] = $tr->translate(
					'Als u wilt dat de gebruiker naar een pagina wordt doorgestuurd na succesvolle inschrijving is een pagina verplicht'
				);
			}

		} else {
			if(empty($data[RedirectOptionsEnum::NO_REDIRECT_SUCCESS_TEXT] ?? true)){
				$errors[RedirectOptionsEnum::REDIRECT_AFTER_SUCCESS] = [false];
				$errors[RedirectOptionsEnum::NO_REDIRECT_SUCCESS_TEXT] = $tr->translate(
					'Als u wilt dat de gebruiker op dezelfde pagina blijft na succesvolle inschrijving is een text verplicht'
				);
			}
		}

		if ($data[RedirectOptionsEnum::REDIRECT_AFTER_FAILURE] ?? false){
			if(empty($data[RedirectOptionsEnum::REDIRECT_FAILURE_PAGE] ?? false)){
				$errors[RedirectOptionsEnum::REDIRECT_AFTER_FAILURE] = [false];
				$errors[RedirectOptionsEnum::REDIRECT_FAILURE_PAGE] = $tr->translate(
					'Als u wilt dat de gebruiker naar een pagina wordt doorgestuurd na een mislukte inschrijving is een pagina verplicht'
				);
			}

		} else {
			if(empty($data[RedirectOptionsEnum::NO_REDIRECT_FAILURE_TEXT] ?? true)){
				$errors[RedirectOptionsEnum::REDIRECT_AFTER_FAILURE] = [false];
				$errors[RedirectOptionsEnum::NO_REDIRECT_FAILURE_TEXT] = $tr->translate(
					'Als u wilt dat de gebruiker op dezelfde pagina blijft na een mislukte inschrijving is een text 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;

		$bool = [
			RedirectOptionsEnum::REDIRECT_AFTER_SUCCESS,
			RedirectOptionsEnum::REDIRECT_AFTER_FAILURE
		];

		$int = [
			RedirectOptionsEnum::REDIRECT_SUCCESS_PAGE,
			RedirectOptionsEnum::REDIRECT_FAILURE_PAGE
		];

		$string = [
			RedirectOptionsEnum::NO_REDIRECT_SUCCESS_TEXT,
			RedirectOptionsEnum::NO_REDIRECT_FAILURE_TEXT
		];

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

}