<?php
/**
 * Magetop Store
 *
 * @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\Compare;

use Magento\Catalog\Block\Product\Compare\ListCompare as Compare;

class ListCompare
{
    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    private $_quotesystemHelper;

    /**
     * Initialize dependencies.
     *
     * @param \Magetop\Quotesystem\Helper\Data $helper
     */
    public function __construct(
        \Magetop\Quotesystem\Helper\Data $helper
    ) {
        $this->_quotesystemHelper = $helper;
    }

    /**
     * Change redirect after login
     *
     * @param Compare                           $subject
     * @param \Closure                          $proceed
     * @param \Magento\Catalog\Model\Product    $product
     * @param int                               $idSuffix
     */
    public function aroundGetProductPrice(
        Compare $subject,
        \Closure $proceed,
        \Magento\Catalog\Model\Product $product,
        $idSuffix
    ) {
        $result = $proceed($product, $idSuffix);
        $isQuoted = $product->getQuoteStatus();
        if ($isQuoted == 1) {
            $_productCollection = $this->_quotesystemHelper->getLoadedProduct();
            $quotedProductsData = $this->_quotesystemHelper->getQuotedProductInfo($_productCollection);
            $buttonStatus = $quotedProductsData[$product->getData('entity_id')];

            if ($buttonStatus['status']===1) {
                $button = "<div class='actions-primary quote_button'>
                <button type='button' title='Add to Quote' 
                class='quotesystem_cat_add action toquote primary' 
                data-product-id='" . $product->getData('entity_id') . "' 
                data-qty='" . $buttonStatus['min_qty'] . "' 
                data-url='" . $buttonStatus['url'] . "'><span>Add to Quote</span></button></a></div>";
            } else {
                $button = "<div class='actions-primary quote_button'>
                <button type='button' title='Add to Quote' class='quotesystem_redirect action toquote primary' 
                data-url='" . $buttonStatus['url'] . "?options=quote'><span>Add to Quote</span></button></div>";
            }

            $showPrice = (int)$this->_quotesystemHelper->getConfigShowPrice();

            if (!$showPrice) {
                return $button;
            }
            $result = $result.$button;
        }
        return $result;
    }
}
