<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Plugin\Catalog\Block\Product;

use Aitoc\PreOrders\Helper\PreOrder as PreOrderHelper;
use Aitoc\PreOrders\Helper\Settings as SettingsHelper;
use Aitoc\PreOrders\Api\Data\Source\PreOrderNoteScope as PreOrderNoteScopeSourceInterface;
use \Magento\Framework\Registry;
use Aitoc\PreOrders\Api\Data\Source\RegistryNameInterface;
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection as CatalogSearchFulltextCollection;
use Magento\Catalog\Block\Product\ListProduct as MagentoListProduct;

/**
 * Class ListProduct
 */
class ListProduct
{
    const PRODUCT_LIST_BLOCK_NAME = "category.products.list";

    /**
     * @var PreOrderHelper
     */
    private $preOrderHelper;

    /**
     * @var SettingsHelper
     */
    private $settingsHelper;

    /**
     * @var Registry
     */
    private $registry;

    /**
     * @param PreOrderHelper $preOrderHelper
     * @param Registry $registry
     * @param SettingsHelper $settingsHelper
     */
    public function __construct(
        PreOrderHelper $preOrderHelper,
        Registry $registry,
        SettingsHelper $settingsHelper
    ) {
        $this->preOrderHelper = $preOrderHelper;
        $this->settingsHelper = $settingsHelper;
        $this->registry = $registry;
    }

    /**
     * @param MagentoListProduct $subject
     * @param CatalogSearchFulltextCollection $result
     * @return mixed
     */
    public function afterGetLoadedProductCollection(
        MagentoListProduct $subject,
        $result
    ) {
        $productIds = $result->getColumnValues($result->getIdFieldName());
        if ($this->registry->registry(RegistryNameInterface::CURRENT_PRODUCT_IDS)) {
            $this->registry->unregister(RegistryNameInterface::CURRENT_PRODUCT_IDS);
        }
        $this->registry->register(RegistryNameInterface::CURRENT_PRODUCT_IDS, $productIds);
        return $result;
    }

    /**
     * @param MagentoListProduct $subject
     * @param string $result
     * @return string
     */
    public function afterToHtml(
        MagentoListProduct $subject,
        $result
    ) {
        switch ($subject->getNameInLayout()) {
            case self::PRODUCT_LIST_BLOCK_NAME:
                if ($this->canBeApplied()) {
                    $result = $this->changeButtonText($result);
                }
                break;
        }
        return $result;
    }

    /**
     * @return bool
     */
    private function canBeApplied()
    {
        $preOrderNoteScope = $this->settingsHelper->getPreOrderNoteScopeSettings();
        return in_array(PreOrderNoteScopeSourceInterface::CATEGORY_PAGE_SCOPE, $preOrderNoteScope);
    }

    /**
     * @param string $html
     * @return string
     */
    public function changeButtonText($html)
    {
        $productIds = $this->registry->registry(RegistryNameInterface::CURRENT_PRODUCT_IDS);
        $addToCartLabel = __('Add to Cart');
        $preOrderLabel = __('Pre-Order');
        $tempLabel = __('To Cart Temp');
        foreach ($productIds as $productId) {
            $isPreOrder = $this->preOrderHelper->isProductPreOrder($productId);
            $buttonTitle = '/' . preg_quote(">" . $addToCartLabel . "<", '/') . '/';
            if ($isPreOrder) {
                $preOrderButtonTitle = ">" . $preOrderLabel . "<";
            } else {
                $preOrderButtonTitle = ">" . $tempLabel . "<";
            }

            $html = preg_replace($buttonTitle, $preOrderButtonTitle, $html, 1);
        }
        $html = str_replace($tempLabel, $addToCartLabel, $html);
        return $html;
    }
}
