<?php
namespace Solwin\Verticalmenu\Controller\Adminhtml\Menu;

class RefreshPath extends \Solwin\Verticalmenu\Controller\Adminhtml\Menu
{
    /**
     * JSON Result Factory
     * 
     * @var \Magento\Framework\Controller\Result\JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * constructor
     * 
     * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
     * @param \Solwin\Verticalmenu\Model\MenuFactory $menuFactory
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Solwin\Verticalmenu\Model\MenuFactory $menuFactory,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Backend\App\Action\Context $context
    ) {
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($menuFactory, $coreRegistry, $context);
    }


    /**
     * Build response for refresh input element 'path' in form
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $menuId = (int)$this->getRequest()->getParam('menu_id');
        if ($menuId) {
            $menu = $this->menuFactory->create()->load($menuId);

            /** @var \Magento\Framework\Controller\Result\Json $resultJson */
            $resultJson = $this->resultJsonFactory->create();
            return $resultJson->setData(['id' => $menuId, 'path' => $menu->getPath()]);
        }
    }
}
