<?php

namespace Magetop\Agency\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class CustomPrice implements ObserverInterface
{
    /**
     * Request instance.
     *
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $_request;

    /**
     * @var \Magetop\Agency\Helper\Data
     */
    protected $_assignHelper;

    /**
     * @var \Magento\Checkout\Model\CartFactory
     */
    protected $_cart;

    /**
     * @param RequestInterface $request
     * @param \Magetop\Agency\Helper\Data $helper
     * @param \Magento\Checkout\Model\CartFactory $cartFactory
     */
    public function __construct(
        RequestInterface $request,
        \Magetop\Agency\Helper\Data $helper,
        \Magento\Checkout\Model\CartFactory $cartFactory
    )
    {
        $this->_request = $request;
        $this->_assignHelper = $helper;
        $this->_cart = $cartFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $helper = $this->_assignHelper;
        $item = $observer->getEvent()->getData('quote_item');
        $info = $observer->getEvent()->getData('info');
        if (is_object($info)) {
            $info = $info->getData();
        }
        $data = $this->_request->getParams();
        $product = $observer->getEvent()->getData('product');
        $assignId = 0;
        $childAssignId = 0;
        $productId = $product->getId();
        if (is_array($info)) {
            if (array_key_exists("assignproduct_id", $info)) {
                $assignId = $info['assignproduct_id'];
            }
            if (array_key_exists("associate_id", $info)) {
                $childAssignId = $info['associate_id'];
            }
        }
        if ($product->getTypeId() == "configurable") {
            if (!$helper->isConfigQtyAllowed($info, $product)) {
                $meg = __('Requested quantity not available from agency.');
                throw new \Magento\Framework\Exception\LocalizedException( $meg);
            }
        }

        $itemId = (int) $item->getId();
        if ($itemId > 0) {
            $cartModel = $this->_cart->create();
            $quote = $cartModel->getQuote();
            $requestedItemId = $helper->getRequestedItemId($assignId, $productId, $quote->getId());
            foreach ($quote->getAllItems() as $item) {
                $quoteItemId = $item->getId();
            }
        } else {
            if ($assignId > 0) {
                if ($helper->isEnabled($assignId)) {
                    if ($childAssignId > 0) {
                        $childAssignId = $childAssignId;
                        $price = $helper->getAssocitePrice($assignId, $childAssignId);
                    } else {
                        $price = $helper->getAssignProductPrice($assignId);
                    }
                    $price = $helper->getFinalPrice($price);
                    $item->setCustomPrice($price);
                    $item->setOriginalCustomPrice($price);
                    $item->getProduct()->setIsSuperMode(true);
                } else {
                    $meg = __('Product is currently not available from agency.');
                    throw new \Magento\Framework\Exception\LocalizedException($meg);
                }
            }
        }
    }
}
