<?php
/**
 * Class WooCommerceDHLPolskaSettings
 *
 * @package DhlPolska\WooCommerceSettings
 */

namespace DhlPolska\WooCommerceSettings;

use DhlPolska\SanitizeTrait;

/**
 * DHL Polska Settings.
 */
class WooCommerceDHLPolskaSettings {
	use SanitizeTrait;

	private const DEFAULT_WEIGHT       = 0.01;
	private const META_TEMPLATE_WEIGHT = '_dhl_polska_template_weight';

	/**
	 * @return string
	 */
	public function get_webapi_login(): string {
		return $this->get_api_setting( WooCommerceDHLPolskaSettingsFields::OPTION_TEST_WEBAPI_LOGIN, WooCommerceDHLPolskaSettingsFields::OPTION_WEBAPI_LOGIN );
	}

	/**
	 * @return string
	 */
	public function get_webapi_password(): string {
		return $this->get_api_setting( WooCommerceDHLPolskaSettingsFields::OPTION_TEST_WEBAPI_PASSWORD, WooCommerceDHLPolskaSettingsFields::OPTION_WEBAPI_PASSWORD );
	}

	/**
	 * @return string
	 */
	public function get_webapi_sap_number(): string {
		return $this->get_api_setting( WooCommerceDHLPolskaSettingsFields::OPTION_TEST_WEBAPI_SAP_NUMBER, WooCommerceDHLPolskaSettingsFields::OPTION_WEBAPI_SAP_NUMBER );
	}

	/**
	 * @return float
	 */
	public function get_default_weight(): float {
		$template_id = $this->get_default_template_id();
		if ( '' === $template_id ) {
			return self::DEFAULT_WEIGHT;
		}

		$weight = get_post_meta( (int) $template_id, self::META_TEMPLATE_WEIGHT, true );
		if ( '' === $weight || null === $weight ) {
			return self::DEFAULT_WEIGHT;
		}

		$weight = $this->get_float_value( (string) $weight );
		if ( $weight < self::DEFAULT_WEIGHT ) {
			return self::DEFAULT_WEIGHT;
		}

		return $weight;
	}

	/**
	 * @return string
	 */
	public function get_default_template_id(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_DEFAULT_TEMPLATE_ID, '' );
	}

	/**
	 * @return bool
	 */
	public function is_allowed_email_tracking_number(): bool {
		return $this->get_bool_value( get_option( WooCommerceDHLPolskaSettingsFields::OPTION_EMAIL_TRACKING_NUMBER, 'no' ) );
	}

	/**
	 * @return string
	 */
	public function get_label_mode(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_LABEL_MODE, 'pdf' );
	}

	/**
	 * @return bool
	 */
	public function is_test_mode(): bool {
		return $this->get_bool_value( get_option( WooCommerceDHLPolskaSettingsFields::OPTION_TEST_MODE, false ) );
	}

	/**
	 * @return string
	 */
	public function get_sender_name(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_NAME, '' );
	}

	/**
	 * @return string
	 */
	public function get_sender_street(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_STREET, '' );
	}

	/**
	 * @return string
	 */
	public function get_sender_house_number(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_HOUSE_NUMBER, '' );
	}

	/**
	 * @return string
	 */
	public function get_sender_postcode(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_POSTCODE, '' );
	}

	/**
	 * @return string
	 */
	public function get_sender_city(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_CITY, '' );
	}

	/**
	 * @return string
	 */
	public function get_sender_phone(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_PHONE, '' );
	}

	/**
	 * @return string
	 */
	public function get_sender_email(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SENDER_EMAIL, '' );
	}

	/**
	 * @return string[]
	 */
	public function get_auto_shipment_statuses(): array {
		return (array) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_AUTO_SHIPMENT_STATUS, [] );
	}

	/**
	 * @return string[]
	 */
	public function get_auto_courier_statuses(): array {
		return (array) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_AUTO_COURIER_STATUS, [] );
	}

	/**
	 * @return string
	 */
	public function get_courier_pickup_start(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_COURIER_PICKUP_START, '10:00' );
	}

	/**
	 * @return string
	 */
	public function get_courier_pickup_end(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_COURIER_PICKUP_END, '16:00' );
	}

	public function get_shipment_date_cutoff_time(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_SHIPMENT_DATE_CUTOFF, '' );
	}

	public function get_courier_booking_date_cutoff_time(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_COURIER_DATE_CUTOFF, '' );
	}

	/**
	 * @return string
	 */
	public function get_return_shipment_type(): string {
		return (string) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_RETURN_SHIPMENT_TYPE, 'courier' );
	}

	/**
	 * @return int
	 */
	public function get_return_label_expiration(): int {
		return (int) get_option( WooCommerceDHLPolskaSettingsFields::OPTION_RETURN_LABEL_EXPIRATION, 14 );
	}

	/**
	 * @return bool
	 */
	public function is_logging_enabled(): bool {
		return $this->get_bool_value( get_option( WooCommerceDHLPolskaSettingsFields::OPTION_LOGGING_ENABLED, 'no' ) );
	}

	/**
	 * @param string $test_field       .
	 * @param string $production_field .
	 *
	 * @return string
	 */
	private function get_api_setting( string $test_field, string $production_field ): string {
		if ( $this->is_test_mode() ) {
			return (string) get_option( $test_field );
		}

		return (string) get_option( $production_field );
	}
}
