<?php


namespace WPDesk\FakturyKSEF\Repository;

use WPDesk\FakturyKSEF\Api\Environments\AbstractEnvironment;
use WPDesk\FakturyKSEF\Api\Environments\DemoEnvironment;
use WPDesk\FakturyKSEF\Api\Environments\ProdEnvironment;
use WPDesk\FakturyKSEF\Api\Environments\TestEnvironment;
use WPDeskKSEFVendor\WPDesk\Persistence\Adapter\WordPress\WordpressOptionsContainer;

class PluginSettings {

	public const KSEF_ENVIRONMENT          = 'ksef_environment';
	public const KSEF_TOKEN                = 'ksef_token';
	public const KSEF_EXEMPT_VAT_RATE      = 'ksef_zw_rate';
	public const KSEF_NP_VAT_RATE          = 'ksef_np_rate';
	public const AUTO_DOCUMENT_SENDING     = 'auto_send_to_ksef';
	public const AUTO_CROSS_BORDER_SENDING = 'auto_cross_border_send_to_ksef';
	public const EXEMPTION                 = 'ksef_defaults_exemption';
	public const DEFAULT_NP_RATE_TYPE      = 'ksef_defaults_np_vat_rate_type';

	private WordpressOptionsContainer $container;

	public function __construct( WordpressOptionsContainer $container ) {
		$this->container = $container;
	}

	public function get( string $key ) {
		return $this->container->get( $key );
	}

	public function get_exemption_reason(): string {
		return $this->container->get_fallback( self::EXEMPTION, '' );
	}

	public function is_auto_sending_enabled(): bool {
		return filter_var( $this->container->get_fallback( self::AUTO_DOCUMENT_SENDING, false ), FILTER_VALIDATE_BOOLEAN );
	}

	public function is_cross_border_sending_enabled(): bool {
		return filter_var( $this->container->get_fallback( self::AUTO_CROSS_BORDER_SENDING, false ), FILTER_VALIDATE_BOOLEAN );
	}

	public function get_exempt_rate(): string {
		return $this->container->get_fallback( self::KSEF_EXEMPT_VAT_RATE, '' );
	}

	public function get_np_rate(): string {
		return $this->container->get_fallback( self::KSEF_NP_VAT_RATE, '' );
	}

	public function get_ksef_api_token(): string {
		return $this->container->get_fallback( self::KSEF_TOKEN, '' );
	}

	public function get_company_vat_number(): string {
		return $this->container->get_fallback( 'company_nip', '' );
	}

	public function get_environment(): AbstractEnvironment {
		$selected_environment = $this->container->get_fallback( self::KSEF_ENVIRONMENT, TestEnvironment::SLUG );

		switch ( $selected_environment ) {
			case ProdEnvironment::SLUG:
				$environment = new ProdEnvironment();
				break;
			case DemoEnvironment::SLUG:
				$environment = new DemoEnvironment();
				break;
			case TestEnvironment::SLUG:
			default:
				$environment = new TestEnvironment();
				break;
		}

		return $environment;
	}
}
