<?php
/**
 * Class Packages
 *
 * @package DhlPolska\Shipment\Metabox\Field
 */

namespace DhlPolska\Shipment\Metabox\Field;

use DhlPolska\Shipment\ShipmentTypes;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * New field type for DHL packages.
 */
class Packages implements Hookable {
	public const FIELD_TYPE = 'dhl_polska_packages';

	/**
	 * @var float
	 */
	private float $default_weight;

	/**
	 * @var string
	 */
	private string $weight_unit;

	/**
	 * @var string
	 */
	private string $dimension_unit;

	/**
	 * @var ShipmentTypes
	 */
	private ShipmentTypes $shipment_types;

	/**
	 * Packages constructor.
	 *
	 * @param float         $default_weight .
	 * @param string        $weight_unit    .
	 * @param string        $dimension_unit .
	 * @param ShipmentTypes $shipment_types .
	 */
	public function __construct( float $default_weight, string $weight_unit, string $dimension_unit, ShipmentTypes $shipment_types ) {
		$this->default_weight = $default_weight;
		$this->weight_unit    = $weight_unit;
		$this->dimension_unit = $dimension_unit;
		$this->shipment_types = $shipment_types;
	}

	/**
	 * Add hooks.
	 */
	public function hooks() {
		add_filter( 'woocommerce_form_field_' . self::FIELD_TYPE, [ $this, 'add_field' ], 10, 4 );
	}

	/**
	 * @param string $field .
	 * @param string $key   .
	 * @param array  $args  .
	 * @param string $value .
	 *
	 * @return string
	 */
	public function add_field( $field, $key, $args, $value ): string {
		$default_weight           = $this->default_weight;
		$weight_unit              = $this->weight_unit;
		$dimension_unit           = $this->dimension_unit;
		$shipment_types           = $this->shipment_types;
		$package_row_allowed_html = $this->get_package_row_allowed_html();

		ob_start();
		include __DIR__ . '/views/html-field-packages.php';

		return (string) ob_get_clean();
	}

	/**
	 * @return array<string, array<string, bool>>
	 */
	private function get_package_row_allowed_html(): array {
		$allowed_html = wp_kses_allowed_html( 'post' );

		$allowed_html['input']    = [
			'type'          => true,
			'name'          => true,
			'value'         => true,
			'id'            => true,
			'class'         => true,
			'style'         => true,
			'placeholder'   => true,
			'checked'       => true,
			'disabled'      => true,
			'readonly'      => true,
			'required'      => true,
			'maxlength'     => true,
			'min'           => true,
			'max'           => true,
			'step'          => true,
			'size'          => true,
			'autocomplete'  => true,
			'data-*'        => true,
			'aria-*'        => true,
		];
		$allowed_html['select']   = [
			'name'      => true,
			'id'        => true,
			'class'     => true,
			'style'     => true,
			'multiple'  => true,
			'disabled'  => true,
			'required'  => true,
			'data-*'    => true,
			'aria-*'    => true,
		];
		$allowed_html['option']   = [
			'value'     => true,
			'label'     => true,
			'class'     => true,
			'selected'  => true,
			'disabled'  => true,
		];
		$allowed_html['optgroup'] = [
			'label'     => true,
			'class'     => true,
			'disabled'  => true,
		];
		$allowed_html['label']    = array_merge(
			$allowed_html['label'] ?? [],
			[
				'for'       => true,
				'class'     => true,
				'data-*'    => true,
				'aria-*'    => true,
			]
		);
		$allowed_html['span']     = array_merge(
			$allowed_html['span'] ?? [],
			[
				'class'     => true,
				'data-*'    => true,
				'aria-*'    => true,
			]
		);
		$allowed_html['p']        = array_merge(
			$allowed_html['p'] ?? [],
			[
				'class'     => true,
				'data-*'    => true,
				'aria-*'    => true,
			]
		);
		$allowed_html['div']      = array_merge(
			$allowed_html['div'] ?? [],
			[
				'class'     => true,
				'data-*'    => true,
				'aria-*'    => true,
			]
		);

		return $allowed_html;
	}
}
