<?php

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

use Planaday\Config\OptionGroups\Booking\BookingOptions;
use Planaday\Config\OptionGroups\Booking\Form\Company\Invoice\InvoiceOptions;
use Planaday\Config\OptionGroups\OptionCluster;
use Planaday\Config\OptionGroups\OptionGroup;
use Planaday\Config\OptionTypes\BoolOption;
use Planaday\Utility\DependencyInjection\ComponentRegistry;
use Planaday\Utility\DependencyInjection\ComponentRegistryShortCut;

class CompanyOptions extends OptionGroup implements ComponentRegistryShortCut {
	private const NAME = 'Bedrijf';

	private const DESCRIPTION = 'De instellingen voor de registratie van bedrijven in het boeking formulier.';

	/** @var InvoiceOptions */
	private $invoice;

	/** @var BoolOption<bool> */
	private $allowCompanies;

	/** @var BoolOption<bool> */
	private $asCompanyDefault;

	/** @var BoolOption<bool> */
	private $askPhoneNumber;

	/** @var BoolOption<bool> */
	private $phoneNumberRequired;

	public function __construct( string $index, array $data ) {
		$this->invoice = new InvoiceOptions( CompanyOptionsEnum::INVOICE, $data[ CompanyOptionsEnum::INVOICE ] ?? []);

		$this->allowCompanies = new BoolOption(
			CompanyOptionsEnum::ALLOW_COMPANIES,
			'Bedrijven toestaan',
			"Mogen bedrijven met cursisten boeken?",
			$data[ CompanyOptionsEnum::ALLOW_COMPANIES ] ?? null,
			true
		);

		$this->asCompanyDefault = new BoolOption(
			CompanyOptionsEnum::AS_COMPANY_DEFAULT,
			'Standaard als bedrijf boeken?',
			"Moet een cursist standaard als bedrijf boeken? <br>" .
			"Als dit niet geselecteerd is boekt de cursist standaard als particulier.",
			$data[ CompanyOptionsEnum::AS_COMPANY_DEFAULT ] ?? null,
			false
		);

		$this->askPhoneNumber = new BoolOption(
			CompanyOptionsEnum::ASK_PHONE_NUMBER,
			'Telefoonnummer vragen?',
			"Moet er een telefoonnummer gevraagd worden?",
			$data[ CompanyOptionsEnum::ASK_PHONE_NUMBER ] ?? null,
			false
		);

		$this->phoneNumberRequired = new BoolOption(
			CompanyOptionsEnum::PHONE_NUMBER_REQUIRED,
			'Telefoonnummer verplicht?',
			"Moet het telefoonnummer verplicht zijn?",
			$data[ CompanyOptionsEnum::PHONE_NUMBER_REQUIRED ] ?? null,
			false
		);

		$layout = [
			$this->invoice,
			$this->allowCompanies,
			$this->asCompanyDefault,
			new OptionCluster(
				'Telefoonnummer',
				'Een telefoonnummer kan gevraagd worden aan bedrijven.',
				[
					"vragen" => $this->askPhoneNumber,
					"verplicht" => $this->phoneNumberRequired
				]
			)
		];
		parent::__construct( $index, self::NAME, self::DESCRIPTION, $layout );
	}

	public function invoice(): InvoiceOptions {
		return $this->invoice;
	}

	/**
	 * @return BoolOption<bool>
	 */
	public function allowCompanies(): BoolOption {
		return $this->allowCompanies;
	}

	/**
	 * @return BoolOption<bool>
	 */
	public function asCompanyDefault(): BoolOption {
		return $this->asCompanyDefault;
	}

	/**
	 * @return BoolOption<bool>
	 */
	public function askPhoneNumber(): BoolOption {
		return $this->askPhoneNumber;
	}

	/**
	 * @return BoolOption<bool>
	 */
	public function phoneNumberRequired(): BoolOption {
		return $this->phoneNumberRequired;
	}

	public static function componentRegistryConstruct( ComponentRegistry $registry ) {
		return $registry->getComponent(BookingOptions::class)->formOptions()->company();
	}
}