<?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 Webkul\Agorae\Model\MpfavouritesellerRepository;

/**
 * Webkul Agorae FavouriteSellerList Block
 */
class FavouriteSellerList extends \Magento\Framework\View\Element\Template
{
    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

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

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

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

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

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

    /**
     * @var \Magento\Framework\View\Element\Template\Context
     */
    protected $_urlinterface;

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

    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
     */
    protected $eavAttribute;

    /**
     * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
     */
    protected $customerFactory;

    /**
     * @var \Webkul\Marketplace\Model\ResourceModel\Seller\Collection
     */
    protected $mpSeller;

    /**
     * @param MpfavouritesellerRepository                                      $mpFavouriteSellerRepository
     * @param \Magento\Customer\Model\Session                                  $customerSession
     * @param \Magento\Customer\Model\Customer                                 $customerModel
     * @param \Magento\Framework\View\Element\Template\Context                 $context
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute                $eavAttribute
     * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
     * @param array                                                            $data
     */
    public function __construct(
        MpfavouritesellerRepository $mpFavouriteSellerRepository,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Customer $customerModel,
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory,
        array $data = []
    ) {
        $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->_mpFavouriteSellerRepository = $mpFavouriteSellerRepository;
        $this->_customerSession = $customerSession;
        $this->_customerModel = $customerModel;
        $this->moduleManager = $moduleManager;
        $this->eavAttribute = $eavAttribute;
        $this->customerFactory = $customerFactory;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->_mpSellerModel = $this->_objectManager->create(\Webkul\Marketplace\Model\Seller::class);
            $this->_mpHelper = $this->_objectManager->create(\Webkul\Marketplace\Helper\Data::class);
            $this->mpSeller = $this->_objectManager->create(
                \Webkul\Marketplace\Model\ResourceModel\Seller\Collection::class
            );
        }
        $this->_urlinterface = $context->getUrlBuilder();
        parent::__construct($context, $data);
    }

    /**
     * Get favourite Seller list
     *
     * @return array
     */
    public function getFavouriteSellerList()
    {
        $filter = $this->getRequest()->getParam('s')!="" ?
                $this->getRequest()->getParam('s') : "";
        $customerId = $this->_customerSession->getCustomer()->getId();
        $collection  = $this->_mpFavouriteSellerRepository
            ->getsellersCollectionByCustomerId($customerId);
        $firstName = $this->eavAttribute->getIdByCode("customer", "firstname");
        $lastName = $this->eavAttribute->getIdByCode("customer", "lastname");
        $customerEntity = $this->customerFactory->create()->getTable('customer_entity');
        $marketplaceUserdata = $this->mpSeller->getTable('marketplace_userdata');
        $collection->getSelect()->join(
            $customerEntity.' as cpev',
            'main_table.seller_id = cpev.entity_id',
            ["CONCAT(firstname,' ',lastname)"]
        )->where(
            "CONCAT(cpev.firstname,' ',cpev.lastname) like '%".$filter."%'"
        );
        $collection->getSelect()->join(
            $marketplaceUserdata.' as mpud',
            'main_table.seller_id = mpud.seller_id',
            ['shop_url','logo_pic']
        );
        $collection->setOrder('entity_id', 'DESC');
        return $collection;
    }

    /**
     * [_prepare Layout]
     *
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if ($this->getFavouriteSellerList()) {
            $pager = $this->getLayout()->createBlock(
                \Magento\Theme\Block\Html\Pager::class,
                'marketplace.product.list.pager'
            )->setCollection(
                $this->getFavouriteSellerList()
            );
            $this->setChild('pager', $pager);
        }
        return $this;
    }

    /**
     * Get Pager HTML
     *
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    /**
     * Get customer name
     *
     * @param  int $customerId contain customer id
     * @return String
     */
    public function getCustomerName($customerId)
    {
        return $this->_customerModel
                ->load($customerId)->getName();
    }

    /**
     * Get seller logo image
     *
     * @param  string $logoPic
     * @return string
     */
    public function getSellerLogo($logoPic)
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            if ($logoPic) {
                return $this->_mpHelper
                ->getMediaUrl().'avatar/'.$logoPic;
            } else {
                return $this->_mpHelper
                ->getMediaUrl().'avatar/noimage.png';
            }
        }
    }

    /**
     * Return collection url of seller
     *
     * @param  string $shopUrl
     * @return string
     */
    public function getSellerCollectionUrl($shopUrl)
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            return $this->_mpHelper->getRewriteUrl('marketplace/seller/collection/shop/'.$shopUrl);
        }
    }

    /**
     * Return seller profile url
     *
     * @param  string $shopUrl
     * @return string
     */
    public function getSellerProfileUrl($shopUrl)
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            return $this->_mpHelper->getRewriteUrl('marketplace/seller/profile/shop/'.$shopUrl);
        }
    }

    /**
     * Get current url
     *
     * @return string
     */
    public function getCurrentSiteUrl()
    {
        return $this->_urlinterface->getCurrentUrl();
    }

    /**
     * Get filter Params
     *
     * @return string
     */
    public function getFilterParam()
    {
        $filter = $this->getRequest()->getParam('s')!=""?
                $this->getRequest()->getParam('s'):"";
        return $filter;
    }
}
