<?php
namespace PpPhp\Plugins;
use PpPhp\Core\EditPlugin;
use PpPhp\Core\Designer\FieldControl\LookupOptions;

/**
 * Button toggle (segmented control) — pick ONE value from options rendered
 * as a joined button group. Great for status/type fields. Options from a
 * manual list or a database-table lookup. Variant / color / size / shape.
 * Submits the chosen value via a hidden input the buttons drive with JS
 * (progressive: the first option is pre-selected server-side).
 */
class ButtonToggle implements EditPlugin
{
    private const COLORS = ['primary', 'info', 'success', 'warning', 'danger', 'secondary', 'dark'];

    public static function id(): string { return 'ButtonToggle'; }
    public static function label(): string { return 'Button Toggle'; }
    public static function version(): string { return '1.0.0'; }
    public static function description(): string { return 'A segmented control of mutually exclusive toggle buttons for a single choice.'; }

    public static function render(string $fieldName, mixed $value, array $settings): string
    {
        $color = (string) ($settings['color'] ?? 'primary'); if (!in_array($color, self::COLORS, true)) $color = 'primary';
        $size = (string) ($settings['size'] ?? 'md'); if (!in_array($size, ['sm','md','lg'], true)) $size = 'md';
        $rounded = ($settings['shape'] ?? 'default') === 'rounded';
        $block = in_array(strtolower((string) ($settings['block'] ?? '')), ['1', 'yes', 'true', 'on'], true);
        $disabled = ($settings['state'] ?? '') === 'disabled';

        $options = LookupOptions::resolve($settings);
        if ($options === []) $options = ['Option A' => 'Option A', 'Option B' => 'Option B'];

        $cur = (string) $value;
        if ($cur === '') $cur = (string) array_key_first($options);

        $cls = 'pp-seg pp-bc-' . $color . ' pp-bz-' . $size
            . ($rounded ? ' is-rounded' : '') . ($block ? ' is-block' : '') . ($disabled ? ' is-disabled' : '');

        $buttons = '';
        foreach ($options as $val => $label) {
            $on = ((string) $val === $cur) ? ' is-on' : '';
            $buttons .= '<button type="button" class="pp-seg-btn' . $on . '" data-val="' . htmlspecialchars((string) $val) . '">'
                . htmlspecialchars((string) $label) . '</button>';
        }

        return '<div class="' . $cls . '" data-pp-seg="' . $fieldName . '">'
            . '<input type="hidden" name="' . $fieldName . '" id="field_' . $fieldName . '" value="' . htmlspecialchars($cur) . '">'
            . $buttons . '</div>'
            . '<script>(function(){var g=document.querySelector(\'[data-pp-seg="' . $fieldName . '"]\');if(!g||g.__b)return;g.__b=1;'
            . 'g.addEventListener("click",function(e){var b=e.target.closest(".pp-seg-btn");if(!b)return;'
            . 'g.querySelector("input").value=b.getAttribute("data-val");'
            . 'g.querySelectorAll(".pp-seg-btn").forEach(function(x){x.classList.toggle("is-on",x===b)});});})();</script>';
    }

    public static function settingsSchema(): array
    {
        return array_merge([
            ['key' => 'color', 'label' => 'Color', 'type' => 'select', 'options' => self::COLORS, 'default' => 'primary'],
            ['key' => 'size', 'label' => 'Size', 'type' => 'select', 'options' => ['sm', 'md', 'lg'], 'default' => 'md'],
            ['key' => 'shape', 'label' => 'Shape', 'type' => 'select', 'options' => ['default', 'rounded'], 'default' => 'default'],
            ['key' => 'block', 'label' => 'Full width', 'type' => 'bool', 'default' => 'no'],
            ['key' => 'state', 'label' => 'State', 'type' => 'select', 'options' => ['normal', 'disabled'], 'default' => 'normal'],
        ], LookupOptions::schema());
    }

    public static function assets(): array { return ['js' => [], 'css' => []]; }
}
