<?php

namespace GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormControls;

class Signature extends \GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormControls\Format
{
    public function getHtml()
    {
        $field_settings = $this->item->getProp('field_settings');
        $s = is_array($field_settings) ? $field_settings : [];

        $placeholder = isset($s['signature_placeholder']) && $s['signature_placeholder'] !== ''
            ? $s['signature_placeholder']
            : __('Sign here', 'griffinforms-form-builder');

        $pad_height = isset($s['pad_height']) ? (int) $s['pad_height'] : 200;
        $pad_height = max(50, min(600, $pad_height));

        $bg_color  = isset($s['bg_color'])  && $s['bg_color']  !== '' ? $s['bg_color']  : '#ffffff';
        $pen_color = isset($s['pen_color']) && $s['pen_color'] !== '' ? $s['pen_color'] : '#000000';

        $this->renderFieldStyles(['text_input']);
        $this->renderSignatureClearButtonStyle();
        $this->renderSignatureBgColorStyle($bg_color);

        $style_attrs  = $this->getFieldStyleAttributes('text_input');
        $clear_attrs  = $this->getFieldStyleAttributes('gf_sig_clear');
        $pad_style    = 'width:100%;max-width:100%;height:' . $pad_height . 'px;' . ($this->isThemeSet() ? '' : 'background-color:' . esc_attr($bg_color) . ';');
        $fallback_pad = $this->isThemeSet() ? '' : ' border rounded';

        echo '<div class="d-flex align-items-center justify-content-center position-relative' . $fallback_pad . '" style="' . esc_attr($pad_style) . '"' . $style_attrs . '>';
        echo '<span style="color:' . esc_attr($pen_color) . ';font-size:13px;opacity:0.5;">' . esc_html($placeholder) . '</span>';
        echo '</div>';
        echo '<div class="d-flex justify-content-end mt-1">';
        $clear_classes = $this->isThemeSet() ? 'gf-signature-clear-preview' : 'btn btn-sm btn-outline-secondary';
        echo '<button type="button" class="' . esc_attr($clear_classes) . '" disabled' . $clear_attrs . '>' . esc_html__('Clear', 'griffinforms-form-builder') . '</button>';
        echo '</div>';
    }

    private function renderSignatureBgColorStyle(string $bg_color): void
    {
        if (!$this->isThemeSet() || $bg_color === '') {
            return;
        }

        $group    = esc_attr($this->getFieldStyleGroupId());
        $selector = '[data-gf-style-group="' . $group . '"][data-gf-style-type="text_input"]';
        $css      = $selector . ' { background-color: ' . esc_attr($bg_color) . ' !important; }';
        $css      = \GriffinForms\Items\FormTheme::prepareStyleTagContent($css);
        if ($css !== '') {
            echo '<style>' . $css . '</style>';
        }
    }

    private function renderSignatureClearButtonStyle(): void
    {
        if (!$this->isThemeSet()) {
            return;
        }

        $prev_css = $this->form_theme->getPreviousButtonCss('default');
        if (empty($prev_css)) {
            return;
        }

        $group   = esc_attr($this->getFieldStyleGroupId());
        $selector = '[data-gf-style-group="' . $group . '"][data-gf-style-type="gf_sig_clear"]';
        $css     = $selector . ' { ' . $prev_css . ' }';
        $css     = \GriffinForms\Items\FormTheme::prepareStyleTagContent($css);
        if ($css !== '') {
            echo '<style>' . $css . '</style>';
        }
    }
}
