<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\ProductPreview;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class DellItem extends \Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
{
    /**
     *
     */
    const CUSTOM_PREVIEWS = 'custom_previews';
    /**
     * @var \Magento\Framework\Filesystem\Driver\File
     */
    protected $file;
    /**
     * @var \Magetop\Advancepreview\Model\AdvancePreviewFileFactory
     */
    protected $advancePreviewFileFactory;
    /**
     * @var \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory
     */
    protected $advancePreviewUploadFactory;
    /**
     * @var \Magetop\Advancepreview\Model\PreviewThreeFactory
     */
    protected $previewThreeFactory;
    /**
     * @var \Magetop\Advancepreview\Model\ResourceModel\PreviewThreeFactory
     */
    protected $resPreviewThreeFactory;
    /**
     * @var \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory
     */
    protected $colPreviewThreeFactory;

    /**
     * UploadMedia constructor.
     * @param Action\Context $context
     * @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
     * @param \Magento\Framework\Filesystem\Driver\File $file
     * @param \Magetop\Advancepreview\Model\AdvancePreviewFileFactory $advancePreviewFileFactory
     * @param \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory $advancePreviewUploadFactory
     * @param \Magetop\Advancepreview\Model\PreviewThreeFactory $previewThreeFactory
     * @param \Magetop\Advancepreview\Model\ResourceModel\PreviewThreeFactory $resPreviewThreeFactory
     * @param \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory $colPreviewThreeFactory
     */
    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magetop\Advancepreview\Model\AdvancePreviewFileFactory $advancePreviewFileFactory,
        \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory $advancePreviewUploadFactory,
        \Magetop\Advancepreview\Model\PreviewThreeFactory $previewThreeFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewThreeFactory $resPreviewThreeFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory $colPreviewThreeFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->file = $file;
        $this->advancePreviewFileFactory = $advancePreviewFileFactory;
        $this->advancePreviewUploadFactory = $advancePreviewUploadFactory;
        $this->previewThreeFactory = $previewThreeFactory;
        $this->resPreviewThreeFactory = $resPreviewThreeFactory;
        $this->colPreviewThreeFactory = $colPreviewThreeFactory;

    }

    /**
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\ResultInterface|void
     * @throws \Magento\Framework\Exception\FileSystemException
     */
    function execute()
    {
        $response = [
            'status'=>'error',
            'message'=>'Can not delete model',
            'html'=>''
        ];
        $id = $this->getRequest()->getParam('preview_id',0);
        if($id > 0) {
            if($this->dellThreeModel($id)) {
                $response['status'] = 'success';
                $response['message'] = 'Delete model preview success';
                $response['html'] = $this->_view->getLayout()->createBlock('Magetop\Advancepreview\Block\Adminhtml\Product\Edit\Form')->setTemplate('Magetop_Advancepreview::product/items.phtml')->toHtml();

            }
        }
        return $this->getAjaxResponse($response);
    }

    /**
     * @param int $id
     * @return bool
     */
    protected function dellThreeModel($id) {
        $model = $this->previewThreeFactory->create();
        $model->setId($id);
        try {
            $this->resPreviewThreeFactory->create()->delete($model);
            return true;
        }
        catch (\Exception $exception) {

        }
        return  false;
    }
}
