<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @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;
use Webkul\Agorae\Model\Mpfavouriteseller;

/**
 * Webkul Agorae ShopFollowerList Block
 */
class ShopFollowerList 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 Webkul\MpFavouriteSeller\Model\Mpfavouriteseller
     */
    protected $_mpFavouriteSellerModel;

    /**
     * @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;

    /**
     * @param MpfavouritesellerRepository                      $mpFavouriteSellerRepository
     * @param \Magento\Customer\Model\Session                  $customerSession
     * @param \Magento\Customer\Model\Customer                 $customerModel
     * @param Mpfavouriteseller                                $mpFavouriteSellerModel
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Module\Manager                $moduleManager
     * @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,
        Mpfavouriteseller $mpFavouriteSellerModel,
        \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->_mpFavouriteSellerModel = $mpFavouriteSellerModel;
        $this->eavAttribute = $eavAttribute;
        $this->moduleManager = $moduleManager;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->_mpHelper = $this->_objectManager->create(\Webkul\Marketplace\Helper\Data::class);
            $this->_mpSellerModel = $this->_objectManager->create(\Webkul\Marketplace\Model\Seller::class);
        }
        $this->customerFactory = $customerFactory;
        $this->_urlinterface = $context->getUrlBuilder();
        parent::__construct($context, $data);
    }

    /**
     * Get seller followers list
     *
     * @return object
     */
    public function getFollowerList()
    {
        $sellerId = $this->_customerSession
                    ->getCustomer()->getId();
        $collection  = $this->_mpFavouriteSellerRepository
                            ->getCustomersCollectionBySellerId($sellerId);
        $paramData = $this->getRequest()->getParams();
        $filter = '';
        $filterDateFrom = '';
        $filterDateTo = '';
        $from = null;
        $to = null;
        if (isset($paramData['s'])) {
            $filter = $paramData['s'] != '' ? $paramData['s'] : '';
        }
        if (isset($paramData['from_date'])) {
            $filterDateFrom = $paramData['from_date'] != '' ? $paramData['from_date'] : '';
        }
        if (isset($paramData['to_date'])) {
            $filterDateTo = $paramData['to_date'] != '' ? $paramData['to_date'] : '';
        }
        if ($filterDateTo) {
            $todate = date_create($filterDateTo);
            $to = date_format($todate, 'Y-m-d 23:59:59');
        }
        if (!$to) {
            $to = date('Y-m-d 23:59:59');
        }
        if ($filterDateFrom) {
            $fromdate = date_create($filterDateFrom);
            $from = date_format($fromdate, 'Y-m-d H:i:s');
        }
        $eavAttribute = $this->eavAttribute;
        $firstName = $eavAttribute->getIdByCode("customer", "firstname");
        $lastName = $eavAttribute->getIdByCode("customer", "lastname");
        $customerEntity = $this->customerFactory->create()->getTable('customer_entity');
        $collection->getSelect()->join(
            $customerEntity.' as cpev',
            'main_table.customer_id = cpev.entity_id',
            ["CONCAT(firstname,' ',lastname)"]
        )->where(
            "CONCAT(cpev.firstname,' ',cpev.lastname) like '%".$filter."%'"
        );
        if ($from && $to) {
            $collection->getSelect()->where(
                "main_table.liked_at BETWEEN '". $from ."' AND '". $to ."'"
            );
        }
        $collection->setOrder('entity_id', 'DESC');
        return $collection;
    }

    /**
     * [_prepareLayout]
     *
     * @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);
            $this->getFavouriteSellerList()->load();
        }
        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 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;
    }

    /**
     * Get from date
     *
     * @return string
     */
    public function filterDateFromParam()
    {
        $paramData = $this->getRequest()->getParams();
        if (isset($paramData['from_date'])) {
            return $paramData['from_date'] != '' ? $paramData['from_date'] : '';
        }
        return false;
    }

    /**
     * Get to date
     *
     * @return string
     */
    public function filterDateToParam()
    {
        $paramData = $this->getRequest()->getParams();
        if (isset($paramData['to_date'])) {
            return $paramData['to_date'] != '' ? $paramData['to_date'] : '';
        }
        return false;
    }

    /**
     * Get current session of customer
     *
     * @return int
     */
    public function getCurrentCustomerId()
    {
        return $this->_customerSession
                    ->getCustomer()->getId();
    }
}
