<?php
/**
 * Plumrocket Inc.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the End-user License Agreement
 * that is available through the world-wide-web at this URL:
 * http://wiki.plumrocket.net/wiki/EULA
 * If you are unable to obtain it through the world-wide-web, please
 * send an email to support@plumrocket.com so we can send you a copy immediately.
 *
 * @package     Plumrocket Product Filter v3.x.x
 * @copyright   Copyright (c) 2018 Plumrocket Inc. (http://www.plumrocket.com)
 * @license     http://wiki.plumrocket.net/wiki/EULA  End-user License Agreement
 */

namespace Plumrocket\ProductFilter\Model;

class FilterList extends \Magento\Framework\Model\AbstractModel
{

    /**
     * @var Config\Source\AdditionalFilters
     */
    protected $_additionalFilters;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
     */
    protected $_filterableAttributes;

    /**
     * FilterList constructor.
     *
     * @param \Magento\Framework\Model\Context                                         $context
     * @param \Magento\Framework\Registry                                              $registry
     * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $filterableAttributes
     * @param Config\Source\AdditionalFilters                                          $additionalFilters
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null             $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb|null                       $resourceCollection
     * @param array                                                                    $data
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $filterableAttributes,
        \Plumrocket\ProductFilter\Model\Config\Source\AdditionalFilters $additionalFilters,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        $this->_additionalFilters = $additionalFilters;
        $this->_filterableAttributes = $filterableAttributes;
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    /**
     * @return array
     */
    public function getFilters()
    {
        $collection = $this->_filterableAttributes->create();
        $collection->setItemObjectClass('Magento\Catalog\Model\ResourceModel\Eav\Attribute')
            ->setOrder('position', 'ASC');

        $collection->addFieldToFilter(
            ['additional_table.is_filterable', 'frontend_input'],
            [
                ['gt' => 0],
                ['in' => ['select', 'price', 'multiselect']]
            ]
        );

        $collection->addFieldToFilter('attribute_code', ['neq' => 'visibility']);

        //Create simple array with attributes
        $attributes = [];
        foreach ($collection as $attr) {
            $attributes[$attr->getAttributeCode()] = $attr;
        }

        //Adding additional filters
        $additionalFilters = $this->_getAdditionalFilters();
        $attributes = array_merge($attributes, $additionalFilters);

        return $attributes;
    }

    /**
     * @return \Magento\Framework\DataObject[]
     */
    protected function _getAdditionalFilters()
    {
        $filters = $this->_additionalFilters->toArray();

        $_filters = [];
        foreach ($filters as $code => $filter) {
            $_filters[$code] = new \Magento\Framework\DataObject(
                [
                    'attribute_code' => $code,
                    'frontend_label' => $filter
                ]
            );
        }
        return $_filters;
    }

    /**
     * Retrieve attributes in format: code => label
     *
     * @return string[]
     */
    public function getAttributeList()
    {
        $attributeList = [];
        $attributes = $this->getFilters();
        foreach ($attributes as $code => $attribute) {
            /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
            $attributeList[$code] = (string)$attribute->getFrontendLabel();
        }

        asort($attributeList);

        return $attributeList;
    }
}
