<?php

namespace Solwin\Cpanel\Block;

class BestList extends \Magento\Catalog\Block\Product\AbstractProduct {

    /**
     * Product collection model
     *
     * @var Magento\Catalog\Model\Resource\Product\Collection
     */
    protected $_collection;
    protected $_soldcollection;

    /**
     * Product collection model
     *
     * @var Magento\Catalog\Model\Resource\Product\Collection
     */
    protected $_productCollection;

    /**
     * @var \Magento\Framework\Url\Helper\Data
     */
    protected $urlHelper;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\Collection $collection, 
        \Magento\Reports\Model\ResourceModel\Product\Sold\Collection $soldcollection, 
        \Magento\Framework\Url\Helper\Data $urlHelper,
        array $data = []
    ) {
        $this->_soldcollection = $soldcollection;
        $this->_collection = $collection;
        $this->urlHelper = $urlHelper;
        parent::__construct($context, $data);       
        
        if($this->getExtesionStatus()) {
            $this->pageConfig->getTitle()->set(__($this->getPageTitle()));
        }

        if ($this->_storeManager->hasSingleStore()) {
            $this->setStoreId($this->_storeManager->getStore(true)->getId());
        }
    }

    /**
     * Get product collection
     */
    protected function getProducts() {
        $itemsCollection = $this->_soldcollection
                ->addAttributeToSelect('*')
                ->addAttributeToSelect('id')
                ->addOrderedQty()
                ->setOrder('ordered_qty', 'desc');
        $itemsCollection->getSelect()->columns('item_id');

        $prod = array();
        if (sizeof($itemsCollection) > 0) {
            foreach ($itemsCollection as $item) {
                $prod[] = $item->getItemId();
            }
        }

        $collection = $this->_collection
                ->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToFilter('is_saleable', 1, 'left');

        $collection->getSelect()->where('e.entity_id IN(?)', $prod)->order('rand()');

        // Set Pagination Toolbar for list page
        $pager = $this->getLayout()->createBlock('Magento\Theme\Block\Html\Pager', 'catalogpages.grid.record.pager')->setCollection($collection);
        $this->setChild('pager', $pager);

        $this->_productCollection = $collection;
        return $this->_productCollection;
    }

    public function getLoadedProductCollection() {
        return $this->getProducts();
    }

    public function getToolbarHtml() {
        return $this->getChildHtml('pager');
    }

    public function getMode() {
        return 'grid';
    }

    /**
     * Get image helper
     */
    public function getImageHelper() {
        return $this->_imageHelper;
    }
    
    public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
    {
        $url = $this->getAddToCartUrl($product);
        return [
            'action' => $url,
            'data' => [
                'product' => $product->getEntityId(),
                \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED =>
                    $this->urlHelper->getEncodedUrl($url),
            ]
        ];
    }
}
