<?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\Product;

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

    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    private $_productInfo;

    /**
     * @param \Magetop\Quotesystem\Helper\Data $quoteHelper
     */
    public function __construct(
        \Magetop\Quotesystem\Helper\Data $quoteHelper
    ) {
        $this->quoteHelper = $quoteHelper;
    }

    /**
     * Change product data
     *
     * @param \Magetop\Quotesystem\Block\Product\ListProduct     $subject
     * @param \Magento\Framework\Controller\Result\Redirect     $result
     */
    public function afterGetProductPrice(
        \Magetop\Quotesystem\Block\Product\ListProduct $subject,
        $result
    ) {
        $showPrice = (int)$this->quoteHelper->getConfigShowPrice();
        $modStatus = $this->quoteHelper->getModuleStatus();
        $quoteStatus = $this->_productInfo->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
     * @return \Magento\Catalog\Model\Product
     */
    public function beforeGetProductPrice(
        \Magento\Catalog\Block\Product\ListProduct $listProduct,
        \Magento\Catalog\Model\Product $product
    ) {
        $this->_productInfo = $product ;
    }
}
