<?php
/**
 * BINSHOPS | Best In Shops
 *
 * @author BINSHOPS | Best In Shops
 * @copyright BINSHOPS | Best In Shops
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 * Best In Shops eCommerce Solutions Inc.
 *
 */

require_once dirname(__FILE__) . '/../AbstractRESTController.php';
require_once dirname(__FILE__) . '/../../classes/RESTProductLazyArray.php';
require_once dirname(__FILE__) . '/../../classes/RESTProductLazyArrayLegacy.php';

use PrestaShop\PrestaShop\Adapter\Category\CategoryProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;

/**
 * This REST endpoint gets featured products list
 *
 */
class BinshopsrestFeaturedproductsModuleFrontController extends AbstractRESTController
{
    protected function processGetRequest()
    {
        $featuredProductsList = $this->getFeaturedProducts();

        $this->ajaxRender(json_encode([
            'code' => 200,
            'success' => true,
            'psdata' => $featuredProductsList['products']
        ]));
        die;
    }

    public function getFeaturedProducts(){
        $category = new Category((int) Configuration::get('HOME_FEATURED_CAT'));

        $searchProvider = new CategoryProductSearchProvider(
            $this->context->getTranslator(),
            $category
        );

        $context = new ProductSearchContext($this->context);

        $query = new ProductSearchQuery();
        $nProducts = Configuration::get('HOME_FEATURED_NBR');
        if ($nProducts < 0) {
            $nProducts = 12;
        }

        $query
            ->setResultsPerPage($nProducts)
            ->setPage(1)
        ;

        if (Configuration::get('HOME_FEATURED_RANDOMIZE')) {
            $query->setSortOrder(SortOrder::random());
        } else {
            $query->setSortOrder(new SortOrder('product', 'position', 'asc'));
        }

        $result = $searchProvider->runQuery(
            $context,
            $query
        );

        $products_for_template = [];
        $settings = $this->getProductPresentationSettings();
        $retriever = new \PrestaShop\PrestaShop\Adapter\Image\ImageRetriever(
            $this->context->link
        );

        foreach ($result->getProducts() as $rawProduct) {
            $populated_product = (new ProductAssembler($this->context))
                ->assembleProduct($rawProduct);

            if (version_compare(_PS_VERSION_, '9.0', '<=')) {
                $lazy_product = new RESTProductLazyArrayLegacy(
                    $settings,
                    $populated_product,
                    $this->context->language,
                    new \PrestaShop\PrestaShop\Adapter\Product\PriceFormatter(),
                    $retriever,
                    $this->context->getTranslator()
                );
            }else{
                $lazy_product = new RESTProductLazyArray(
                    $settings,
                    $populated_product,
                    $this->context->language,
                    new \PrestaShop\PrestaShop\Adapter\Product\PriceFormatter(),
                    $retriever,
                    $this->context->getTranslator()
                );
            }

            $products_for_template[] = $lazy_product->getProduct();
        }

        return array(
            'products' => $products_for_template,
            'allProductsLink' => Context::getContext()->link->getCategoryLink($this->getConfigFieldsValues()['HOME_FEATURED_CAT']),
        );
    }

    public function getConfigFieldsValues()
    {
        return array(
            'HOME_FEATURED_NBR' => Tools::getValue('HOME_FEATURED_NBR', (int) Configuration::get('HOME_FEATURED_NBR')),
            'HOME_FEATURED_CAT' => Tools::getValue('HOME_FEATURED_CAT', (int) Configuration::get('HOME_FEATURED_CAT')),
            'HOME_FEATURED_RANDOMIZE' => Tools::getValue('HOME_FEATURED_RANDOMIZE', (bool) Configuration::get('HOME_FEATURED_RANDOMIZE')),
        );
    }

    private function getFactory()
    {
        return new ProductPresenterFactory($this->context, new TaxConfiguration());
    }

    protected function getProductPresentationSettings()
    {
        return $this->getFactory()->getPresentationSettings();
    }
}
