<?php

namespace DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Service\Viewer;

use DropshippingXmlFreeVendor\WPDesk\View\Renderer\Renderer;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Analyser\XmlAnalyser;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Infrastructure\Data\DataFormat;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Service\Locator\FileLocatorService;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Form\Fields\ImportCsvSelectorFormFields;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Infrastructure\Service\Listener\Items\Conditional\Conditional;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Infrastructure\View\Abstraction\Displayable;
/**
 * Class CsvSidebarViewerService
 *
 * @package WPDesk\Library\DropshippingXmlCore\Service\Viewer
 */
class CsvSidebarViewerService implements Displayable, Conditional
{
    const FIRST_PAGE = 1;
    /**
     * @var Renderer
     */
    private $renderer;
    /**
     * @var XmlAnalyser
     */
    private $xml_analyser;
    /**
     * @var FileLocatorService
     */
    private $file_locator;
    /**
     * @var SidebarViewerService
     */
    private $sidebar_viewer_service;
    /**
     * @var string
     */
    private $uid = '';
    private array $render_data = [];
    public function __construct(Renderer $renderer, XmlAnalyser $xml_analyser, FileLocatorService $file_locator, SidebarViewerService $sidebar_viewer_service, string $uid)
    {
        $this->renderer = $renderer;
        $this->xml_analyser = $xml_analyser;
        $this->file_locator = $file_locator;
        $this->uid = $uid;
        $this->sidebar_viewer_service = $sidebar_viewer_service;
    }
    public function show()
    {
        $this->sidebar_viewer_service->show();
        $this->show_csv();
    }
    public function isActive(): bool
    {
        return !empty($this->get_render_data($this->uid));
    }
    private function show_csv()
    {
        $process_data = $this->get_render_data($this->uid);
        $data = ['title' => __('Csv table view', 'dropshipping-xml-for-woocommerce'), 'renderer' => $this->renderer];
        $data = array_merge($data, $process_data);
        $this->renderer->output_render('Import/Sidebar_CSV', $data);
    }
    private function get_render_data(string $uid): array
    {
        if (isset($this->render_data[$uid])) {
            return $this->render_data[$uid];
        }
        $converted_file = $this->file_locator->get_converted_file($uid);
        $this->xml_analyser->load_from_file($converted_file);
        $this->render_data[$uid] = ['items' => $this->xml_analyser->count_element(ImportCsvSelectorFormFields::NODE_ELEMENT_VALUE), 'item_nr' => self::FIRST_PAGE, 'table_data' => ['node' => $this->xml_analyser->get_element_as_object(ImportCsvSelectorFormFields::NODE_ELEMENT_VALUE, self::FIRST_PAGE)], 'format' => DataFormat::CSV];
        return $this->render_data[$uid];
    }
}
