<?php

namespace Planaday\Config\OptionGroups\Course\Overview;

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

class OverviewOptionsValidator {
	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 ) {
				$this->logError('Invalid field: ' . $field . ' with error: ' . $fieldException->getMessage());
				$errors[ $field ] = $tr->translate(
					'Het ' . strtolower($field) . ' veld bij de overview opties is met ongeldige data ingevuld.'
				);
			}

			$output = $errors;
			return false;
		}

		if(($data[OverviewOptionsEnum::SHOW_COURSE_BUTTON] ?? false) && empty($data[OverviewOptionsEnum::COURSE_BUTTON_TEXT] ?? true)){
			$errors[OverviewOptionsEnum::SHOW_COURSE_BUTTON] = [false];
			$errors[OverviewOptionsEnum::COURSE_BUTTON_TEXT] = $tr->translate(
				'Als u een knop wilt tonen is een tekst verplicht'
			);
		}

		if(empty($data[OverviewOptionsEnum::NO_COURSE_TEXT] ?? true)) {
			$errors[ OverviewOptionsEnum::NO_COURSE_TEXT ] = $tr->translate(
				'Een tekst voor wanneer er geen cursussen zijn is verplicht'
			);
		}

		if(($data[OverviewOptionsEnum::SHOW_DESCRIPTION] ?? false) && empty($data[OverviewOptionsEnum::MAX_LENGTH_DESCRIPTION] ?? true)){
			$errors[OverviewOptionsEnum::SHOW_DESCRIPTION] = [false];
			$errors[OverviewOptionsEnum::MAX_LENGTH_DESCRIPTION] = $tr->translate(
				'Als u een beschrijving wilt tonen is een maximale lengte verplicht'
			);
		}

		if (!($data[OverviewOptionsEnum::SHOW_DAY_PART_LIST] ?? false)){
			if($data[OverviewOptionsEnum::SHOW_LOCATION_DAY_PART] ?? false){
				$errors[OverviewOptionsEnum::SHOW_DAY_PART_LIST] = [false];
				$errors[OverviewOptionsEnum::SHOW_LOCATION_DAY_PART] = $tr->translate('Als u de locatie van een dagdeel wilt tonen is de lijst met dagdelen verplicht');
			}

			if($data[OverviewOptionsEnum::SHOW_LABELS_DAY_PART] ?? false){
				$errors[OverviewOptionsEnum::SHOW_DAY_PART_LIST] = [false];
				$errors[OverviewOptionsEnum::SHOW_LABELS_DAY_PART] = $tr->translate('Als u de labels van een dagdeel wilt tonen is de lijst met dagdelen verplicht');
			}

			if($data[OverviewOptionsEnum::INCLUDE_E_LEARNING_DAY_PARTS] ?? false){
				$errors[OverviewOptionsEnum::SHOW_DAY_PART_LIST] = [false];
				$errors[OverviewOptionsEnum::INCLUDE_E_LEARNING_DAY_PARTS] = $tr->translate('Als u de e-learning dagdelen wilt laten zien moet de lijst met dagdelen worden getoond');
			}
		}

		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 = [
			OverviewOptionsEnum::COURSE_BUTTON_TEXT,
			OverviewOptionsEnum::NO_COURSE_TEXT
		];

		$bool = [
			OverviewOptionsEnum::SHOW_CODE_95_ICON,
			OverviewOptionsEnum::SHOW_SOOB_ICON,
			OverviewOptionsEnum::SHOW_E_LEARNING_ICON,
			OverviewOptionsEnum::SHOW_MONEY_BACK_NOTICE,
			OverviewOptionsEnum::SHOW_START_GUARANTEE_NOTICE,
			OverviewOptionsEnum::SHOW_FINANCIAL_INFO,
			OverviewOptionsEnum::SHOW_PRICE,
			OverviewOptionsEnum::SHOW_DAY_PART_COUNT,
			OverviewOptionsEnum::SHOW_LABELS,
			OverviewOptionsEnum::SHOW_FIRST_LOCATION,
			OverviewOptionsEnum::SHOW_AVAILABILITY,

			OverviewOptionsEnum::SHOW_DESCRIPTION,

			OverviewOptionsEnum::SHOW_DAY_PART_LIST,
			OverviewOptionsEnum::INCLUDE_E_LEARNING_DAY_PARTS,
			OverviewOptionsEnum::SHOW_LOCATION_DAY_PART,
			OverviewOptionsEnum::SHOW_LABELS_DAY_PART,

			OverviewOptionsEnum::SHOW_COURSE_BUTTON,
			OverviewOptionsEnum::INCLUDE_FULL_COURSES,
			OverviewOptionsEnum::INCLUDE_PARTIALLY_PASSED_COURSES,
			OverviewOptionsEnum::E_LEARNING_COUNTS_AS_START,
		];

		$int = [
			OverviewOptionsEnum::MAX_LENGTH_DESCRIPTION
		];

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

}