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

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

class MassDelete extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var PageFactory
     */
    private $resultPageFactory;

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

    /**
     * @var Webkul\Agorae\Model\MpfavouritesellerRepository
     */
    private $mpFavouritesellerRepository;

    /**
     * @param Context                         $context
     * @param MpfavouritesellerRepository     $mpFavouritesellerRepository
     * @param \Magento\Customer\Model\Session $customerSession
     * @param PageFactory                     $resultPageFactory
     */
    public function __construct(
        Context $context,
        MpfavouritesellerRepository $mpFavouritesellerRepository,
        \Magento\Customer\Model\Session $customerSession,
        PageFactory $resultPageFactory
    ) {
        $this->mpFavouritesellerRepository = $mpFavouritesellerRepository;
        $this->_customerSession = $customerSession;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * MassDelete
     */
    public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($this->getRequest()->isPost()) {
            $customerIds = $this->getRequest()->getParam('customer_mass_delete');
            $sellerId = $this->_customerSession->getCustomer()->getId();

            foreach ($customerIds as $customerId) {
                $followerCollection = $this->mpFavouritesellerRepository
                                    ->getSellerCollectionByCustomerId($sellerId, $customerId);

                foreach ($followerCollection as $follower) {
                    $this->_delFollower($follower);
                }
            }
        }
        $this->messageManager->addSuccess(
            __(
                'Successfully followers removed'
            )
        );
        return $resultRedirect->setPath('agorae/shopfollower/index');
    }

    /**
     * Delete follower
     *
     * @param  object $thisObject
     * @return $this
     */
    private function _delFollower($thisObject)
    {
        $thisObject->delete()->save();
    }
}
