<?php

namespace DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Action\View;

use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Factory\ViewActionFactory;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Infrastructure\Request\Request;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Infrastructure\Service\Container\Abstraction\ServiceContainerInterface;
use DropshippingXmlFreeVendor\WPDesk\Library\DropshippingXmlCore\Infrastructure\View\Abstraction\Displayable;
use DropshippingXmlFreeVendor\WPDesk\View\Renderer\Renderer;
/**
 * Class ViewAction, show view based on request.
 *
 * @package WPDesk\Library\DropshippingXmlCore\Action\View
 */
class ViewAction implements Displayable
{
    private Request $request;
    private ViewActionFactory $factory;
    private ServiceContainerInterface $service_container;
    private Renderer $renderer;
    public function __construct(Request $request, ViewActionFactory $factory, ServiceContainerInterface $service_container, Renderer $renderer)
    {
        $this->request = $request;
        $this->factory = $factory;
        $this->service_container = $service_container;
        $this->renderer = $renderer;
    }
    public function show()
    {
        try {
            $view = $this->factory->create_from_request($this->request);
            $this->service_container->register($view);
            $view->show();
        } catch (\Exception $e) {
            $data = ['title' => __('Error page', 'dropshipping-xml-for-woocommerce'), 'message' => $e->getMessage() . ' ' . __('Check whether the downloaded file has the correct XML structure including products.', 'dropshipping-xml-for-woocommerce'), 'renderer' => $this->renderer];
            $this->renderer->output_render('Error', $data);
        }
    }
}
