<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Backend\Block\Template\Context;

/**
 * Custom checkbox field for admin system config
 */
class Checkbox extends Field
{
    /**
     * @var ScopeConfigInterface
     */
    private ScopeConfigInterface $scopeConfig;

    /**
     * @param Context $context
     * @param ScopeConfigInterface $scopeConfig
     * @param array $data
     */
    public function __construct(
        Context $context,
        ScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * Render the checkbox element HTML.
     *
     * @param AbstractElement $element
     * @return string
     */
    protected function _getElementHtml(AbstractElement $element): string
    {
        $htmlId = $element->getHtmlId();
        $name = $element->getName();
        $value = $element->getEscapedValue();
        $checked = $value ? ' checked="checked"' : '';

        $isDisabled = (bool)$this->scopeConfig->getValue(
            'payment/simplyin_payment/default_create_account_checked_disabled'
        );
        $disabled = $isDisabled ? ' disabled="disabled"' : '';

        $html = '<input type="hidden" name="' . $name . '" value="0"/>';
        $html .= '<input type="checkbox" id="' . $htmlId . '" name="' . $name
            . '" value="1"' . $checked . $disabled . '/>';

        if ($isDisabled) {
            $html .= '<p style="margin-top:6px;font-size:12px;color:#999;font-style:italic">'
                . __('This setting is managed by Simply.IN and cannot be changed.')
                . '</p>';
        }

        return $html;
    }

    /**
     * Decorate the configuration row HTML with custom styling.
     *
     * @param AbstractElement $element
     * @param string $html
     * @return string
     */
    protected function _decorateRowHtml(AbstractElement $element, $html): string
    {
        $rowId = 'row_' . $element->getHtmlId();

        $css = '<style>'
            . '#' . $rowId . ' td.label { width: 80%; text-align: left; padding-right: 20px; }'
            . '#' . $rowId . ' td.label label { white-space: normal; float: none; text-align: left; width: 100%; }'
            . '#' . $rowId . ' td.value { width: 20%; text-align: right; padding-right: 30px; }'
            . '#' . $rowId . ' td.value input[type="checkbox"] {'
            . '  appearance: auto; -webkit-appearance: checkbox; -moz-appearance: checkbox;'
            . '  width: 22px; height: 22px; cursor: pointer; accent-color: #006bb4;'
            . '  border: 2px solid #adadad; border-radius: 3px; vertical-align: middle;'
            . '}'
            . '#' . $rowId . ' td.value input[type="checkbox"]:disabled {'
            . '  opacity: 0.5; cursor: not-allowed;'
            . '}'
            . '#' . $rowId . ' td.scope-label,'
            . '#' . $rowId . ' td:last-child:not(.label):not(.value) { display: none; }'
            . '#' . $rowId . 'input[type="checkbox"]:after{content: unset;}'
            . '</style>';

        return $css . '<tr id="' . $rowId . '">' . $html . '</tr>';
    }

    /**
     * Suppress the scope label for this field.
     *
     * @param AbstractElement $element
     * @return string
     */
    protected function _renderScopeLabel(AbstractElement $element): string
    {
        return '';
    }
}
