<?php
/**
 * Shipping method logo label for classic checkout.
 *
 * @package DhlPolska\ShippingMethod
 */

namespace DhlPolska\ShippingMethod;

use DhlPolska\Integration\FlexibleShipping\FlexibleShippingIntegration;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * Adds DHL logo under shipping method name in classic checkout.
 */
class ShippingMethodLogoLabel implements Hookable {

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

	/**
	 * @param string $plugin_url .
	 */
	public function __construct( $plugin_url ) {
		$this->plugin_url = rtrim( (string) $plugin_url, '/' ) . '/';
	}

	/**
	 * Hooks.
	 */
	public function hooks() {
		add_filter( 'woocommerce_cart_shipping_method_full_label', [ $this, 'add_logo_to_label' ], 10, 2 );
	}

	/**
	 * @param string $label  .
	 * @param mixed  $method .
	 *
	 * @return string
	 */
	public function add_logo_to_label( string $label, $method ): string {
		if ( ! $this->should_add_logo() ) {
			return $label;
		}

		if ( ! $this->is_dhl_method( $method ) ) {
			return $label;
		}

		if ( ! $this->is_logo_enabled( $method ) ) {
			return $label;
		}

		return $label . $this->get_logo_markup();
	}

	/**
	 * @return bool
	 */
	private function should_add_logo(): bool {
		return is_checkout() || defined( 'WOOCOMMERCE_CHECKOUT' );
	}

	/**
	 * @param mixed $method .
	 *
	 * @return bool
	 */
	private function is_dhl_method( $method ): bool {
		if ( is_object( $method ) && method_exists( $method, 'get_method_id' ) ) {
			$method_id = (string) $method->get_method_id();
			if ( DhlShippingMethod::SHIPPING_METHOD_ID === $method_id ) {
				return true;
			}
		}

		$fs_method   = $this->get_fs_method_meta( $this->get_meta_data( $method ) );
		$integration = $fs_method['method_integration'] ?? $fs_method['integration_id'] ?? '';

		return FlexibleShippingIntegration::INTEGRATION_ID === $integration;
	}

	/**
	 * @param mixed $method .
	 *
	 * @return bool
	 */
	private function is_logo_enabled( $method ): bool {
		$meta_data = $this->get_meta_data( $method );
		$fs_method = $this->get_fs_method_meta( $meta_data );

		$show_logo = null;

		if ( isset( $fs_method[ DhlInstanceSettings::OPTION_SHOW_LOGO ] ) ) {
			$show_logo = $fs_method[ DhlInstanceSettings::OPTION_SHOW_LOGO ];
		} elseif ( isset( $fs_method[ FlexibleShippingIntegration::INTEGRATION_ID . '_show_logo' ] ) ) {
			$show_logo = $fs_method[ FlexibleShippingIntegration::INTEGRATION_ID . '_show_logo' ];
		}

		if ( null === $show_logo && isset( $meta_data[ FlexibleShippingIntegration::INTEGRATION_ID . '_show_logo' ] ) ) {
			$show_logo = $meta_data[ FlexibleShippingIntegration::INTEGRATION_ID . '_show_logo' ];
		}

		if ( null === $show_logo && isset( $meta_data[ DhlInstanceSettings::OPTION_SHOW_LOGO ] ) ) {
			$show_logo = $meta_data[ DhlInstanceSettings::OPTION_SHOW_LOGO ];
		}

		if ( null === $show_logo ) {
			return false;
		}

		return $this->normalize_flag_value( $show_logo );
	}

	/**
	 * @param mixed $value .
	 *
	 * @return bool
	 */
	private function normalize_flag_value( $value ): bool {
		if ( is_bool( $value ) ) {
			return $value;
		}

		if ( is_numeric( $value ) ) {
			return (int) $value === 1;
		}

		$normalized = strtolower( trim( (string) $value ) );

		return in_array( $normalized, [ 'yes', 'true', '1', 't' ], true );
	}

	/**
	 * @param mixed $method .
	 *
	 * @return array<string, mixed>
	 */
	private function get_meta_data( $method ): array {
		if ( ! is_object( $method ) || ! method_exists( $method, 'get_meta_data' ) ) {
			return [];
		}

		$meta_data = $method->get_meta_data();

		return is_array( $meta_data ) ? $meta_data : [];
	}

	/**
	 * @param array<string, mixed> $meta_data .
	 *
	 * @return array<string, mixed>
	 */
	private function get_fs_method_meta( array $meta_data ): array {
		if ( ! isset( $meta_data['_fs_method'] ) ) {
			return [];
		}

		$value = $meta_data['_fs_method'];
		if ( is_array( $value ) ) {
			return $value;
		}

		if ( is_string( $value ) ) {
			$decoded = json_decode( $value, true );
			if ( is_array( $decoded ) ) {
				return $decoded;
			}
		}

		return [];
	}

	/**
	 * @return string
	 */
	private function get_logo_markup(): string {
		$logo_url = esc_url( $this->plugin_url . 'assets/images/DHL_Logo.svg' );
		$alt_text = esc_attr__( 'DHL Polska', 'dhl-polska' );

		return sprintf(
			'<span class="dhl-polska-shipping-logo" style="display:block;margin-top:6px;"><img src="%s" alt="%s" style="max-width:120px;height:auto;" loading="lazy" /></span>',
			$logo_url,
			$alt_text
		);
	}
}
