<?php

namespace Planaday\Config\OptionGroups\Payment;

use Planaday\Config\OptionGroups\OptionGroup;
use Planaday\Config\OptionTypes\BoolOption;
use Planaday\Config\OptionTypes\StringOption;
use Planaday\Platform\PlatformAbstraction\DataHandler;

class PaymentOptions extends OptionGroup {
	private const NAME = 'Payment Options';
	public const INDEX = '-payment-options';

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

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

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

	/** @var StringOption<string> */
	private $paymentNotice;

	/** @var StringOption<null> */
	private $mollieLiveKey;

	/** @var StringOption<null> */
	private $mollieTestKey;

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

	public function __construct( DataHandler $dataHandler ) {
		$data     = $dataHandler->getOption( self::INDEX );

		$this->testPaymentActive = new BoolOption(
			PaymentOptionsEnum::TEST_PAYMENT_ACTIVE,
			'Test modus actief?',
			"Betalingen naar test key sturen om deze als testbetalingen doen?<br>" .
			"<b>Let op: Deze moet enkel uit worden gezet als er tenminste één testbetaling is gedaan!",
			$data[ PaymentOptionsEnum::TEST_PAYMENT_ACTIVE ] ?? null,
			true
		);

		$this->paymentActive = new BoolOption(
			PaymentOptionsEnum::PAYMENT_ACTIVE,
			'Betalingen actief?',
			"Moet betalingen (via iDeal) actief zijn?",
			$data[ PaymentOptionsEnum::PAYMENT_ACTIVE ] ?? null,
			false
		);

		$this->cancelBookingIfNoPayment = new BoolOption(
			PaymentOptionsEnum::CANCEL_BOOKING_IF_NO_PAYMENT,
			'Boeking annuleren?',
			"Een boeking annuleren als de betaling van deze is mislukt? <br>" .
			"<b>Let op: </b> van een eventuele geannuleerde boeking wordt geen communicatie verstuurd!",
			$data[ PaymentOptionsEnum::CANCEL_BOOKING_IF_NO_PAYMENT ] ?? null,
			false
		);

		$this->paymentNotice = new StringOption(
			PaymentOptionsEnum::PAYMENT_NOTICE,
			'Toelichting op boekingsformulier',
			"Tekst bij boekingformulier, zodat de klant weet dat zij kunnen betalen<br>" .
			"Bijvoorbeeld 'Na bevestiging kunt u meteen betalen'.",
			$data[ PaymentOptionsEnum::PAYMENT_NOTICE ] ?? null,
			"Na bevestiging kunt u meteen betalen"
		);

		$this->mollieLiveKey = new StringOption(
			PaymentOptionsEnum::MOLLIE_API_KEY_LIVE,
			'Mollie Live API Key',
			'Vul hier de "live API key" van Mollie in om betalingen te kunnen doen',
			$data[ PaymentOptionsEnum::MOLLIE_API_KEY_LIVE ] ?? null,
			null
		);

		$this->mollieTestKey = new StringOption(
			PaymentOptionsEnum::MOLLIE_API_KEY_TEST,
			'Mollie Test API Key',
			'Vul hier de "test API key" van Mollie in om test betalingen te kunnen doen',
			$data[ PaymentOptionsEnum::MOLLIE_API_KEY_TEST ] ?? null,
			null
		);

		$this->allowBulkPayment = new BoolOption(
			PaymentOptionsEnum::ALLOW_BULK_PAYMENT,
			'Bulk betalingen toestaan?',
			"Kan een bedrijf meerdere deelnemers toevoegen en betalen met een enkele boeking?",
			$data[ PaymentOptionsEnum::ALLOW_BULK_PAYMENT ] ?? null,
			true
		);

		$layout = [
			$this->paymentActive,
			$this->testPaymentActive,
			$this->cancelBookingIfNoPayment,
			$this->allowBulkPayment,
			$this->paymentNotice,
			$this->mollieLiveKey,
			$this->mollieTestKey
		];

		parent::__construct( self::INDEX, self::NAME, '', $layout );
	}

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

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

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

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

	/**
	 * @return StringOption<null>
	 */
	public function mollieLiveKey(): StringOption {
		return $this->mollieLiveKey;
	}

	/**
	 * @return StringOption<null>
	 */
	public function mollieTestKey(): StringOption {
		return $this->mollieTestKey;
	}

	/**
	 * @return StringOption<string>
	 */
	public function paymentNotice(): StringOption {
		return $this->paymentNotice;
	}

	public function isPaymentPossible(): bool {
		return $this->paymentActive->is() && (
			$this->testPaymentActive->is() ?
				$this->mollieTestKey->isSet() :
				$this->mollieLiveKey->isSet()
			);
	}

	public function isPaymentLive(): bool {
		return $this->isPaymentPossible() && $this->testPaymentActive->is( false );
	}

	public function getActiveMollieKey(): ?string {
		return $this->testPaymentActive->is() ? $this->mollieTestKey->getValue() : $this->mollieLiveKey->getValue();
	}
}