<?php

namespace Easymailing\App\Core\Provider\Woocommerce\Widgets;

use Easymailing\App\Core\Provider\Woocommerce\Service\MetadataHelper;
use Easymailing\App\Core\Provider\Woocommerce\Service\SynchronizationViewHandler;


class OrderWidgets
{

	private $synchronizationViewHandler;

	public function __construct(SynchronizationViewHandler $synchronizationViewHandler)
	{
		$this->synchronizationViewHandler = $synchronizationViewHandler;
	}

	function addWidgets($order): void
	{

		add_meta_box(
			'easymailing_synchronization_status_meta_box_id',
			__('Sincronización Easymailing', 'easymailing'),
			array($this, 'addMetaBoxContent'),
			'woocommerce_page_wc-orders',
			'side',
			'default'
		);

	}

	public function addMetaBoxContent($post): void
	{
		$status = get_post_meta($post->ID, MetadataHelper::getSynchronizationStatus(), true);
		$content = $this->synchronizationViewHandler->getWidgetContent($status, $post->ID, 'order');
		echo $content;
	}

	public function addSynchronizationColumn($columns): array
	{
		$columns[MetadataHelper::getSynchronizationStatus()] = __('Sincronización Easymailing', 'easymailing');
		return $columns;
	}

//	public function addWidgets($order): void
//	{
//		$orderId = $order->get_id();
//
//		$status = get_post_meta($orderId, MetadataHelper::getSynchronizationStatus(), true);
//		$synchroHtml = $this->synchronizationViewHandler->getSynchronizationColumnContent($status, $orderId, 'order');
//
//		echo $content;
//
//		// Genera el contenido para mostrar
//		$content = '
//        <div class="easymailing-order-widget" >
//            <h4 class="em-m-0 em-fs-6">' . __('Sincronización Easymailing', 'easymailing') . '</h4>
//            <p><strong>Order Resource ID:</strong> ' . esc_html(get_post_meta($orderId, MetadataHelper::getResourceId(), true)) . '</p>
//            <p><strong>Customer Resource ID:</strong> ' . esc_html(get_post_meta($orderId, MetadataHelper::getResourceId(), true)) . '</p>
//            <div class="em-mt-4">
//            '.$synchroHtml.'
//		</div>'
//		;
//
//		$content .= '</div>';
//		echo $content;
//	}



    function showCustomOrderColumnData($columnName, $order): void
    {
        $orderId = $order->get_id();
        if (MetadataHelper::getSynchronizationStatus() === $columnName) {
            $status = get_post_meta($orderId, MetadataHelper::getSynchronizationStatus(), true);

            $content = $this->synchronizationViewHandler->getTableContent($status, $orderId, 'order');
            echo $content;
        }
    }
}
