<?php

namespace Webkul\WooCommerceBundle\Connector\Writer;

use Webkul\WooCommerceBundle\Traits\DataMappingTrait;
use Webkul\WooCommerceBundle\Listener\ClassDefinationForCompatibility;

$obj = new ClassDefinationForCompatibility();
$obj->objectCreate();

/**
 * Add categories to WooCommerce.
 *
 * @author    Webkul Software Pvt Ltd
 * @copyright 2010-2017 Webkul pvt. ltd.
 * @license   https://store.webkul.com/license.html
 */
class RemoveTrackerWriter extends BaseWriter implements \ItemWriterInterface
{
    use DataMappingTrait;
    protected $parameters;
    const AKENEO_ENTITY_NAME = 'product';
    const SETTING_SECTION = 'woocommerce_connector_settings';

    public function write(array $items)
    {
        if (!$this->parameters) {
            $this->parameters = $this->stepExecution->getJobParameters();
            $exportProfile = $this->parameters->has('exportProfile') && (!empty($this->parameters->get('exportProfile'))) ? $this->parameters->get('exportProfile') : "";
             
        } else {
            $params = $this->stepExecution ? $this->stepExecution->getJobparameters()->all() : null;
            $exportProfile = (!empty($params['exportProfile'])) ? $params['exportProfile'] : "";
            
        }
        if ($this->getJobName() == 'woocommerce_quick_export') {
            $exportProfile = $this->connectorService->getDefaultCredential() ? $this->connectorService->getDefaultCredential()->getId() : '';
        }

        foreach ($items as $item) {
            if (!empty($item['parent'])) {
                $item['code'] = $item['parent'];
                $item['type'] = 'variable';
            } else {
                $item['code'] = $item['identifier'];
                $item['type'] = 'simple';
            }

            $mapping = $this->checkMappingInDb($item,$entity = self::AKENEO_ENTITY_NAME,$exportProfile);
            $varResult = null;

            if ($mapping) {
                $updateTrack = $this->connectorService->getEntityTrackByEntityAndCode(self::AKENEO_ENTITY_NAME, $item['code']);

                if ($updateTrack) {
                    $this->connectorService->removeTrack($updateTrack);
                    $this->stepExecution->incrementSummaryInfo('remove');
                    continue;
                } else {
                    $this->stepExecution->incrementSummaryInfo('read', -1);
                }
            }
        }
    }
}
