<?php

namespace FiloBlu\ExtBlackbirdMenuManager\Controller\Adminhtml\Subcategory\Widget;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\View\LayoutFactory;

/**
 *
 */
class Chooser extends Action
{
    /**
     * @var \Magento\Framework\Controller\Result\RawFactory
     */
    protected $resultRawFactory;

    /**
     * @var \Magento\Framework\View\LayoutFactory
     */
    protected $layoutFactory;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
     * @param \Magento\Framework\View\LayoutFactory $layoutFactory
     */
    public function __construct(
        Context $context,
        RawFactory $resultRawFactory,
        LayoutFactory $layoutFactory
    ) {
        parent::__construct($context);
        $this->resultRawFactory = $resultRawFactory;
        $this->layoutFactory = $layoutFactory;
    }

    /**
     * Chooser Source action
     *
     * @return \Magento\Framework\Controller\Result\Raw
     */
    public function execute()
    {

        $layout = $this->layoutFactory->create();
        // subcategory Chooser
        $block = $layout->createBlock(
           // \FiloBlu\ExtBlackbirdMenuManager\Block\Adminhtml\Subcategory\Widget\Chooser::class,
            \Blackbird\MenuManager\Block\Adminhtml\Category\Widget\Chooser::class,
            'menu_nodetype_subcategory',
            [
                'data' => [
                    'id' => $this->getRequest()->getParam('uniq_id'),
                    'use_massaction' => $this->getRequest()->getParam('use_massaction', false),
                    'js_form_object' => $this->getRequest()->getParam('form', null),
                ]
            ]
        );
        $html = $block->toHtml();

        /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
        $resultRaw = $this->resultRawFactory->create();

        return $resultRaw->setContents($html);
    }
}
