<?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\Adminhtml\Sliderimages;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;
use Magento\TestFramework\ErrorLog\Logger;

/**
 * Agorae Adminhtml Sliderimages massDelete Controller
 */
class MassDelete extends \Magento\Backend\App\Action
{
    /**
     * @var \Webkul\Agorae\Helper\Data
     */
    protected $helper;

    /**
     * @var \Webkul\Agorae\Model\Sliderimages
     */
    protected $sliderImages;

    /**
     * @var \Magento\Ui\Component\MassAction\Filter
     */
    protected $uiFilter;

    /**
     * @param Action\Context             $context
     * @param \Webkul\Agorae\Helper\Data $helper
     * @param \Webkul\Agorae\Model\Sliderimages $sliderImages
     * @param \Magento\Ui\Component\MassAction\Filter $uiFilter
     */
    public function __construct(
        Action\Context $context,
        \Webkul\Agorae\Helper\Data $helper,
        \Webkul\Agorae\Model\Sliderimages $sliderImages,
        \Magento\Ui\Component\MassAction\Filter $uiFilter
    ) {
        $this->helper = $helper;
        $this->sliderImages = $sliderImages;
        $this->uiFilter = $uiFilter;
        parent::__construct($context);
    }

    /**
     * {@inheritdoc}
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Webkul_Agorae::sliderimages');
    }

    /**
     * MassDelete Action file
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {

        $sliderimagesModel = $this->sliderImages;
        $model = $this->uiFilter;
        $collection = $model->getCollection($sliderimagesModel->getCollection());
        foreach ($collection as $image) {
            $this->helper->deleteUploadedFile(true, $image['link']);
            $image->delete();
        }
        $this->messageManager->addSuccess(__('Image(s) deleted successfully.'));
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/');
    }
}
