<?php
namespace Autogedal\BlogInternalLinking\Controller\Adminhtml\Group;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Autogedal\BlogInternalLinking\Model\ResourceModel\Group\CollectionFactory;

class MassDelete extends Action
{
    protected $filter;
    protected $collectionFactory;

    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
    {
        $this->filter            = $filter;
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context);
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Autogedal_BlogInternalLinking::groups');
    }

    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $deleted    = 0;

        foreach ($collection->getItems() as $group) {
            $group->delete();
            $deleted++;
        }

        $this->messageManager->addSuccessMessage(__('A total of %1 group(s) deleted.', $deleted));
        return $this->resultRedirectFactory->create()->setPath('*/*/');
    }
}
