<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Faqs
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\Faqs\Block\Adminhtml\Article\Edit\Tab;

use Magento\Backend\Block\Store\Switcher\Form\Renderer\Fieldset\Element;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Form\Element\Dependence;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Cms\Model\Wysiwyg\Config;
use Magento\Config\Model\Config\Source\Design\Robots;
use Magento\Config\Model\Config\Source\Yesno;
use Magento\Framework\Data\Form;
use Magento\Framework\Data\Form\Element\Renderer\RendererInterface;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Store\Model\System\Store;
use Mageplaza\Faqs\Block\Adminhtml\Article\Edit\Tab\Renderer\Category;
use Mageplaza\Faqs\Block\Adminhtml\Icon\Edit\Tab\Renderer\Image;
use Mageplaza\Faqs\Block\Adminhtml\Article\Edit\Tab\Renderer\Tag;
use Mageplaza\Faqs\Helper\File as HelperFile;
use Mageplaza\Faqs\Model\Article as ArticleModel;
use Mageplaza\Faqs\Model\Config\Source\IconType;
use Mageplaza\Faqs\Model\Config\Source\Visibility;

/**
 * Class Article
 * @package Mageplaza\Faqs\Block\Adminhtml\Article\Edit\Tab
 */
class Article extends Generic implements TabInterface
{
    /**
     * @var Store
     */
    public $systemStore;

    /**
     * @var Config
     */
    public $wysiwygConfig;

    /**
     * @var Yesno
     */
    protected $_booleanOption;

    /**
     * @var Visibility
     */
    protected $_visibility;

    /**
     * @var Robots
     */
    protected $_metaRobotsOptions;

    /**
     * @var IconType
     */
    protected $iconType;

    /**
     * @var HelperFile
     */
    protected $helperFile;

