<?php
/**
 * Magetop
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Plugin\Catalog\Block\Product;

class ListProduct
{
    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    protected $quoteHelper;

    /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    private $_productInfo;

    /**
     *  ListProduct
     *
     * @param \Magetop\Quotesystem\Helper\Data                   $quoteHelper
     * @param \Magento\Catalog\Api\ProductRepositoryInterface   $productRepository
     */
    public function __construct(
        \Magetop\Quotesystem\Helper\Data $quoteHelper,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->quoteHelper = $quoteHelper;
        $this->repository = $productRepository;
    }
    
    /**
     * Change Product Price
     *
     * @param \Magento\Catalog\Block\Product\ListProduct        $subject
     * @param \Magento\Framework\Controller\Result\Redirect     $result
     */
    public function afterGetProductPrice(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        $result
    ) {
        $showPrice = (int)$this->quoteHelper->getConfigShowPrice();
        $modStatus = $this->quoteHelper->getModuleStatus();
        $id = $this->_productInfo->getEntityId();
        $quoteStatus = $this->repository->getById($id)->getQuoteStatus();
        if ($modStatus && ($quoteStatus == 1) && !$showPrice) {
            return $this->quoteHelper->removePriceInfo($result);
        }
        return $result;
    }

    /**
     * BeforeGetProductPrice plugin to assign the product model to a variable
     *
     * @param  \Magento\Catalog\Block\Product\ListProduct   $listProduct
     * @param  \Magento\Catalog\Model\Product               $product
     */
    public function beforeGetProductPrice(
        \Magento\Catalog\Block\Product\ListProduct $listProduct,
        \Magento\Catalog\Model\Product $product
    ) {
        $this->_productInfo = $product ;
    }
}
