<?php
/**
 * Block for add quote on a product on product page
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Block;

use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Session;
use Magento\Catalog\Model\Product;
use Magetop\Quotesystem\Helper\Data;

class Quoteproduct extends \Magento\Framework\View\Element\Template
{
    /**
     * @var Product
     */
    protected $_product = null;
    
    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $_customer;
    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $_session;
    /**
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;
    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    protected $_helper;

    /**
     * @var \Magento\Directory\Model\Currency
     */
    protected $_currency;

    /**
     * @var StoreManager
     */
    protected $_storeManager;

    /**
     * @var Magento\CatalogInventory\Model\Stock\StockItemRepository
     */
    protected $_stockItemRepository;

    /**
     * @param \Magento\Framework\View\Element\Template\Context          $context
     * @param \Magento\Framework\Registry                               $registry
     * @param Customer                                                  $customer
     * @param \Magento\Customer\Model\Session                           $session
     * @param Data                                                      $helper
     * @param \Magento\Directory\Model\Currency                         $currency
     * @param \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
     * @param \Magento\CatalogInventory\Model\Stock\Item                $stockFactory
     * @param \Magento\Framework\Json\Helper\Data                       $jsonHelper
     * @param array                                                     $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        Customer $customer,
        \Magento\Customer\Model\Session $session,
        Data $helper,
        \Magento\Directory\Model\Currency $currency,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        \Magento\CatalogInventory\Model\Stock\Item $stockFactory,
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        array $data = []
    ) {
        $this->_storeManager = $context->getStoreManager();
        $this->_currency = $currency;
        $this->_customer = $customer;
        $this->_session = $session;
        $this->_coreRegistry = $registry;
        $this->_helper = $helper;
        $this->_stockItemRepository = $stockItemRepository;
        $this->_stockFactory = $stockFactory;
        $this->jsonHelper = $jsonHelper;
        parent::__construct($context, $data);
    }

    /**
     * Get Product which is saved in registry
     *
     * @return Product
     */
    public function getProduct()
    {
        if (!$this->_product) {
            $this->_product = $this->_coreRegistry->registry('product');
        }
        return $this->_product->setStore($this->_helper->getStore());
    }

    /**
     * Use to get current url.
     */
    public function getCurrentUrl()
    {
        // Give the current url of recently viewed page
        return $this->_urlBuilder->getCurrentUrl();
    }
    
    /**
     * Get request is secure
     */
    public function getIsSecure()
    {
        return $this->getRequest()->isSecure();
    }

    /**
     * Get Current currency
     */
    public function getCurrentCurrency()
    {
        $code = $this->_storeManager->getStore()->getCurrentCurrencyCode();
        return $this->_currency->load($code)->getCurrencySymbol();
    }

    /**
     * Get stock product id
     */
    public function stockProductId()
    {
        $productId = $this->getRequest()->getParam('product_id');
        if (!$productId) {
            $productId = $this->getRequest()->getParam('id');
        }
        $stockProducts = $this->_stockFactory->load($productId, 'product_id');
        return $stockProducts;
    }

    /**
     * Get Product item id
     *
     * @param object $stockProducts
     */
    public function getProductItemId($stockProducts)
    {
        $stockItemId = $stockProducts->getId();
        return $stockItemId;
    }

    /**
     * Get product Qty
     */
    public function getProductQty()
    {
        $stockProductsId = $this->stockProductId();
        return $this->_stockItemRepository->get($this->getProductItemId($stockProductsId))->getQty();
    }

    /**
     * Check is in stock
     */
    public function isInStock()
    {
        $stockProducts = $this->stockProductId();
        $stockItemId = $stockProducts->getIsInStock();
        return $stockItemId;
    }

    /**
     * Get Helper
     *
     * @return Magetop\QuoteSystem\Helper\Data
     */
    public function getHelper()
    {
        return $this->_helper;
    }

    /**
     * Get Json Helper
     *
     * @return Magento\Framework\Json\Helper\Data
     */
    public function getJsonHelper()
    {
        return $this->jsonHelper;
    }

    /**
     * Get configured allowed file types
     */
    public function getAllowedFileTypes()
    {
        return $this->_helper->getAllowedFileTypes();
    }
}
