<?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\Catalog\ResourceModel\Layer\Filter;

class Attribute extends \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute
{
    //Which attributes will use reseted category collection
    protected $skipFilterAttributes = ["color"];

    public function getCount(\Magento\Catalog\Model\Layer\Filter\FilterInterface $filter)
    {
        $attribute = $filter->getAttributeModel();
        $attributeCode = $attribute->getAttributeCode();
        $storeId = $filter->getStoreId();
        $connection = $this->getConnection();
        $tableAlias = sprintf('%s_idx', $attributeCode);

        $select = clone $filter->getLayer()->getProductCollection()->getSelect();

        if (in_array($attributeCode, $this->skipFilterAttributes)) {

            $from = $select->getPart(\Magento\Framework\DB\Select::FROM);

            unset($from["search_result"]);

            $select->reset(\Magento\Framework\DB\Select::FROM);
            $select->setPart(\Magento\Framework\DB\Select::FROM, $from);
        }

        $select->reset(\Magento\Framework\DB\Select::COLUMNS);
        $select->reset(\Magento\Framework\DB\Select::ORDER);
        $select->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
        $select->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);

        $conditions = [
            "{$tableAlias}.entity_id = e.entity_id",
            $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
            $connection->quoteInto("{$tableAlias}.store_id = ?", $storeId),
        ];

        $select->join(
            [$tableAlias => $this->getMainTable()],
            join(' AND ', $conditions),
            ['value', 'count' => new \Zend_Db_Expr("COUNT({$tableAlias}.entity_id)")]
        )->group(
            "{$tableAlias}.value"
        );

        return $connection->fetchPairs($select);
    }
}
