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

namespace DhlPolska\Shipment\Metabox\Field;

use DhlPolska\Shipment\ShipmentTypes;

/**
 * Renderer for a single DHL package row in the metabox.
 */
class PackageRow {

	/**
	 * @var int
	 */
	private $package_number;

	/**
	 * @var array
	 */
	private $package;

	/**
	 * @var bool
	 */
	private $disabled;

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

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

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

	/**
	 * PackageRow constructor.
	 *
	 * @param int           $package_number .
	 * @param array         $package        .
	 * @param ShipmentTypes $shipment_types .
	 * @param bool          $disabled       .
	 * @param string        $weight_unit    .
	 * @param string        $dimension_unit .
	 */
	public function __construct(
		int $package_number,
		array $package,
		ShipmentTypes $shipment_types,
		bool $disabled = false,
		string $weight_unit = 'kg',
		string $dimension_unit = 'cm'
	) {
		$this->package_number = $package_number;
		$this->package        = $package;
		$this->shipment_types = $shipment_types;
		$this->disabled       = $disabled;
		$this->weight_unit    = $weight_unit;
		$this->dimension_unit = $dimension_unit;
	}

	/**
	 * @return string
	 */
	public function render(): string {
		$package        = $this->package;
		$fields         = $this->get_fields();
		$package_number = $this->package_number;
		$disabled       = $this->disabled;
		$weight_unit    = $this->weight_unit;
		$dimension_unit = $this->dimension_unit;

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

		return (string) ob_get_clean();
	}

	/**
	 * @return array
	 */
	private function get_fields(): array {
		return [
			'type'   => [
				'key'               => 'packages[' . $this->package_number . '][type]',
				'label'             => __( 'Typ', 'dhl-polska' ),
				'type'              => 'select',
				'required'          => true,
				'value'             => $this->package['type'] ?? ShipmentTypes::TYPE_PACKAGE,
				'options'           => $this->shipment_types->get_type_labels(),
				'class'             => [ 'form-row-wide' ],
			],
			'weight' => [
				'key'               => 'packages[' . $this->package_number . '][weight]',
				'label'             => sprintf(
					// Translators: weight unit.
					__( 'Waga (%s)', 'dhl-polska' ),
					$this->weight_unit
				),
				'input_class'       => [ 'js--field-weight' ],
				'type'              => 'number',
				'required'          => true,
				'value'             => $this->package['weight'] ?? '',
				'custom_attributes' => [
					'min'  => 0.01,
					'step' => 0.01,
				],
				'class'             => [ 'form-row-wide' ],
			],
			'width'  => [
				'key'               => 'packages[' . $this->package_number . '][width]',
				'label'             => '',
				'type'              => 'number',
				'value'             => $this->package['width'] ?? '',
				'custom_attributes' => [
					'min'         => 1,
					'step'        => 1,
					'placeholder' => __( 'szer.', 'dhl-polska' ),
				],
				'class'             => [ 'form-row-dimension' ],
			],
			'height' => [
				'key'               => 'packages[' . $this->package_number . '][height]',
				'label'             => '',
				'type'              => 'number',
				'value'             => $this->package['height'] ?? '',
				'custom_attributes' => [
					'min'         => 1,
					'step'        => 1,
					'placeholder' => __( 'wys.', 'dhl-polska' ),
				],
				'class'             => [ 'form-row-dimension' ],
			],
			'length' => [
				'key'               => 'packages[' . $this->package_number . '][length]',
				'label'             => '',
				'type'              => 'number',
				'value'             => $this->package['length'] ?? '',
				'custom_attributes' => [
					'min'         => 1,
					'step'        => 1,
					'placeholder' => __( 'dł.', 'dhl-polska' ),
				],
				'class'             => [ 'form-row-dimension' ],
			],
		];
	}
}
