<?php

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

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

class MailOptionsValidator {
	use Logging;

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

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

	/** @var AdvancedValidatorFactory */
	private $advancedValidator;

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

	/**
	 * @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 mail instellingen is met ongeldige data ingevuld.'
				);
			}

			$output = $errors;
			return false;
		}

		if(!$this->advancedValidator->isEmail($data[ MailOptionsEnum::ADMIN_ADDRESS ])){
			$errors[ MailOptionsEnum::ADMIN_ADDRESS ] = $tr->translate('Uw email adres is niet geldig');
		}

		if(($data[MailOptionsEnum::MAIL_ADMIN] ?? false) && empty($data[ MailOptionsEnum::ADMIN_ADDRESS] ?? false)){
			$errors[MailOptionsEnum::MAIL_ADMIN] = [false];
			$errors[ MailOptionsEnum::ADMIN_ADDRESS] = $tr->translate(
				'Als u mails wilt ontvangen is een geldig adres verplicht'
			);
		}

		if(($data[MailOptionsEnum::MAIL_COMPANY] ?? false) && empty($data[ MailOptionsEnum::MAIL_COMPANY_CONTENTS] ?? false)){
			$errors[MailOptionsEnum::MAIL_COMPANY] = [false];
			$errors[ MailOptionsEnum::MAIL_COMPANY_CONTENTS] = $tr->translate(
				'Als u mails wilt verzenden naar bedrijven is een inhoud verplicht'
			);
		}

		if(($data[MailOptionsEnum::MAIL_STUDENT] ?? false) && empty($data[ MailOptionsEnum::MAIL_STUDENT_CONTENTS] ?? true)){
			$errors[MailOptionsEnum::MAIL_STUDENT] = [false];
			$errors[ MailOptionsEnum::MAIL_STUDENT_CONTENTS] = $tr->translate(
				'Als u mails wilt verzenden naar studenten is een inhoud 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 = [
			MailOptionsEnum::MAIL_COMPANY_CONTENTS,
			MailOptionsEnum::MAIL_STUDENT_CONTENTS,
			MailOptionsEnum::ADMIN_ADDRESS
		];

		$bool = [
			MailOptionsEnum::MAIL_ADMIN,
			MailOptionsEnum::MAIL_STUDENT,
			MailOptionsEnum::MAIL_COMPANY
		];

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