    /**
     * Article constructor.
     *
     * @param Context $context
     * @param Registry $registry
     * @param FormFactory $formFactory
     * @param Yesno $yesno
     * @param Robots $robots
     * @param Store $systemStore
     * @param Visibility $visibility
     * @param Config $wysiwygConfig
     * @param IconType $iconType
     * @param HelperFile $helperFile
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        Yesno $yesno,
        Robots $robots,
        Store $systemStore,
        Visibility $visibility,
        Config $wysiwygConfig,
        IconType $iconType,
        HelperFile $helperFile,
        array $data = []
    ) {
        $this->_booleanOption     = $yesno;
        $this->_metaRobotsOptions = $robots;
        $this->systemStore        = $systemStore;
        $this->_visibility        = $visibility;
        $this->wysiwygConfig      = $wysiwygConfig;
        $this->iconType           = $iconType;
        $this->helperFile         = $helperFile;

        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * @inheritdoc
     * @return Generic
     * @throws LocalizedException
     */
    protected function _prepareForm()
    {
        /** @var ArticleModel $article */
        $article = $this->_coreRegistry->registry('mageplaza_faqs_article');

        /** @var Form $form */
        $form = $this->_formFactory->create();

        $form->setHtmlIdPrefix('article_');
        $form->setFieldNameSuffix('article');

        $fieldset = $form->addFieldset('base_fieldset', [
            'legend' => __('General'),
            'class'  => 'fieldset-wide'
        ]);

        $fieldset->addField('name', 'text', [
            'name'     => 'name',
            'label'    => __('Question'),
            'title'    => __('Question'),
            'required' => true
        ]);

        $fieldset->addField('url_key', 'text', [
            'name'  => 'url_key',
            'label' => __('URL Key'),
            'title' => __('URL Key')
        ]);

        $fieldset->addField('article_content', 'editor', [
            'name'   => 'article_content',
            'label'  => __('Answer'),
            'title'  => __('Answer'),
            'config' => $this->wysiwygConfig->getConfig([
                'add_variables'  => false,
                'add_widgets'    => false,
                'add_directives' => true
            ])
        ]);

        $fieldset->addField('categories_ids', Category::class, [
            'name'  => 'categories_ids',
            'label' => __('Categories'),
            'title' => __('Categories'),
        ]);
        if (!$article->getCategoriesIds()) {
            $article->setCategoriesIds($article->getCategoryIds());
        }

        $fieldset->addField('tags_ids', Tag::class, [
            'name'  => 'tags_ids',
            'label' => __('Tags'),
            'title' => __('Tags'),
        ]);
        if (!$article->getTagsIds()) {
            $article->setTagsIds($article->getTagIds());
        }

        if (!$this->_storeManager->isSingleStoreMode()) {
            /** @var RendererInterface $rendererBlock */
            $rendererBlock = $this->getLayout()->createBlock(Element::class);
            $fieldset->addField('store_ids', 'multiselect', [
                'name'     => 'store_ids',
                'label'    => __('Store Views'),
                'title'    => __('Store Views'),
                'required' => true,
                'values'   => $this->systemStore->getStoreValuesForForm(false, true)
            ])->setRenderer($rendererBlock);

            if (!$article->hasData('store_ids')) {
                $article->setStoreIds(0);
            }
        } else {
            $fieldset->addField('store_ids', 'hidden', [
                'name'  => 'store_ids',
                'value' => $this->_storeManager->getStore()->getId()
            ]);
        }

        $fieldset->addField('visibility', 'select', [
            'name'   => 'visibility',
            'label'  => __('Visibility'),
            'title'  => __('Visibility'),
            'values' => $this->_visibility->toOptionArray()
        ]);

        $fieldset->addField('position', 'text', [
            'name'  => 'position',
            'label' => __("Position"),
            'title' => __("Position"),
        ]);
        if (!$article->hasData('position')) {
            $article->setPosition(0);
        }

        $iconType = $fieldset->addField('icon_type', 'select', [
            'name'  => 'icon_type',
            'label' => __('Icon Type'),
            'title' => __('Icon Type'),
            'values' => $this->iconType->toOptionArray()
        ]);

        $icon = $fieldset->addField('icon', 'text', [
            'name'  => 'icon',
            'label' => __('Icon'),
            'title' => __('Icon'),
            'note'  => '<div id="article-icon-image" title="Icon Image"><i class="far fa-file-alt fa-3x"><img src="' .
                $this->getViewFileUrl('Mageplaza_Faqs::media/images/icon-loader.gif') . '"></i></div>'
        ]);

        if (!$article->hasData('icon')) {
            $article->setIcon('far fa-file-alt fa-3x');
        }

        $iconCustomPath = $fieldset->addField('icon_custom_path', Image::class, [
            'name'     => 'icon_custom_path',
            'label'    => __('Icon'),
            'title'    => __('Icon'),
            'path'     => $this->helperFile->getBaseMediaPath(HelperFile::TEMPLATE_MEDIA_TYPE_FILE),
            'required' => true
        ]);

        $iconCustomWidth = $fieldset->addField('icon_custom_width', 'text', [
            'name'  => 'icon_custom_width',
            'label' => __('Icon Custom Width'),
            'title' => __('Icon Custom Width'),
            'note'  => __('Please leave 80px for the best Width icon. If empty or 0, it will default to 80px'),
            'class' => 'validate-number'
        ]);

        $iconCustomHeight = $fieldset->addField('icon_custom_height', 'text', [
            'name'  => 'icon_custom_height',
            'label' => __('Icon Custom Height'),
            'title' => __('Icon Custom Height'),
            'note'  => __('Please leave 60px for the best Height icon. If empty or 0, it will default to 60px'),
            'class' => 'validate-number'
        ]);

        $customerFieldset = $form->addFieldset('customer_fieldset', [
            'legend' => __('Customer Information'),
            'class'  => 'fieldset-wide'
        ]);

        $customerFieldset->addField('author_name', 'text', [
            'name'     => 'author_name',
            'label'    => __('Author Name'),
            'title'    => __('Author Name'),
            'required' => true
        ]);

        $customerFieldset->addField('author_email', 'text', [
            'name'     => 'author_email',
            'label'    => __('Author Email'),
            'title'    => __('Author Email'),
            'required' => true,
            'class'    => 'validate-email'
        ]);

        $customerFieldset->addField('email_notify', 'checkbox', [
            'name'     => 'email_notify',
            'label'    => __('Receive Email Notification'),
            'title'    => __('Receive Email Notification'),
            'checked'  => ($article->getEmailNotify()) ? true : false,
            'onchange' => 'this.value = this.checked ? 1 : 0;',
            'value'    => $article->getEmailNotify()
        ]);

        $seoFieldset = $form->addFieldset('seo_fieldset', [
            'legend' => __('SEO Config'),
            'class'  => 'fieldset-wide'
        ]);

        $seoFieldset->addField('meta_title', 'text', [
            'name'  => 'meta_title',
            'label' => __('Meta Title'),
            'title' => __('Meta Title')
        ]);

        $seoFieldset->addField('meta_description', 'textarea', [
            'name'  => 'meta_description',
            'label' => __('Meta Description'),
            'title' => __('Meta Description')
        ]);

        $seoFieldset->addField('meta_keywords', 'textarea', [
            'name'  => 'meta_keywords',
            'label' => __('Meta Keywords'),
            'title' => __('Meta Keywords')
        ]);

        $seoFieldset->addField('meta_robots', 'select', [
            'name'   => 'meta_robots',
            'label'  => __('Meta Robots'),
            'title'  => __('Meta Robots'),
            'values' => $this->_metaRobotsOptions->toOptionArray()
        ]);

        $form->addValues($article->getData());
        $this->setChild(
            'form_after',
            $this->getLayout()->createBlock(Dependence::class)
                ->addFieldMap($iconType->getHtmlId(), $iconType->getName())
                ->addFieldMap($icon->getHtmlId(), $icon->getName())
                ->addFieldMap($iconCustomPath->getHtmlId(), $iconCustomPath->getName())
                ->addFieldMap($iconCustomWidth->getHtmlId(), $iconCustomWidth->getName())
                ->addFieldMap($iconCustomHeight->getHtmlId(), $iconCustomHeight->getName())
                ->addFieldDependence($icon->getName(), $iconType->getName(), 0)
                ->addFieldDependence($iconCustomPath->getName(), $iconType->getName(), 1)
                ->addFieldDependence($iconCustomWidth->getName(), $iconType->getName(), 1)
                ->addFieldDependence($iconCustomHeight->getName(), $iconType->getName(), 1)
        );
        $this->setForm($form);

        return parent::_prepareForm();
    }

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

    /**
     * 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;
    }
}
