<?php

namespace Planaday\Config\General\Price;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Config\SettingsWithUsageLog;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;

#[Scoped]
class PriceSettings implements EnumMappedSettings, SettingsWithForm, SettingsWithUsageLog {
	use FormGroupable;
	use BasicEnumMappedSetting;
	use BasicSettingsLog;

	protected bool $includingTax = true;
	protected bool $withTaxLabel = true;
	protected bool $usePointsForDecimals = false;

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase
	) {
		$this->settingsDatabase->reloadData($this, PriceOptionsEnum::class );
	}

	protected function createFormGroup(): BasicGroup {
		$t = function ( string $string ) {
			return $this->translator->translate( $string );
		};

		return ( new BasicGroup() )
			->setIndex( PriceOptionsEnum::class )
			->addContents(
				( new BoolInput() )
					->setIndex( PriceOptionsEnum::INCLUDING_TAX->value )
					->setName( $t( 'Inclusief BTW' ) )
					->setDescription( $t( 'Moet de prijs inclusief BTW worden getoond?' ) )
					->setDefaultValue( $this->includingTax )
					->addSetCallback( function ( $value ) {
						$this->includingTax = $value ?? $this->includingTax;
					}),
				( new BoolInput() )
					->setIndex( PriceOptionsEnum::WITH_TAX_LABEL->value )
					->setName( $t( 'BTW label' ) )
					->setDescription( $t( 'Moet er een label worden getoond voor de BTW?' ) )
					->setDefaultValue( $this->withTaxLabel )
					->addSetCallback( function ( $value ) {
						$this->withTaxLabel = $value ?? $this->withTaxLabel;
					}),
				( new BoolInput() )
					->setIndex( PriceOptionsEnum::USE_POINT_FOR_DECIMALS->value )
					->setName( $t( 'Punt inplaats van komma' ) )
					->setDescription( $t( 'Moet er een punt gebruikt worden bij prijzen inplaats van een komma?' ) )
					->setDefaultValue( $this->usePointsForDecimals )
					->addSetCallback( function ( $value ) {
						$this->usePointsForDecimals = $value ?? $this->usePointsForDecimals;
					})
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				PriceOptionsEnum::INCLUDING_TAX->value => &$this->includingTax,
				PriceOptionsEnum::WITH_TAX_LABEL->value => &$this->withTaxLabel,
				PriceOptionsEnum::USE_POINT_FOR_DECIMALS->value => &$this->usePointsForDecimals,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isIncludingTax(): bool {
		$this->logUse( PriceOptionsEnum::INCLUDING_TAX );
		return $this->includingTax;
	}

	public function isWithTaxLabel(): bool {
		$this->logUse( PriceOptionsEnum::WITH_TAX_LABEL );
		return $this->withTaxLabel;
	}

	public function isUsePointsForDecimals(): bool {
		$this->logUse( PriceOptionsEnum::USE_POINT_FOR_DECIMALS );
		return $this->usePointsForDecimals;
	}
}