<?php

namespace KupShop\PohodaBundle\Utils\Importers;

use KupShop\PohodaBundle\Utils\PohodaBase;
use KupShop\PohodaBundle\Utils\Tools\PohodaOrderImporter;

class OrdersImporter
{
    use PohodaBase;
    protected $transformXslt = 'ordersIn.xsl';
    protected $requestTemplate = 'requests/ordersIn.tpl';

    /**
     * @var PohodaOrderImporter
     */
    protected $pohodaOrderImporter;

    protected $editOrder = false;
    protected $insertOrder = false;

    protected array $IDs = [];

    public function processData($XMLStringResponse = null)
    {
        $this->pohodaConnector->prepareSyncTime();
        if (!$XMLStringResponse) {
            $XMLStringResponse = $this->getData();
        }

        if ($XMLStringResponse) {
            $resultXML = $this->transformHelper->baseTransform($XMLStringResponse, $this->transformHelper->loadXslt($this->transformXslt));
            $this->pohodaOrderImporter->importOrders($resultXML, $this->editOrder, $this->insertOrder);

            $this->pohodaConnector->setLastSyncTime('ordersIn');
        }
    }

    public function getData()
    {
        $request = $this->pohodaConnector->getRequestContent($this->requestTemplate, ['full' => $this->isFullSync(), 'IDs' => $this->IDs]);
        $request = $this->replace($request);

        $this->addRequestLog('Orders importer', $request);

        return $this->pohodaConnector->sendRequest($request);
    }

    /**
     * @required
     */
    public function setPohodaOrderImporter(PohodaOrderImporter $pohodaOrderImporter)
    {
        $this->pohodaOrderImporter = $pohodaOrderImporter;
    }

    public function setIDs($IDs)
    {
        $this->IDs = $IDs;
    }
}
