<?php

namespace Ilabs\Inpost_International\Emails;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly.

/**
 * OrderEmail
 */
class OrderEmail {


	/**
	 * Constructor
	 */
	public function __construct() {
		$this->hooks();
	}

	/**
	 * Hooks
	 */
	public function hooks() {
		add_action(
			'woocommerce_email_order_meta',
			array( $this, 'print_parcel_machine_info' ),
			10,
			3
		);
	}

	/**
	 * Add info about locker point to New Order Email
	 *
	 * @param WC_Order $wc_order $wc_order.
	 */
	public function print_parcel_machine_info( $wc_order ) {

		if ( ! $wc_order || is_wp_error( $wc_order ) || ! is_object( $wc_order ) ) {
			return;
		}

		$locker_point = $wc_order->get_meta( 'inpost_international_locker_id' );

		if ( empty( $locker_point ) ) {
			return;
		}

		$parcel_locker_methods = array(
			'inpost_international_automated_locker_point',
		);

		$fs_method_name = $wc_order->get_meta( 'inpost_international_fs_linked' );

		$shipping_method_id = '';

		foreach ( $wc_order->get_items( 'shipping' ) as $item_id => $item ) {
			$shipping_method_id = $item->get_method_id(); // The method ID.
		}

		if ( in_array( $shipping_method_id, $parcel_locker_methods, true )
			|| ( ! empty( $fs_method_name ) && in_array( $fs_method_name, $parcel_locker_methods, true ) ) ) {

			$notice = sprintf(
			/* translators: %s: Locker point name */
				esc_html__( 'Selected parcel machine: %s', 'inpost-international' ),
				esc_html( $locker_point )
			);

			$img_yellow = inpost_intl_core()->get_plugin_images_url() . '/yellow.png';
			$img        = ' <span class="inpost-intl-shipping-method-logo">
 						    <img style="width:60px;height:auto;" src="' . esc_url( $img_yellow ) . '" alt="Inpost International" />
 						</span>';

			$allowed_html = array(
				'span' => array(
					'class' => true,
				),
				'img'  => array(
					'style'  => true,
					'src'    => true,
					'alt'    => true,
					'width'  => true,
					'height' => true,
				),
			);

			echo "<div style='margin-bottom: 40px'>";
			echo wp_kses_post( $notice );
			echo wp_kses( $img, $allowed_html );
			echo '</div>';
		}
	}
}
