<?php
/**
 * Webkul Software.
 *
 * @category   Webkul
 * @package    Webkul_Agorae
 * @author     Webkul Software Private Limited
 * @copyright  Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license    https://store.webkul.com/license.html
 */
namespace Webkul\Agorae\Block;

/*
 * Webkul Agorae Sellerblock Block
 */
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Session;
use Magento\Catalog\Model\Product;

class Sellerblock extends \Magento\Framework\View\Element\Template
{
    /**
     * @var Product
     */
    protected $_product = null;

    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $customer;

    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $session;

    /**
     * Core registry.
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @var \Webkul\Marketplace\Helper\Data
     */
    protected $_marketplaceDataHelper;

    /**
     * @var \Webkul\Agorae\Helper\Data
     */
    protected $_helper;

    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $productFactory;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Registry                      $registry
     * @param Customer                                         $customer
     * @param \Magento\Customer\Model\Session                  $session
     * @param \Webkul\Agorae\Helper\Data                       $helper
     * @param \Magento\Framework\Module\Manager                $moduleManager
     * @param \Magento\Catalog\Model\ProductFactory            $productFactory
     * @param array                                            $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        Customer $customer,
        \Magento\Customer\Model\Session $session,
        \Webkul\Agorae\Helper\Data $helper,
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        array $data = []
    ) {
        $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->Customer = $customer;
        $this->Session = $session;
        $this->_coreRegistry = $registry;
        $this->_helper = $helper;
        $this->moduleManager = $moduleManager;
        $this->productFactory = $productFactory;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->_marketplaceDataHelper = $this->_objectManager->create(\Webkul\Marketplace\Helper\Data::class);
        }
        parent::__construct($context, $data);
    }

    /**
     * Get Product
     *
     * @return Product
     */
    public function getProduct()
    {
        if (!$this->_product) {
            $this->_product = $this->_coreRegistry->registry('product');
        }
        return $this->_product;
    }

    /**
     * GetSellerDetailsByProductId used to get Seller Details from Product Id
     *
     * @param  int $productId [contains product Id]
     * @return boolean | object
     */
    public function getSellerDetailsByProductId($productId)
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $sellerId = $this->_helper->getSellerIdByProductId($productId);
            if ($sellerId) {
                $sellerData = $this->_marketplaceDataHelper->getSellerDataBySellerId($sellerId);
                if ($sellerData->getSize()) {
                    return $sellerData->getFirstItem();
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

    /**
     * IsSeller used to check is a customer is seller or not
     *
     * @param  int  $productId [product id]
     * @return boolean
     */
    public function isSeller($productId)
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $isSeller = false;
            $sellerId = $this->_helper->getSellerIdByProductId($productId);
            if ($sellerId) {
                $sellerData = $this->_marketplaceDataHelper->getSellerDataBySellerId($sellerId);
                if ($sellerData->getSize()) {
                    foreach ($sellerData as $value) {
                        if ($value->getIsSeller()) {
                            $isSeller = true;
                        } else {
                            $isSeller = false;
                        }
                    }
                }
            }
            return $isSeller;
        }
    }

    /**
     * GetSellerProducts used to get seller products from seller Id
     *
     * @param  int $sellerId [seller Id]
     * @return object
     */
    public function getSellerProducts($sellerId)
    {
        $sellerProductcollection = $this->_helper->getSellerProductDataBySellerId($sellerId);
        $querydata = $sellerProductcollection
        ->addFieldToFilter('status', ['neq' => \Webkul\Agorae\Model\Mpfavouriteseller::STATUS_DISABLED])
        ->addFieldToSelect('mageproduct_id')
        ->setOrder('mageproduct_id');
        $collection = $this->productFactory->create()->getCollection();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('entity_id', ['in' => $querydata->getData()]);
        $collection->addAttributeToFilter('visibility', ['in' => [4]]);
        $collection->addStoreFilter();
        $collectionConfigurable = $this->productFactory->create()->getCollection()
        ->addAttributeToFilter('type_id', 'configurable')
        ->addAttributeToFilter('entity_id', ['in' => $querydata->getData()]);
        $outOfStockConfis = [];
        foreach ($collectionConfigurable as $_configurableproduct) {
            $product = $this->productFactory->create()->load($_configurableproduct->getId());
            if (!$product->getData('is_salable')) {
                $outOfStockConfis[] = $product->getId();
            }
        }
        if (count($outOfStockConfis)) {
            $collection->addAttributeToFilter('entity_id', ['nin' => $outOfStockConfis]);
        }
        $collectionBundle = $this->productFactory->create()
            ->getCollection()
            ->addAttributeToFilter('type_id', 'bundle')
            ->addAttributeToFilter('entity_id', ['in' => $querydata->getData()]);
        $outOfStockConfis = [];
        foreach ($collectionBundle as $_bundleproduct) {
            $product = $this->productFactory->create()->load($_bundleproduct->getId());
            if (!$product->getData('is_salable')) {
                $outOfStockConfis[] = $product->getId();
            }
        }
        if (count($outOfStockConfis)) {
            $collection->addAttributeToFilter('entity_id', ['nin' => $outOfStockConfis]);
        }
        $collectionGrouped = $this->productFactory->create()
            ->getCollection()
            ->addAttributeToFilter('type_id', 'grouped')
            ->addAttributeToFilter('entity_id', ['in' => $querydata->getData()]);
        $outOfStockConfis = [];
        foreach ($collectionGrouped as $_groupedproduct) {
            $product = $this->productFactory->create()->load($_groupedproduct->getId());
            if (!$product->getData('is_salable')) {
                $outOfStockConfis[] = $product->getId();
            }
        }
        if (count($outOfStockConfis)) {
            $collection->addAttributeToFilter(
                'entity_id',
                ['nin' => $outOfStockConfis]
            );
        }
        return $collection;
    }
}
