<?php
	
namespace MGS\ThemeSettings\Block\TopSale;

class Sale extends \Magento\Framework\View\Element\Template{

	protected $_productFactory;

	public function __construct(
		\Magento\Framework\View\Element\Template\Context $context,
		array $data = [],
		\Magento\Catalog\Model\ProductFactory $productFactory,
		\Magento\Store\Model\StoreManagerInterface $storeManager,
		\Magento\Review\Model\ReviewFactory $reviewFactory
	){
		$this->_storeManager = $storeManager;
		$this->_productFactory = $productFactory;
		$this->_reviewFactory = $reviewFactory;
		return parent::__construct($context, $data);
	}
	
	public function getRatingSummary($product)
	{
		$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
		$ratingSummary = $product->getRatingSummary()->getRatingSummary();
		return $ratingSummary;
	}
	
	public function getNumberShow(){
		return $this->getData('number_show');
	}
	
	public function getItemPage() {
		return $this->getData('item_page');
	}
	
	public function getUseSlider() {
		return $this->getData('use_slider');
	}
	
	public function getAttribute() {
		return $this->getData('attribute');
	}

	public function getProductTopSale(){

		$model = $this->_productFactory->create();

		$list_product = $model->getCollection()
					->addAttributeToSelect("*")
    				->addFieldToFilter($this->getAttribute(), 1)
					->setPageSize($this->getNumberShow())
    				->load();

		return $list_product;


	}

	public function getMediaUrl(){
		$mediaUrl = $this ->_storeManager-> getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA );

		return $mediaUrl;
	}
	
	public function getProductPrice(\Magento\Catalog\Model\Product $product)
    {
        $priceRender = $this->getPriceRender();

        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
                $product,
                [
                    'include_container' => true,
                    'display_minimal_price' => true,
                    'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
                    'list_category_page' => true
                ]
            );
        }

        return $price;
    }
	protected function getPriceRender()
    {
        return $this->getLayout()->getBlock('product.price.render.default')
            ->setData('is_product_list', true);
    }
}