<?php

declare(strict_types=1);

namespace MyCustomizer\WooCommerce\Connector\Controller\Admin;

use MyCustomizer\WooCommerce\Connector\Factory\MczrFactory;
use WC_Order_Item;
use WC_Product;

class MczrOrderController
{
    /** @var MczrFactory */
    private $factory;
    /** @var \Twig\Environment */
    private $twig;

    public function __construct()
    {
        $this->factory = MczrFactory::getInstance();
        $this->twig = $this->factory->getTwig();
    }

    public function init(): void
    {
        \add_action('woocommerce_before_order_itemmeta', [$this, 'addMczrMetasAfterAdminOrderItemsTitle'], 10, 3);
    }

    /**
     * @param WC_Product|bool|null $_product
     */
    public function addMczrMetasAfterAdminOrderItemsTitle(int $item_id, WC_Order_Item $_item, $_product): void
    {
        $vars = [
            'props' => \wc_get_order_item_meta($item_id, 'mczr', true)
        ];

        $allowed_html = [
            'a' => [
                'href' => [],
                'target' => [],
            ],
            'br' => [],
            'button' => [
                'class' => [],
                'id' => [],
                'name' => [],
                'type' => [],
            ],
            'div' => [
                'class' => [],
            ],
            'fieldset' => [],
            'form' => [
                'name' => [],
                'method' => [],
            ],
            'h1' => [],
            'h2' => [],
            'h3' => [],
            'h4' => [],
            'hr' => [],
            'img' => [
                'alt' => [],
                'class' => [],
                'src' => [],
                'width' => [],
            ],
            'input' => [
                'id' => [],
                'class' => [],
                'name' => [],
                'type' => [],
                'readonly' => [],
                'value' => [],
            ],
            'label' => [
                'class' => [],
                'for' => [],
            ],
            'li' => [
                'class' => [],
            ],
            'textarea' => [
                'class' => [],
                'id' => [],
                'help' => [],
                'name' => [],
            ],
            'small' => [
                'class' => [],
            ],
            'strong' => [],
            'style' => [],
            'ul' => [
                'class' => [],
            ],
        ];

        echo \wp_kses($this->twig->render('Admin/order-properties.html.twig', $vars), $allowed_html);
    }
}
