<?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\Controller\Favouriteseller;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Webkul\Agorae\Helper\Data;
use Webkul\Agorae\Model\Mpfavouriteseller;
use Webkul\Agorae\Model\MpfavouritesellerRepository;

class AddFavouriteSeller extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var PageFactory
     */
    protected $_resultPageFactory;

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

    /**
     * @var Webkul\SocialSignup\Helper\Data
     */
    protected $_dataHelper;

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

    /**
     * @var Webkul\Agorae\Model\Mpfavouriteseller
     */
    protected $_mpFavouritesellerModel;

    /**
     * @param Context                                     $context
     * @param Data                                        $dataHelper
     * @param Mpfavouriteseller                           $mpFavouritesellerModel
     * @param MpfavouritesellerRepository                 $mpFavouritesellerRepository
     * @param \Magento\Customer\Model\Session             $customerSession
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
     * @param PageFactory                                 $resultPageFactory
     */
    public function __construct(
        Context $context,
        Data $dataHelper,
        Mpfavouriteseller $mpFavouritesellerModel,
        MpfavouritesellerRepository $mpFavouritesellerRepository,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        PageFactory $resultPageFactory
    ) {
        $this->_mpFavouritesellerModel = $mpFavouritesellerModel;
        $this->_mpFavouritesellerRepository = $mpFavouritesellerRepository;
        $this->_dataHelper = $dataHelper;
        $this->_date = $date;
        $this->_customerSession = $customerSession;
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * AddFavouriteSeller
     */
    public function execute()
    {
        try {
            $resultRedirect = $this->resultRedirectFactory->create();
            $time = $this->_date->gmtDate();
            $sellerId = $this->getRequest()->getParam('seller_id');
            $customerId = $this->_customerSession->getCustomer()->getId();
            $favouritesCollection = $this->_mpFavouritesellerRepository
            ->getSellerCollectionByCustomerId($sellerId, $customerId);
            if (!count($favouritesCollection)) {
                $favouriteSellerModel = $this->_mpFavouritesellerModel;
                $favouriteSellerModel->setSellerId($sellerId);
                $favouriteSellerModel->setCustomerId($customerId);
                $favouriteSellerModel->setLikedAt($time);
                $autoId = $favouriteSellerModel->save()->getEntityId();
                $this->messageManager->addSuccess(
                    __('Successfully Saved in your favourite Seller list')
                );
            } else {
                $this->messageManager->addNotice(
                    __('Already added to your favourite Seller list')
                );
            }
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
        }
        return $resultRedirect->setPath('agorae/favouriteseller/index');
    }
}
