<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_QuickCart
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\QuickCart\Plugin\Product\Wishlist;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Wishlist\Controller\AbstractIndex;
use Magento\Wishlist\Controller\Index\Add as AddWishlist;
use Mageplaza\QuickCart\Helper\QuickViewData as HelperData;

/**
 * Class Add
 * @package Mageplaza\QuickCart\Plugin\Product\Wishlist
 */
class Add
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var HelperData
     */
    protected $_helperData;

    /**
     * @var ManagerInterface
     */
    protected $messageManager;

    /**
     * @var StoreManagerInterface
     */
    protected $storeManager;

    /**
     * Add constructor.
     * @param ProductRepositoryInterface $productRepository
     * @param ManagerInterface $messageManager
     * @param HelperData $helperData
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        ProductRepositoryInterface $productRepository,
        ManagerInterface $messageManager,
        HelperData $helperData,
        StoreManagerInterface $storeManager
    ) {
        $this->productRepository = $productRepository;
        $this->_helperData       = $helperData;
        $this->messageManager    = $messageManager;
        $this->storeManager      = $storeManager;
    }

    /**
     * @param AbstractIndex $subject
     * @param $result
     *
     * @return array|Http
     * @throws NoSuchEntityException
     */
    public function afterExecute(AbstractIndex $subject, $result)
    {
        if (!$this->_helperData->isEnabled()) {
            return $result;
        }
        $request     = $subject->getRequest();
        $message     = $this->messageManager->getMessages();
        $messageData = $message->getMessageByIdentifier('addProductSuccessMessage')->getData();

        if (isset($messageData['referer']) && str_contains($messageData['referer'], 'mpquickview')) {
            $messageData['referer'] = str_replace('?mpquickview=1', '', $messageData['referer']);
            $message->clear();
            $this->messageManager->addComplexSuccessMessage(
                'addProductSuccessMessage',
                $messageData
            );
        }

        if (!$request->isAjax()) {
            return $result;
        }

        if ($subject instanceof AddWishlist) {
            $productId   = (int)$request->getParam('product');
            $product     = $this->productRepository->getById($productId);
            $productName = $product->getName();

            $result = [
                'success' => true,
                'message' => __(
                    '%1 has been added to your Wish List.Click <a href="%2">here</a> to continue shopping.',
                    $productName,
                    $messageData['referer']
                )
            ];
        }

        return $subject->getResponse()->representJson(HelperData::jsonEncode($result));
    }
}
