<?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;

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

		return (string) ob_get_clean();
	}
}
