<?php
/**
 * Class ShipmentTemplateListColumns
 *
 * @package DhlPolska\ShipmentTemplates
 */

namespace DhlPolska\ShipmentTemplates;

use DhlPolska\Shipment\ShipmentTypes;
use DhlPolska\WooCommerceSettings\Templates\TemplatesRepository;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * Customize shipment templates list columns.
 */
class ShipmentTemplateListColumns implements Hookable {
	private const COLUMN_TYPE       = 'dhl_polska_template_type';
	private const COLUMN_WEIGHT     = 'dhl_polska_template_weight';
	private const COLUMN_DIMENSIONS = 'dhl_polska_template_dimensions';
	private const COLUMN_COD        = 'dhl_polska_template_cod';
	private const COLUMN_INSURANCE  = 'dhl_polska_template_insurance';

	private TemplatesRepository $templates_repository;

	private ShipmentTypes $shipment_types;

	public function __construct( TemplatesRepository $templates_repository, ShipmentTypes $shipment_types ) {
		$this->templates_repository = $templates_repository;
		$this->shipment_types       = $shipment_types;
	}

	/**
	 * Hooks.
	 */
	public function hooks() {
		add_filter( 'manage_' . ShipmentTemplatePostType::POST_TYPE . '_posts_columns', [ $this, 'add_columns' ] );
		add_action( 'manage_' . ShipmentTemplatePostType::POST_TYPE . '_posts_custom_column', [ $this, 'render_column' ], 10, 2 );
	}

	/**
	 * @param array<string, string> $columns .
	 *
	 * @return array<string, string>
	 */
	public function add_columns( array $columns ): array {
		$updated = [];

		foreach ( $columns as $key => $label ) {
			$updated[ $key ] = $label;

			if ( 'title' === $key ) {
				$updated[ self::COLUMN_TYPE ]       = __( 'Typ', 'dhl-polska' );
				$updated[ self::COLUMN_WEIGHT ]     = __( 'Domyślna waga', 'dhl-polska' );
				$updated[ self::COLUMN_DIMENSIONS ] = __( 'Wymiary', 'dhl-polska' );
				$updated[ self::COLUMN_COD ]        = __( 'Pobranie', 'dhl-polska' );
				$updated[ self::COLUMN_INSURANCE ]  = __( 'Ubezpieczenie', 'dhl-polska' );
			}
		}

		if ( ! isset( $updated[ self::COLUMN_TYPE ] ) ) {
			$updated[ self::COLUMN_TYPE ]       = __( 'Typ', 'dhl-polska' );
			$updated[ self::COLUMN_WEIGHT ]     = __( 'Domyślna waga', 'dhl-polska' );
			$updated[ self::COLUMN_DIMENSIONS ] = __( 'Wymiary', 'dhl-polska' );
			$updated[ self::COLUMN_COD ]        = __( 'Pobranie', 'dhl-polska' );
			$updated[ self::COLUMN_INSURANCE ]  = __( 'Ubezpieczenie', 'dhl-polska' );
		}

		return $updated;
	}

	/**
	 * @param string $column .
	 * @param int    $post_id .
	 *
	 * @return void
	 */
	public function render_column( string $column, int $post_id ): void {
		$template        = $this->templates_repository->get_template_meta( $post_id );
		$weight_unit     = get_option( 'woocommerce_weight_unit', 'kg' );
		$dimension_unit  = get_option( 'woocommerce_dimension_unit', 'cm' );
		$weight          = wc_format_decimal( (string) ( $template['weight'] ?? '' ) );
		$type_labels     = $this->shipment_types->get_type_labels();
		$type_label      = $type_labels[ (string) ( $template['type'] ?? '' ) ] ?? (string) ( $template['type'] ?? '' );
		$dimensions      = sprintf(
			'%s x %s x %s',
			(string) ( $template['dimensions']['length'] ?? '' ),
			(string) ( $template['dimensions']['width'] ?? '' ),
			(string) ( $template['dimensions']['height'] ?? '' )
		);
		$cod             = (bool) ( $template['services']['cod'] ?? false );
		$insurance       = (bool) ( $template['services']['insurance'] ?? false );
		$cod_label       = $cod ? __( 'Tak', 'dhl-polska' ) : __( 'Nie', 'dhl-polska' );
		$insurance_label = $insurance ? __( 'Tak', 'dhl-polska' ) : __( 'Nie', 'dhl-polska' );

		$output_map = [
			self::COLUMN_TYPE       => $type_label,
			self::COLUMN_WEIGHT     => trim( $weight . ' ' . $weight_unit ),
			self::COLUMN_DIMENSIONS => trim( $dimensions . ' ' . $dimension_unit ),
			self::COLUMN_COD        => $cod_label,
			self::COLUMN_INSURANCE  => $insurance_label,
		];

		if ( ! isset( $output_map[ $column ] ) ) {
			return;
		}

		echo wp_kses_post( esc_html( $output_map[ $column ] ) );
	}
}
