<?php

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

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

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

			$output = $errors;
			return false;
		}

		if ( ($data[ StudentOptionsEnum::COST_CENTER_CODE_REQUIRED ] ?? false) && !($data[StudentOptionsEnum::ASK_COST_CENTER_CODE] ?? false)) {
			$errors[ StudentOptionsEnum::ASK_COST_CENTER_CODE ]  = [false];
			$errors[ StudentOptionsEnum::COST_CENTER_CODE_REQUIRED ] = $tr->translate(
				'Als u de kostenplaats verplicht wilt hebben moet deze ook worden gevraagd.'
			);
		}

		if ( ($data[ StudentOptionsEnum::DATE_OF_BIRTH_REQUIRED ] ?? false) && !($data[StudentOptionsEnum::ASK_DATE_OF_BIRTH] ?? false)) {
			$errors[ StudentOptionsEnum::ASK_DATE_OF_BIRTH ]  = [false];
			$errors[ StudentOptionsEnum::DATE_OF_BIRTH_REQUIRED ] = $tr->translate(
				'Als u de geboortedatum verplicht wilt hebben moet deze ook worden gevraagd.'
			);
		}

		if ( ($data[ StudentOptionsEnum::PLACE_OF_BIRTH_REQUIRED ] ?? false) && !($data[StudentOptionsEnum::ASK_PLACE_OF_BIRTH] ?? false)) {
			$errors[ StudentOptionsEnum::ASK_PLACE_OF_BIRTH ]  = [false];
			$errors[ StudentOptionsEnum::PLACE_OF_BIRTH_REQUIRED ] = $tr->translate(
				'Als u de geboorteplaats verplicht wilt hebben moet deze ook worden gevraagd.'
			);
		}

		if ( ($data[ StudentOptionsEnum::PHONE_NUMBER_REQUIRED ] ?? false) && !($data[StudentOptionsEnum::ASK_PHONE_NUMBER] ?? false)) {
			$errors[ StudentOptionsEnum::ASK_PHONE_NUMBER ]  = [false];
			$errors[ StudentOptionsEnum::PHONE_NUMBER_REQUIRED ] = $tr->translate(
				'Als u het telefoonnummer verplicht wilt hebben moet deze ook worden gevraagd.'
			);
		}

		if ( ($data[ StudentOptionsEnum::COMPANY_POSITION_REQUIRED ] ?? false) && !($data[StudentOptionsEnum::ASK_COMPANY_POSITION] ?? false)) {
			$errors[ StudentOptionsEnum::ASK_COMPANY_POSITION ]  = [false];
			$errors[ StudentOptionsEnum::COMPANY_POSITION_REQUIRED ] = $tr->translate(
				'Als u de functie verplicht wilt hebben moet deze ook worden gevraagd.'
			);
		}

		if(($data[StudentOptionsEnum::ASK_COST_CENTER_CODE] ?? false) && empty($data[StudentOptionsEnum::COST_CENTER_CODE_TEXT] ?? true)){
			$errors[StudentOptionsEnum::ASK_COST_CENTER_CODE] = [false];
			$errors[StudentOptionsEnum::COST_CENTER_CODE_TEXT] = $tr->translate(
				'Als u de kostenplaats wilt vragen is de text hiervoor verplicht'
			);
		}

		if(($data[StudentOptionsEnum::ASK_CODE_95] ?? false) && empty($data[StudentOptionsEnum::CODE_95_TEXT] ?? true)){
			$errors[StudentOptionsEnum::ASK_CODE_95] = [false];
			$errors[StudentOptionsEnum::CODE_95_TEXT] = $tr->translate(
				'Als u de code 95 wilt vragen is de text hiervoor verplicht'
			);
		}

		if(($data[StudentOptionsEnum::ASK_SOOB] ?? false) && empty($data[StudentOptionsEnum::SOOB_TEXT] ?? true)){
			$errors[StudentOptionsEnum::ASK_SOOB] = [false];
			$errors[StudentOptionsEnum::SOOB_TEXT] = $tr->translate(
				'Als u de soob wilt vragen is de text hiervoor verplicht'
			);
		}

		if(($data[StudentOptionsEnum::TERMS_AND_CONDITIONS_REQUIRED] ?? false) && empty($data[StudentOptionsEnum::TERMS_AND_CONDITIONS_URL] ?? true)){
			$errors[StudentOptionsEnum::TERMS_AND_CONDITIONS_REQUIRED] = [false];
			$errors[StudentOptionsEnum::TERMS_AND_CONDITIONS_URL] = $tr->translate(
				'Als u wilt dat de bezoeker de voorwaarden moet accepteren moet deze ingesteld zijn.'
			);
		}

		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 = [
			StudentOptionsEnum::TERMS_AND_CONDITIONS_URL,
			StudentOptionsEnum::COST_CENTER_CODE_TEXT,
			StudentOptionsEnum::CODE_95_TEXT,
			StudentOptionsEnum::SOOB_TEXT
		];

		$bool = [
			StudentOptionsEnum::ASK_ADDRESS,
			StudentOptionsEnum::ASK_IF_CONTACT,
			StudentOptionsEnum::ASK_NICK_NAME,
			StudentOptionsEnum::ASK_MAIDEN_NAME,
			StudentOptionsEnum::ASK_INTERNAL_REFERENCE,
			StudentOptionsEnum::ASK_COMPANY_POSITION,
			StudentOptionsEnum::ASK_COST_CENTER_CODE,
			StudentOptionsEnum::ASK_EMPLOYEE_NUMBER,
			StudentOptionsEnum::ASK_DATE_OF_BIRTH,
			StudentOptionsEnum::ASK_PLACE_OF_BIRTH,
			StudentOptionsEnum::ASK_PHONE_NUMBER,
			StudentOptionsEnum::ASK_CODE_95,
			StudentOptionsEnum::ASK_SOOB,
			StudentOptionsEnum::ASK_REMARK,
			StudentOptionsEnum::COST_CENTER_CODE_REQUIRED,
			StudentOptionsEnum::DATE_OF_BIRTH_REQUIRED,
			StudentOptionsEnum::PLACE_OF_BIRTH_REQUIRED,
			StudentOptionsEnum::PHONE_NUMBER_REQUIRED,
			StudentOptionsEnum::COMPANY_POSITION_REQUIRED,
			StudentOptionsEnum::TERMS_AND_CONDITIONS_REQUIRED
		];

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

}