<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Controller\Adminhtml\Answer;

use Magediary\ProductQuestion\Controller\Adminhtml\Answer;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;

class Delete extends Answer
{
    public const ADMIN_RESOURCE = 'Magediary_ProductQuestion::question_answer_delete';

    /**
     * Delete action
     *
     * @return ResultInterface
     */
    public function execute()
    {
        /** @var Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        // check if we know what should be deleted
        $id = $this->getRequest()->getParam('answer_id');
        if ($id) {
            try {
                // init model and delete
                $model = $this->_objectManager->create(\Magediary\ProductQuestion\Model\Answer::class);
                $model->load($id);
                $model->delete();
                // display success message
                $this->messageManager->addSuccessMessage(__('You deleted the Answer.'));
                // go to grid
                return $resultRedirect->setPath('*/*/');
            } catch (\Exception $e) {
                // display error message
                $this->messageManager->addErrorMessage($e->getMessage());
                // go back to edit form
                return $resultRedirect->setPath('*/*/edit', ['answer_id' => $id]);
            }
        }
        // display error message
        $this->messageManager->addErrorMessage(__('We can\'t find a Answer to delete.'));
        // go to grid
        return $resultRedirect->setPath('*/*/');
    }
}
