<?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 Webkul\Agorae\Controller\Adminhtml\Sliderimages as SliderimagesController;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action;

/**
 * Agorae Adminhtml Sliderimages Edit Controller
 */
class Edit extends SliderimagesController
{
    /**
     * @var \Magento\MediaStorage\Model\File\UploaderFactory
     */
    protected $helper;

    /**
     * @var \Magento\Backend\Model\Session
     */
    protected $session;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * @param Action\Context             $context
     * @param \Webkul\Agorae\Helper\Data $helper
     * @param \Magento\Backend\Model\Session $session
     * @param \Magento\Framework\Registry $registry
     */
    public function __construct(
        Action\Context $context,
        \Webkul\Agorae\Helper\Data $helper,
        \Magento\Backend\Model\Session $session,
        \Magento\Framework\Registry $registry
    ) {
        $this->helper = $helper;
        $this->session = $session;
        $this->registry = $registry;
        parent::__construct($context);
    }

    /**
     * Edit action
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        $params = $this->getRequest()->getParams();
        $sliderimagesModel = $this->helper->getSliderimagesModel();
        if (isset($params['id'])) {
            $sliderimagesModel->load($params['id']);
        }
        $data = $this->session->getFormData(true);
        $sliderimagesModel['filename'] = $sliderimagesModel['link'];
        if (!empty($data)) {
            $sliderimagesModel->setData($data);
        }
        $this->registry->register('agorae_sliderimages', $sliderimagesModel);
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        $resultPage->getConfig()->getTitle()->prepend(__('Sliderimages'));
        $resultPage->getConfig()->getTitle()->prepend(
            $sliderimagesModel->getId() ? $sliderimagesModel->getTitle() : __('New Image')
        );
        $resultPage->addBreadcrumb(__('Manage Slider Images'), __('Manage Slider Images'));
        $content = $resultPage->getLayout()->createBlock(\Webkul\Agorae\Block\Adminhtml\Sliderimages\Edit::class);
        $resultPage->addContent($content);
        return $resultPage;
    }
}
