<?php
/**
 * Copyright © SIBS. All rights reserved.
 *
 * @author    SIBS <suporte@sibs.pt>
 * See LICENSE for license details.
 */

namespace Sibs\Sibs\Model\Adminhtml\Source;

/**
 * Class PaymentGroup - Fieldset renderer for SIBS.
 *
 * @SuppressWarnings(PHPMD.CamelCaseMethodName)
 * @SuppressWarnings(PHPMD.CamelCasePropertyName)
 */
class PaymentGroup extends \Magento\Config\Block\System\Config\Form\Fieldset
{
    /**
     * @var \Magento\Config\Model\Config
     */
    private $config;

    /**
     * @var \Sibs\Sibs\Helper\DataHelper
     */
    private $sibsHelper;

    /**
     * @param \Magento\Backend\Block\Context $context
     * @param \Magento\Backend\Model\Auth\Session $authSession
     * @param \Magento\Framework\View\Helper\Js      $jsHelper
     * @param \Magento\Config\Model\Config  $config
     * @param array   $data
     */
    public function __construct(
        \Magento\Backend\Block\Context $context,
        \Magento\Backend\Model\Auth\Session $authSession,
        \Magento\Framework\View\Helper\Js $jsHelper,
        \Magento\Config\Model\Config $config,
        \Sibs\Sibs\Helper\DataHelper $sibsHelper,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $authSession,
            $jsHelper,
            $data
        );
        $this->config     = $config;
        $this->sibsHelper = $sibsHelper;
    }

    /**
     * Add custom css class.
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
     *
     * @return string
     */
    protected function _getFrontendClass($element)
    {
        return parent::_getFrontendClass($element) . ' with-button';
    }

    /**
     * Return header title part of html for payment solution.
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
     *
     * @return string
     */
    protected function _getHeaderTitleHtml($element)
    {
        $htmlId = $element->getHtmlId();
        $url    = $this->getUrl('adminhtml/*/state');

        $configMessage = __('Configure');
        $closeMessage  = __('Close');

        $legend  = $element->getLegend();
        $comment = $element->getComment();

        $html = '<div class="config-heading">';

        $html .= "<div class=\"button-container\">
                <button type=\"button\" class=\"button action-configure\"
                    id=\"$htmlId-head\" onclick=\"psToggleSolution.call(this, '$htmlId', '$url'); return false;\">
                    <span class=\"state-closed\">
                        $configMessage
                    </span>
                    <span class=\"state-opened\">
                        $closeMessage
                    </span>
                </button>
            </div>";
        $html .= '<div class="heading">';
        $html .= "<strong>$legend</strong>";

        if (! empty($comment)) {
            $html .= "<span class=\"heading-intro\">$comment</span>";
        }

        $html .= '<div class="config-alt"></div>';
        $html .= '</div>
            </div>';

        return $html;
    }

    /**
     * Return header comment part of html for payment solution.
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
     *
     * @return string
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function _getHeaderCommentHtml($element)
    {
        return 'v' . $this->sibsHelper->getVersion();
    }

    /**
     * Get collapsed state on-load.
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
     *
     * @return false
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function _isCollapseState($element)
    {
        return false;
    }

    /**
     * Return extra Js.
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
     *
     * @return string
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function _getExtraJs($element)
    {
        $script = "require(['jquery', 'prototype'], function(jQuery){
            window.psToggleSolution = function (id, url) {
                var doScroll = false;
                Fieldset.toggleCollapse(id, url);
                if ($(this).hasClassName(\"open\")) {
                    \$$(\".with-button button.button\").each(function(anotherButton) {
                        if (anotherButton != this && $(anotherButton).hasClassName(\"open\")) {
                            $(anotherButton).click();
                            doScroll = true;
                        }
                    }.bind(this));
                }
                if (doScroll) {
                    var pos = Element.cumulativeOffset($(this));
                    window.scrollTo(pos[0], pos[1] - 45);
                }
            }
        });";

        return $this->_jsHelper->getScript($script);
    }
}
