<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Plugin;


class BeforeAddToCart
{
    /**
     * @var \Magento\Framework\Serialize\Serializer\Json
     */
    protected $_json;
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $request;
    protected $priceHelper;
    protected $filesystem;
    function __construct(
        \Magento\Framework\Serialize\Serializer\Json $json,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magento\Framework\Filesystem $filesystem

    )
    {
        $this->_json = $json;
        $this->request = $request;
        $this->priceHelper = $priceHelper;
        $this->filesystem = $filesystem;
    }

    /**
     * @param \Magento\Checkout\Model\Cart $subject
     * @param $productInfo
     * @param null $requestInfo
     */
    public function beforeAddProduct(\Magento\Checkout\Model\Cart $subject, $productInfo, $requestInfo = null)
    {
        /*$params = [];
        $oldTimeSave = 0;
        if(is_array($requestInfo)) {
            $params = $requestInfo;
        }
        elseif($requestInfo && $requestInfo->getData()) {
            $actionName =  $this->request->getActionName();
            $params = $requestInfo->getData();
        }
        $addOptions = [];
        $extraOptions = $this->getExtraOptions();
        if(count($extraOptions)) {
            foreach ($extraOptions as $extraOption) {
                $extraOption = (array)$extraOption;
                $key = $extraOption['name'];
                if(array_key_exists($key, $params)) {
//                    $price += $extraOption['price'];
                    $addOptions[] = array(
                        'label'=>__($extraOption['title']),
                        'value'=>$this->convertNumberToPrice($extraOption['price']),
                    );
                }
            }
        }
        if(count($addOptions)) {
            $bookOptions = $this->_json->serialize($addOptions);
            $productInfo->addCustomOption('additional_options', $bookOptions);
        }*/
        return [$productInfo, $requestInfo];

    }
    protected function getExtraOptions() {
        $mediaPath = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
        $extraOptions = @file_get_contents($mediaPath . 'previews/b2c_option_product.json');
        $extraOptions = (array)json_decode($extraOptions);
        return $extraOptions;
    }
    function convertNumberToPrice($price) {
        return $this->priceHelper->currency($price,true,false);
    }

}
