<?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\Observer\Frontend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ChangePrice implements ObserverInterface
{
    protected $productFactory;
    protected $resProductFactory;
    protected $optionCollection;
    protected $priceHelper;
    protected $filesystem;
    function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\ResourceModel\ProductFactory $resProductFactory,
        \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $optionCollection,
        \Magento\Framework\Filesystem $filesystem
    )
    {
        $this->productFactory = $productFactory;
        $this->resProductFactory = $resProductFactory;
        $this->optionCollection = $optionCollection;
        $this->filesystem = $filesystem;
    }
    public function execute(Observer $observer)
    {
        /** @var \Magento\Quote\Model\Quote\Item $quoteItem*/
        $quoteItem = $observer->getEvent()->getQuoteItem();
        $customOptions = $quoteItem->getProduct()->getTypeInstance(true)->getOrderOptions($quoteItem->getProduct());
//        $product = $quoteItem->getProduct();

        $customOptionsRequest = $customOptions['info_buyRequest'];
        $extraPrice = $this->getExtraPrice($customOptionsRequest);
        if($extraPrice > 0) {
            $finalPrice = $quoteItem->getProduct()->getFinalPrice() + $extraPrice;
            $quoteItem->setCustomPrice($finalPrice);
            $quoteItem->setOriginalCustomPrice($finalPrice);
            $quoteItem->getProduct()->setIsSuperMode(true);
        }
        return $this;

    }
    protected function getExtraPrice($params) {
        /** @var \Magento\Framework\Filesystem $filesystem */

        $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);
        $price = 0;
        foreach ($extraOptions as $extraOption) {
            $extraOption = (array)$extraOption;
            $key = $extraOption['name'];
            if(array_key_exists($key, $params)) {
                $price += $extraOption['price'];
            }
        }
        return $price;
    }

}
