<?php

namespace GriffinForms\Admin\Html\FormControls\Field\FieldSettings;

class InputRichtextSettings extends \GriffinForms\Admin\Html\FormControls\Field\FieldSettings\Format
{
    private const TOOLBAR_ITEMS = [
        'bold'           => 'Bold',
        'italic'         => 'Italic',
        'underline'      => 'Underline',
        'strike'         => 'Strike',
        'heading1'       => 'Heading 1',
        'heading2'       => 'Heading 2',
        'heading3'       => 'Heading 3',
        'bulletlist'     => 'Bullet List',
        'orderedlist'    => 'Ordered List',
        'blockquote'     => 'Blockquote',
        'code'           => 'Code',
        'link'           => 'Link',
        'horizontalrule' => 'Horizontal Rule',
        'clearformatting' => 'Clear Formatting',
    ];

    private const DEFAULTS = [
        'toolbar'        => ['bold', 'italic', 'underline', 'strike', 'bulletlist', 'orderedlist', 'blockquote', 'link', 'clearformatting'],
        'placeholder'    => 'Write something…',
        'min_height'     => 160,
        'max_height'     => 0,
        'max_characters' => 0,
    ];

    public function html()
    {
        $s = is_array($this->value) ? $this->value : [];

        $toolbar        = is_array($s['toolbar'] ?? null) ? $s['toolbar'] : self::DEFAULTS['toolbar'];
        $placeholder    = isset($s['placeholder']) ? (string) $s['placeholder'] : 'Write something…';
        $min_height     = isset($s['min_height']) ? max(50, (int) $s['min_height']) : 160;
        $max_height     = isset($s['max_height']) ? max(0, (int) $s['max_height']) : 0;
        $max_characters = isset($s['max_characters']) ? max(0, (int) $s['max_characters']) : 0;

        $placeholder_field = new \GriffinForms\Admin\Html\FormControls\InputText('placeholder', $placeholder);
        ?>
        <div class="inputrichtext-settings">
            <div class="row g-3">

                <div class="col-12">
                    <label class="form-label mb-2"><?php echo esc_html($this->lang->getText('richtext_toolbar', 'label')); ?></label>
                    <div class="d-flex flex-wrap gap-2">
                        <?php foreach (self::TOOLBAR_ITEMS as $key => $label) : ?>
                            <label class="d-inline-flex align-items-center gap-1 border rounded px-2 py-1 small">
                                <input type="checkbox"
                                    class="griffinforms-richtext-toolbar-item griffinforms-field griffinforms-formcontrol"
                                    data-toolbar-item="<?php echo esc_attr($key); ?>"
                                    value="<?php echo esc_attr($key); ?>"
                                    <?php checked(in_array($key, $toolbar, true)); ?>>
                                <?php echo esc_html($label); ?>
                            </label>
                        <?php endforeach; ?>
                    </div>
                </div>

                <div class="col-md-6">
                    <label class="form-label"><?php echo esc_html($this->lang->getText('richtext_placeholder', 'label')); ?></label>
                    <?php $placeholder_field->html(); ?>
                </div>

                <div class="col-12">
                    <div class="d-flex flex-wrap align-items-center gap-4">
                        <div class="d-flex align-items-center gap-2">
                            <label class="text-nowrap mb-0"><?php echo esc_html($this->lang->getText('richtext_min_height', 'label')); ?></label>
                            <input type="number" style="width:70px" min="50" class="form-control form-control-sm griffinforms-minheight griffinforms-inputnumber griffinforms-formcontrol griffinforms-field" id="griffinforms-minheight-inputnumber" value="<?php echo esc_attr($min_height); ?>">
                            <span class="text-muted small">px</span>
                        </div>
                        <div class="d-flex align-items-center gap-2">
                            <label class="text-nowrap mb-0"><?php echo esc_html($this->lang->getText('richtext_max_height', 'label')); ?></label>
                            <input type="number" style="width:70px" min="0" class="form-control form-control-sm griffinforms-maxheight griffinforms-inputnumber griffinforms-formcontrol griffinforms-field" id="griffinforms-maxheight-inputnumber" value="<?php echo esc_attr($max_height); ?>">
                            <span class="text-muted small">px</span>
                        </div>
                        <div class="d-flex align-items-center gap-2">
                            <label class="text-nowrap mb-0"><?php echo esc_html($this->lang->getText('richtext_max_characters', 'label')); ?></label>
                            <input type="number" style="width:90px" min="0" class="form-control form-control-sm griffinforms-maxcharacters griffinforms-inputnumber griffinforms-formcontrol griffinforms-field" id="griffinforms-maxcharacters-inputnumber" value="<?php echo esc_attr($max_characters); ?>">
                        </div>
                    </div>
                    <div class="form-text text-muted mt-1"><?php echo esc_html($this->lang->getText('richtext_max_characters', 'description')); ?></div>
                </div>

            </div>
        </div>
        <?php
    }
}
