<?php
namespace Autogedal\Banners\Model\Banner;
use Autogedal\Banners\Model\ResourceModel\Banner\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Class DataProvider
 */
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    protected $collection;

    /**
     * @var DataPersistorInterface
     */
    protected $dataPersistor;

    /**
     * @var array
     */
    protected $loadedData;

    protected $storeManager;
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        DataPersistorInterface $dataPersistor,
        StoreManagerInterface $storeManager,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $collectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        $this->storeManager = $storeManager;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->meta = $this->prepareMeta($this->meta);
    }

    /**
     * Prepares Meta
     *
     * @param array $meta
     * @return array
     */
    public function prepareMeta(array $meta)
    {
        return $meta;
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();

        foreach ($items as $item) {
            $this->loadedData[$item->getId()] = $item->getData();
            if ($item->getImage()) {
                $m['image'][0]['name'] = $item->getImage();
                $m['image'][0]['url'] = $this->getMediaUrl().$item->getImage();
                $fullData = $this->loadedData;
                $this->loadedData[$item->getId()] = array_merge($fullData[$item->getId()], $m);
            }
            if ($item->getImageMobile()) {
                $m['image_mobile'][0]['name'] = $item->getImageMobile();
                $m['image_mobile'][0]['url'] = $this->getMediaUrl().$item->getImageMobile();
                $fullData = $this->loadedData;
                $this->loadedData[$item->getId()] = array_merge($fullData[$item->getId()], $m);
            }
        }

        $data = $this->dataPersistor->get('creadiv_banners_banner');
        if (!empty($data)) {
            $item = $this->collection->getNewEmptyItem();
            $item->setData($data);
            $this->loadedData[$item->getId()] = $item->getData();
            $this->dataPersistor->clear('creadiv_banners_banner');
        }

        return $this->loadedData;
    }

    public function getMediaUrl()
    {
        $mediaUrl = $this->storeManager->getStore()
                ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'banners/tmp/icon/';
        return $mediaUrl;
    }

    protected function getFieldsMap()
    {
        return [
            'general' => [
                'parent',
                'path',
                'is_active',
                'include_in_menu',
                'name',
            ],
            'content' => [
                'image',
                'description',
                'landing_page',
            ],
            'display_settings' => [
                'display_mode',
                'is_anchor',
                'available_sort_by',
                'use_config.available_sort_by',
                'default_sort_by',
                'use_config.default_sort_by',
                'filter_price_range',
                'use_config.filter_price_range',
                'banners_selection'
            ],
            'search_engine_optimization' => [
                'url_key',
                'url_key_create_redirect',
                'url_key_group',
                'meta_title',
                'meta_keywords',
                'meta_description',
            ],
            'assign_products' => [
            ],
            'design' => [
                'custom_use_parent_settings',
                'custom_apply_to_products',
                'custom_design',
                'page_layout',
                'custom_layout_update',
            ],
            'schedule_design_update' => [
                'custom_design_from',
                'custom_design_to',
            ],
            'category_view_optimization' => [
            ],
            'category_permissions' => [
            ],
        ];
    }
}
