<?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;

use Magento\Customer\Model\Customer;
use Webkul\Agorae\Model\MpfavouritesellerRepository;

/**
 * Webkul Agorae AddFavouriteSeller Block
 */
class AddFavouriteSeller extends \Magento\Framework\View\Element\Template
{

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

    /**
     * @var Webkul\Marketplace\helper\Data
     */
    protected $_dataHelperMarketplace;

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

    /**
     * @var Webkul\Marketplace\Model\Seller
     */
    protected $_mpSellerModel;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $_registry;

    /**
     * @var [type]
     */
    protected $_urlinterface;

    /**
     * @var \Magento\Framework\App\ObjectManager::getInstance();
     */
    protected $objectManager;

    /**
     * @var Webkul\Agorae\Model\MpfavouritesellerRepository
     */
    protected $_mpFavouriteSellerRepository;

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

    /**
     * @param Customer                                         $customerModel
     * @param \Magento\Framework\Registry                      $registry
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param MpfavouritesellerRepository                      $mpFavouriteSellerRepository
     * @param \Magento\Framework\Module\Manager                $moduleManager
     * @param array                                            $data
     */
    public function __construct(
        Customer $customerModel,
        \Webkul\Agorae\Helper\Data $helper,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\View\Element\Template\Context $context,
        MpfavouritesellerRepository $mpFavouriteSellerRepository,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {
        $this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->_registry = $registry;
        $this->helper = $helper;
        $this->_customerModel = $customerModel;
        $this->_urlinterface = $context->getUrlBuilder();
        $this->_mpFavouriteSellerRepository = $mpFavouriteSellerRepository;
        $this->moduleManager = $moduleManager;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->_mpSellerModel = $this->objectManager->create(\Webkul\Marketplace\Model\Seller::class);
            $this->_dataHelperMarketplace = $this->objectManager->create(\Webkul\Marketplace\Helper\Data::class);
        }
        parent::__construct($context, $data);
    }

    /**
     * Get Seller id by shop url
     *
     * @return int
     */
    public function getSellerIdbyShopUrlOnProfile()
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $shopUrl = $this->_dataHelperMarketplace->getProfileUrl();
            if (!$shopUrl) {
                $shopUrl = $this->getRequest()->getParam('shop');
            }
            $sellerCollection = $this->_mpSellerModel->getCollection()
            ->addFieldToFilter(
                'shop_url',
                $shopUrl
            );
            foreach ($sellerCollection as $seller) {
                $sellerId = $seller->getSellerId();
            }
            return $sellerId;
        }
    }

    /**
     * Get Seller id by shop url
     *
     * @return int
     */
    public function getSellerIdbyShopUrlOnCollection()
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $shopUrl = $this->_dataHelperMarketplace->getCollectionUrl();
            if (!$shopUrl) {
                $shopUrl = $this->getRequest()->getParam('shop');
            }
            $sellerCollection = $this->_mpSellerModel->getCollection()
            ->addFieldToFilter(
                'shop_url',
                $shopUrl
            );
            foreach ($sellerCollection as $seller) {
                $sellerId = $seller->getSellerId();
            }
            return $sellerId;
        }
    }
    
    /**
     * Get Seller id
     *
     * @return int
     */
    public function getSellerIdOnProductView()
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $_product = $this->_registry->registry('current_product');
            $mpProductCollection = $this->_dataHelperMarketplace
                ->getSellerProductDataByProductId(
                    $_product->getId()
                );
            foreach ($mpProductCollection as $productMp) {
                $sellerId = $productMp->getSellerId();
            }
            return $sellerId;
        }
    }

    /**
     * Get link url
     *
     * @return string
     */
    public function getFavouriteSellerLink()
    {
        $sellerId = $this->getSellerId();
        $url = $this->getUrl(
            'agorae/favouriteseller/addfavouriteseller',
            [
                'seller_id'=>$sellerId
            ]
        );
        return $url;
    }

    /**
     * Is seller follwed
     *
     * @return boolean
     */
    public function isSellerFollowed()
    {
        try {
            if ($this->helper->checkCustomerLoggedIn()) {
                $customer = $this->helper->getLoggedInCustomer();
                $collection = $this->_mpFavouriteSellerRepository
                ->getsellersCollectionByCustomerId($customer->getId())
                ->addFieldToFilter('seller_id', $this->getSellerId());
                if ($collection->getSize()) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * Get seller id
     *
     * @return int
     */
    public function getSellerId()
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $sellerId = '';
            $collectionArray = [];
            $profileArray =[];
            $currentUrl = $this->_urlinterface->getCurrentUrl();
            $targetUrl = $this->_dataHelperMarketplace->getTargetUrlPath();
            if ($targetUrl) {
                $collectionArray = explode('/collection/', $targetUrl);
                $profileArray = explode('/profile/', $targetUrl);
            } else {
                $collectionArray = explode('/collection/', $currentUrl);
                $profileArray = explode('/profile/', $currentUrl);
            }
            
            if (count($collectionArray) > 1) {
                $sellerId = $this->getSellerIdbyShopUrlOnCollection();
            }
            if (count($profileArray) > 1) {
                $sellerId = $this->getSellerIdbyShopUrlOnProfile();
            }
            if ($sellerId == '') {
                $sellerId =  $this->getSellerIdOnProductView();
            }
    
            return $sellerId;
        }
    }
}
