<?php

namespace Leadlion\Autogedal\Model\Export\RowCustomizer;

use Amasty\Feed\Model\Export\Product;
use Magento\CatalogImportExport\Model\Export\RowCustomizerInterface;
use Magento\Framework\UrlInterface;
use Leadlion\Autogedal\Model\Export\Product\Attributes\FeedAttributesStorage;

class Gallery implements RowCustomizerInterface
{
    /**
     * @var string
     */
    private const CACHE_HASH = '51a0da6e8666473d8dee16c1e7fd6750';

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var string
     */
    protected $urlPrefix;

    /**
     * @var array
     */
    protected $gallery = [];

    /**
     * @var Product
     */
    protected $export;

    /**
     * @var \Magento\Framework\App\ProductMetadataInterface
     */
    protected $productMetadata;

    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    protected $resource;

    /**
     * @var \Magento\Framework\DB\Adapter\AdapterInterface
     */
    protected $connection;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\ProductMetadataInterface $productMetadata,
        \Magento\Framework\App\ResourceConnection $resource,
        Product $export
    ) {
        $this->storeManager = $storeManager;
        $this->export = $export;
        $this->productMetadata = $productMetadata;
        $this->connection = $resource->getConnection();
        $this->resource = $resource;
    }

    /**
     * @inheritdoc
     */
    public function prepareData($collection, $productIds)
    {
        if ($this->export->getAttributesStorage()->hasAttributes(FeedAttributesStorage::PREFIX_GALLERY_ATTRIBUTE)) {
            $this->urlPrefix = $this->storeManager->getStore($collection->getStoreId())
                    ->getBaseUrl(UrlInterface::URL_TYPE_MEDIA)
                . 'catalog/product';

            $this->gallery = $this->export->getMediaGallery($productIds);
        }
    }

    /**
     * @return array
     */
    public function getGallery()
    {
        return $this->gallery;
    }

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

    /**
     * @inheritdoc
     */
    public function addData($dataRow, $productId)
    {
        $productId = $this->convertEntityIdToRowIdIfNeed((int)$productId);
        $customData = &$dataRow['amasty_custom_data'];
        $gallery = $this->getGallery();
        $gallery = $gallery[$productId] ?? [];
        $galleryImg = [];
        if (isset($dataRow['_attribute_set']) && in_array($dataRow['_attribute_set'],["Bare Transversale Bundle", "Bare Transversale Simple"])) {
            foreach ($gallery as $key => $data) {
                $data['_media_image'] = '/' . ltrim($data['_media_image'], '/');

                if (!isset($customData['image'])
                    || !in_array($this->getCacheImageUrl($data['_media_image']), $customData['image'])
                ) {
                    $galleryImg[] = $this->getCacheImageUrl($data['_media_image']);
                }
            }
        } else {
            foreach ($gallery as $key => $data) {
                $data['_media_image'] = '/' . ltrim($data['_media_image'], '/');

                if (!isset($customData['image'])
                    || !in_array($this->urlPrefix . $data['_media_image'], $customData['image'])
                ) {
                    $galleryImg[] = $this->urlPrefix . $data['_media_image'];
                }
            }
        }

        $customData[FeedAttributesStorage::PREFIX_GALLERY_ATTRIBUTE] = [
            'image_1' => isset($galleryImg[0]) ? $galleryImg[0] : null,
            'image_2' => isset($galleryImg[1]) ? $galleryImg[1] : null,
            'image_3' => isset($galleryImg[2]) ? $galleryImg[2] : null,
            'image_4' => isset($galleryImg[3]) ? $galleryImg[3] : null,
            'image_5' => isset($galleryImg[4]) ? $galleryImg[4] : null,
        ];

        return $dataRow;
    }

    /**
     * Get cached image URL
     *
     * @param string $imagePath
     * @return string
     */
    private function getCacheImageUrl($imagePath)
    {
        return $this->storeManager->getStore()
                ->getBaseUrl(UrlInterface::URL_TYPE_MEDIA)
            . 'catalog/product/cache/' . self::CACHE_HASH . $imagePath;
    }
    
    /**
     * @inheritdoc
     */
    public function getAdditionalRowsCount($additionalRowsCount, $productId)
    {
        return $additionalRowsCount;
    }

    protected function convertEntityIdToRowIdIfNeed(int $id): int
    {
        if ($this->productMetadata->getEdition() == 'Community') {
            return $id;
        }

        $tableName = $this->resource->getTableName('catalog_product_entity');
        $select = $this->connection->select()
            ->from($tableName, ['row_id'])
            ->where('entity_id = ?', $id)
            ->group('row_id');
        $result = $this->connection->fetchOne($select);

        return (int)$result;
    }
}
