<?php

namespace Planaday\Config\OptionGroups\Booking\Form\Materials;

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

class MaterialsOptionsValidator {
	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 materiaal opties is met ongeldige data ingevuld.'
				);
			}

			$output = $errors;
			return false;
		}

		if ( ($data[ MaterialsOptionsEnum::SHOW_DESCRIPTION_MATERIALS ] ?? false) && !($data[MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM] ?? false)) {
			$errors[ MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM ]  = [false];
			$errors[ MaterialsOptionsEnum::SHOW_DESCRIPTION_MATERIALS ] = $tr->translate(
				'Om de beschrijving van de materialen te tonen moeten de materialen ook worden gevraagd bij de boeking.'
			);
		}

		if ( ($data[ MaterialsOptionsEnum::SHOW_BOTH_WITH_AND_WITHOUT_TAX_PRICE ] ?? false) && !($data[MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM] ?? false)) {
			$errors[ MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM ]  = [false];
			$errors[ MaterialsOptionsEnum::SHOW_BOTH_WITH_AND_WITHOUT_TAX_PRICE ] = $tr->translate(
				'Om de prijzen van de materialen te tonen moeten de materialen ook worden gevraagd bij de boeking.'
			);
		}

		if(($data[MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM] ?? false) && empty($data[MaterialsOptionsEnum::MATERIALS_FORM_TITLE] ?? true)){
			$errors[MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM] = [false];
			$errors[MaterialsOptionsEnum::MATERIALS_FORM_TITLE] = $tr->translate(
				'De titel van het materiaal formulier is verplicht als de materialen worden gevraagd bij de boeking.'
			);
		}

		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 = [
			MaterialsOptionsEnum::MATERIALS_FORM_DESCRIPTION,
			MaterialsOptionsEnum::MATERIALS_FORM_TITLE
		];

		$bool = [
			MaterialsOptionsEnum::SHOW_DESCRIPTION_MATERIALS,
			MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM,
			MaterialsOptionsEnum::SHOW_BOTH_WITH_AND_WITHOUT_TAX_PRICE
		];

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