<?php

namespace Leadlion\Autogedal\Model\Export\RowCustomizer;

use Amasty\Feed\Model\Export\Product;
use Magento\CatalogImportExport\Model\Export\RowCustomizerInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Leadlion\Autogedal\Model\Export\Product\Attributes\FeedAttributesStorage;

class CustomShipCategCost implements RowCustomizerInterface
{
    const XML_SHIP_COST_CATEG = 'leadlion/shipping_cost_category/shipping_cost_category';

    private $productsInfo;
    private $scopeConfig;
    private $serializer;
    protected $_export;

    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        SerializerInterface $serializer,
        ScopeConfigInterface $scopeConfig,
        Product $export
    ) {
        $this->productRepository = $productRepository;
        $this->serializer = $serializer;
        $this->scopeConfig = $scopeConfig;
        $this->_export = $export;
    }

    private function getCatShipCost()
    {
        $confSettings = $this->scopeConfig->getValue(
            self::XML_SHIP_COST_CATEG,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        $info = [];
        if ($confSettings) {
            $feedConfig = $this->serializer->unserialize($confSettings);

            $info = [];
            foreach ($feedConfig as $config) {
                $info[$config['category_id']] = $config['shipping_cost'];
            }
        }
        
        return $info;
    }

    /**
     * @inheritdoc
     */
    public function prepareData($collection, $productIds)
    {
        if ($this->_export->getAttributesStorage()->hasAttributes(FeedAttributesStorage::PREFIX_CATEGORY_SHIP_COST)) {
            $feedConfig = $this->getCatShipCost();
            if (!empty($productIds) && !empty($feedConfig)) {
                foreach ($productIds as $key => $productId) {
                    /** @var \Magento\Catalog\Model\Product $product */
                    $product = $this->productRepository->getById($productId);
                    $categoriesIds = $product->getCategoryIds();
                    if (!empty($categoriesIds)) {
                        foreach ($categoriesIds as $key => $categoryId) {
                            if (isset($feedConfig[$categoryId])) {
                                $this->productsInfo[$productId][] = $feedConfig[$categoryId];
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function addHeaderColumns($columns)
    {
        return $columns;
    }

    /**
     * @inheritdoc
     */
    public function addData($dataRow, $productId)
    {
        $customData = &$dataRow['amasty_custom_data'];

        $catShipCost = '19.00 RON';
        if (isset($customData[FeedAttributesStorage::PREFIX_CATEGORY_SHIP_COST])) {
            $catShipCost = $customData[FeedAttributesStorage::PREFIX_CATEGORY_SHIP_COST];
        }
        $customData[FeedAttributesStorage::PREFIX_CATEGORY_SHIP_COST] = [
            FeedAttributesStorage::PREFIX_CATEGORY_SHIP_COST => $catShipCost
        ];

        return $dataRow;
    }

    /**
     * @inheritdoc
     */
    public function getAdditionalRowsCount($additionalRowsCount, $productId)
    {
        return $additionalRowsCount;
    }
}
