<?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 ProductWidgets
{
	private $synchronizationViewHandler;

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

    public function addWidgets(): void
    {
        add_meta_box(
            'easymailing_synchronization_status_meta_box_id',
	        __('Sincronización Easymailing', 'easymailing'),
            array($this, 'addMetaBoxContent'),
            'product',
            'side',
            'default'
        );
    }

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

		$content .= esc_html(get_post_meta($post->ID, MetadataHelper::getResourceId(), true)) ;
		$content .= esc_html(get_post_meta($post->ID, MetadataHelper::isResourceCreated(), true)) ;
        echo $content;
    }

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

    function showCustomProductColumnData($columnName, $productId): void
    {
        if (MetadataHelper::getSynchronizationStatus() === $columnName) {
            $status = get_post_meta($productId, MetadataHelper::getSynchronizationStatus(), true);
            $content = $this->synchronizationViewHandler->getTableContent($status, $productId, 'product');
            echo $content;
        }
    }
}
