<?php
declare(strict_types=1);
/**
 * Limesharp_Stockists extension
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 *
 * @category  Limesharp
 * @package   Limesharp_Stockists
 * @copyright 2016 Claudiu Creanga
 * @license   http://opensource.org/licenses/mit-license.php MIT License
 * @author    Claudiu Creanga
 */

namespace Limesharp\Stockists\Controller\Ajax;

use Limesharp\Stockists\Model\Stores as StoresModel;
use Limesharp\Stockists\Model\ResourceModel\Stores\CollectionFactory;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Responsible for loading page content.
 *
 * This is a basic controller that only loads the corresponding layout file. It may duplicate other such
 * controllers, and thus it is considered tech debt. This code duplication will be resolved in future releases.
 */
class Stores extends \Magento\Framework\App\Action\Action
{

    /**
     * @var JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;

    /**
     * @var StoreManagerInterface
     */
    public $storeManager;

    public function __construct(
        Context $context,
        JsonFactory $resultJsonFactory,
        CollectionFactory $collectionFactory,
        StoreManagerInterface $storeManager
    )
    {
        $this->collectionFactory = $collectionFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->storeManager = $storeManager;
        parent::__construct($context);
    }

    /**
     * Load the page defined in view/frontend/layout/stockists_index_index.xml
     *
     * @return \Magento\Framework\Controller\Result\JsonFactory
     */
    public function execute()
    {
        $params = $this->getRequest()->getParams();

        $lat = isset($params['lat']) ? $params['lat'] : null;
        $lng = isset($params['lng']) ? $params['lng'] : null;
        $isBoutique = isset($params['boutique']) ? $params['boutique'] : false;
        $isTailor = isset($params['servizio_su_misura']) ? $params['servizio_su_misura'] : false;

        $collection = $this->collectionFactory->create()
            ->addFieldToFilter('status', StoresModel::STATUS_ENABLED)
            ->addStoreFilter($this->storeManager->getStore()->getId());

        if ($isBoutique):
            $collection->addFieldToFilter('boutique', ['eq' => 1]);
        endif;
        if ($isTailor):
            $collection->addFieldToFilter('servizio_su_misura', ['eq' => 1]);
        endif;

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $stores = $objectManager->create('Limesharp\Stockists\Model\Stores');
        $stockists = $stores->getFilteredRangeCollection($collection, $lat, $lng)->getData();

        $json = [];
        foreach ($stockists as $stockist) {
            $json[] = $stockist;
        }
        return $this->resultJsonFactory->create()->setData($json);
    }
}
