<?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\Review\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\App\Request\Http;
use \Magento\Framework\Registry;
use Aitoc\PreOrders\Api\Data\Source\RegistryNameInterface;
use Magento\Catalog\Block\Product\ListProduct as MagentoListProduct;

/**
 * Class ReviewRenderer
 */
class ReviewRenderer
{
    const PRODUCT_LIST_PATH = 'catalog/category/view';
    const SEARCH_RESULT_PATH = 'catalogsearch/result/index';
    const COMPARE_LIST_PATH = 'catalog/product_compare/index';
    /**
     * @var PreOrderHelper
     */
    private $preOrderHelper;

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

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

    /**
     * @var Http
     */
    private $request;

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

    /**
     * @param MagentoListProduct $subject
     * @param $product
     * @param bool $templateType
     * @param bool $displayIfNoReviews
     */
    public function beforeGetReviewsSummaryHtml($subject, $product, $templateType = false, $displayIfNoReviews = false)
    {
        if ($this->registry->registry(RegistryNameInterface::CURRENT_PRODUCT_LIST_PRODUCT_ID)) {
            $this->registry->unregister(RegistryNameInterface::CURRENT_PRODUCT_LIST_PRODUCT_ID);
        }
        $this->registry->register(RegistryNameInterface::CURRENT_PRODUCT_LIST_PRODUCT_ID, $product->getId());
    }

    /**
     * @param MagentoListProduct $subject
     * @param string $html
     * @return string
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function afterGetReviewsSummaryHtml($subject, $html)
    {
        if ($this->canBeApplied()) {
            if ($productId = $this->registry->registry(RegistryNameInterface::CURRENT_PRODUCT_LIST_PRODUCT_ID)) {
                $message = '';
                if ($this->preOrderHelper->isProductPreOrder($productId)) {
                    $message = $this->preOrderHelper->getPreOrderStatusLabelByProductId($productId);
                }
                $html = '<span style="font-size: 0.85em;font-weight: bold;" class="preorder_note">' . $message . '</span>' . $html;
            }
        }
        return $html;
    }

    /**
     * @return int
     */
    private function getCurrentScope()
    {
        $scope = 0;
        $request = $this->request;
        $requestPath = $request->getRouteName() . '/' . $request->getControllerName() . '/' . $request->getActionName();
        switch ($requestPath) {
            case self::PRODUCT_LIST_PATH:
                $scope = PreOrderNoteScopeSourceInterface::CATEGORY_PAGE_SCOPE;
                break;
            case self::SEARCH_RESULT_PATH:
                $scope = PreOrderNoteScopeSourceInterface::SEARCH_RESULT_SCOPE;
                break;
            case self::COMPARE_LIST_PATH:
                $scope = PreOrderNoteScopeSourceInterface::COMPARE_LIST_SCOPE;
                break;
        }
        return $scope;
    }

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