<?php

declare(strict_types=1);

namespace FiloBlu\ExtBlackbirdMenuManager\Plugin\Block\Adminhtml\Menu\Edit;

use FiloBlu\ExtBlackbirdMenuManager\Model\ResourceModel\Group\CollectionFactory;
use Magento\Framework\Data\Form;

/**
 *
 */
class Main
{
    /**
     * @var \FiloBlu\ExtBlackbirdMenuManager\Model\ResourceModel\Group\CollectionFactory
     */
    private $collectionFactory;

    /**
     * @param \FiloBlu\ExtBlackbirdMenuManager\Model\ResourceModel\Group\CollectionFactory $collectionFactory
     */
    public function __construct(
        CollectionFactory $collectionFactory
    ) {
        $this->collectionFactory = $collectionFactory;
    }

    /**
     * @param \Blackbird\MenuManager\Block\Adminhtml\Menu\Edit\Main $subject
     * @param \Magento\Framework\Data\Form $form
     * @return \Magento\Framework\Data\Form[]
     * @see \Blackbird\MenuManager\Block\Adminhtml\Menu\Edit\Main::setForm()
     */
    public function beforeSetForm(
        \Blackbird\MenuManager\Block\Adminhtml\Menu\Edit\Main $subject,
        Form $form
    ) {
        $fieldSet = $form->getElement('menu_fieldset');

        if (!$fieldSet) {
            return null;
        }

        $fieldSet->addField(
            'customer_group',
            'multiselect',
            [
                'name'     => 'customer_group[]',
                'label'    => __('Customer Group'),
                'title'    => __('Customer Group'),
                'values'   => $this->collectionFactory->create()->toOptionArray(),
                'disabled' => false
            ]
        );

        return [$form];
    }
}
