<?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\Controller\Product;

use Magento\Framework\App\Action;


class GetExtraPrice extends Action\Action
{
    protected $jsonFactory;
    protected $priceHelper;
    protected $productFactory;
    protected $resProductFactory;
    protected $optionCollection;
    protected $previewHelper;
    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\ResourceModel\ProductFactory $resProductFactory,
        \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $optionCollection,
        \Magetop\Advancepreview\Helper\Data $previewHelper
    )
    {
        parent::__construct($context);
        $this->jsonFactory = $jsonFactory;
        $this->priceHelper = $priceHelper;
        $this->productFactory = $productFactory;
        $this->resProductFactory = $resProductFactory;
        $this->optionCollection = $optionCollection;
        $this->previewHelper = $previewHelper;
    }

    public function execute()
    {
        $response = array(
            'status'=>'error',
            'message'=>__('Data not found'),
            'price'=>0,
            'final_price'=>0,
            'real_price'=>0,
        );
        $productId = $this->getRequest()->getParam('product',0);
        $discountPricePro = $this->getRequest()->getParam('final_price_discount_pro',0);
        $designType = $this->getRequest()->getParam('txt_custom_preview_design_type',0);
        $product = $this->productFactory->create();
        try {
//            $options = $this->?ge
            $params = $this->getRequest()->getParams();
            $this->resProductFactory->create()->load($product,$productId);
            $price = $discountPricePro; //$product->getPrice();
            $finaPrice = $discountPricePro; //$product->getFinalPrice();
            //$extraPrice = $this->getExtraPrice($params);
            // if($designType == 1) {
            //     $price += $extraPrice;
            //     $finaPrice += $extraPrice;
            // }

            $response['status'] = 'success';
            $response['message'] = 'Done';
            $response['price'] = $this->priceHelper->currency($price,true,false);
            $response['final_price'] = $this->priceHelper->currency($finaPrice,true,false);
            $response['real_price'] = $finaPrice;

        }
        catch (\Exception $exception) {
            $response['message'] = $exception->getMessage();
        }
        return $this->jsonFactory->create()->setData($response);
    }
    protected function getExtraPrice($params) {

        $price = 0;
        if(isset($params['txt_preview_type']) && $params['txt_preview_type'] == 1) {
            $price = isset($params['extra_option_all_price']) ? $params['extra_option_all_price'] : 0;
        }
        else {
            if(isset($params['preview_additional_options'])) {
                $price += $params['preview_additional_options'];
            }
            if(isset($params['pcto_process_price'])) {
                $price += $params['pcto_process_price'];
            }
            if(isset($params['pcto_shipping_price'])) {
                $price += $params['pcto_shipping_price'];
            }
        }


        return $price;
    }
}
