<?php

namespace Solwin\Verticalmenu\Block\Adminhtml\Menu\Edit\Tab;

class Menu extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface {

    /**
     * Type options
     * 
     * @var \Solwin\Verticalmenu\Model\Menu\Source\Type
     */
    protected $typeOptions;

    /**
     * Tab options
     * 
     * @var \Solwin\Verticalmenu\Model\Menu\Source\Tab
     */
    protected $tabOptions;

    /**
     * Status options
     * 
     * @var \Solwin\Verticalmenu\Model\Menu\Source\Status
     */
    protected $statusOptions;

    /**
     * constructor
     * 
     * @param \Solwin\Verticalmenu\Model\Menu\Source\Type $typeOptions
     * @param \Solwin\Verticalmenu\Model\Menu\Source\Tab $tabOptions
     * @param \Solwin\Verticalmenu\Model\Menu\Source\Status $statusOptions
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param array $data
     */
    public function __construct(
    \Solwin\Verticalmenu\Model\Menu\Source\Type $typeOptions, \Solwin\Verticalmenu\Model\Menu\Source\Tab $tabOptions, \Solwin\Verticalmenu\Model\Menu\Source\Status $statusOptions, \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, array $data = []
    ) {
        $this->typeOptions = $typeOptions;
        $this->tabOptions = $tabOptions;
        $this->statusOptions = $statusOptions;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm() {
        /** @var \Solwin\Verticalmenu\Model\Menu $menu */
        $menu = $this->_coreRegistry->registry('solwin_verticalmenu_menu');
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('menu_');
        $htmlIdPrefix = $form->getHtmlIdPrefix();
        $form->setFieldNameSuffix('menu');
        $fieldset = $form->addFieldset(
                'base_fieldset', [
            'legend' => __('Menu Information'),
            'class' => 'fieldset-wide'
                ]
        );
        $fieldset->addType('image', 'Solwin\Verticalmenu\Block\Adminhtml\Menu\Helper\Image');
        if (!$menu->getId()) {
            // path
            if ($this->getRequest()->getParam('parent')) {
                $fieldset->addField(
                        'path', 'hidden', ['name' => 'path', 'value' => $this->getRequest()->getParam('parent')]
                );
            } else {
                $fieldset->addField('path', 'hidden', ['name' => 'path', 'value' => 1]);
            }
        } else {
            $fieldset->addField(
                    'menu_id', 'hidden', ['name' => 'menu_id', 'value' => $menu->getId()]
            );
            $fieldset->addField(
                    'path', 'hidden', ['name' => 'path', 'value' => $menu->getPath()]
            );
        }
        $fieldset->addField(
                'menu_title', 'text', [
            'name' => 'menu_title',
            'label' => __('Menu Title'),
            'title' => __('Menu Title'),
            'required' => true,
                ]
        );
        $fieldset->addField(
                'type', 'select', [
            'name' => 'type',
            'label' => __('Type'),
            'title' => __('Type'),
            'required' => true,
            'values' => array_merge(['' => ''], $this->typeOptions->toOptionArray()),
                ]
        );
        $fieldset->addField(
                'url', 'text', [
            'name' => 'url',
            'label' => __('URL'),
            'title' => __('URL'),
                ]
        );
        $fieldset->addField(
                'tab', 'select', [
            'name' => 'tab',
            'label' => __('Tab'),
            'title' => __('Tab'),
            'values' => array_merge(['' => ''], $this->tabOptions->toOptionArray()),
                ]
        );
        $fieldset->addField(
                'category_id', 'text', [
            'name' => 'category_id',
            'label' => __('Category Id'),
            'title' => __('Category Id'),
                ]
        );
        $fieldset->addField(
                'icon', 'image', [
            'name' => 'icon',
            'label' => __('Icon'),
            'title' => __('Icon'),
                ]
        );
        $fieldset->addField(
                'sort_order', 'text', [
            'name' => 'sort_order',
            'label' => __('Sort Order'),
            'title' => __('Sort Order'),
            'required' => true,
                ]
        );
        $fieldset->addField(
                'status', 'select', [
            'name' => 'status',
            'label' => __('Status'),
            'title' => __('Status'),
            'required' => true,
            'values' => array_merge(['' => ''], $this->statusOptions->toOptionArray()),
                ]
        );

        $menuData = $this->_session->getData('solwin_verticalmenu_menu_data', true);
        if ($menuData) {
            $menu->addData($menuData);
        } else {
            if (!$menu->getId()) {
                $menu->addData($menu->getDefaultValues());
            }
        }
        $form->addValues($menu->getData());
        $this->setChild(
                'form_after', $this->getLayout()->createBlock(
                                'Magento\Backend\Block\Widget\Form\Element\Dependence'
                        )->addFieldMap(
                                "{$htmlIdPrefix}type", 'type'
                        )
                        ->addFieldMap(
                                "{$htmlIdPrefix}url", 'url'
                        )
                        ->addFieldMap(
                                "{$htmlIdPrefix}tab", 'tab'
                        )
                        ->addFieldMap(
                                "{$htmlIdPrefix}category_id", 'category_id'
                        )
                        ->addFieldDependence(
                                'url', 'type', 1
                        )
                        ->addFieldDependence(
                                'url', 'type', 1
                        )
                        ->addFieldDependence(
                                'tab', 'type', 1
                        )
                        ->addFieldDependence(
                                'category_id', 'type', 2
                        )
        );
        $this->setForm($form);
        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return string
     */
    public function getTabLabel() {
        return __('Menu');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle() {
        return $this->getTabLabel();
    }

    /**
     * Can show tab in tabs
     *
     * @return boolean
     */
    public function canShowTab() {
        return true;
    }

    /**
     * Tab is hidden
     *
     * @return boolean
     */
    public function isHidden() {
        return false;
    }

}
