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

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

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

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

    /**
     * @var Webkul\Agorae\Model\MpfavouritesellerRepository
     */
    protected $_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);
    }

    /**
     * Delete class
     */
    public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        $sellerId = $this->getRequest()->getParam('id');
        $customerId = $this->_customerSession
                    ->getCustomer()->getId();
        $favouriteSeller = $this->_mpFavouritesellerRepository
            ->getSellerCollectionByCustomerId($sellerId, $customerId);
        foreach ($favouriteSeller as $favSeller) {
            $this->_delSeller($favSeller);
            $this->messageManager->addSuccess(
                __(
                    'Successfully favourite Seller removed'
                )
            );
        }
        return $resultRedirect->setPath('agorae/favouriteseller/index');
    }

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