<?php
namespace PpPhp\Plugins;
use PpPhp\Core\EditPlugin;

/**
 * Styled switch — five designs (classic thumb-check, slim, inner text,
 * inner icon, circular icon), size, color, on/off labels and disabled state
 * configurable from the Designer properties panel. Submits 1/0.
 * Markup + classes are shared with the Designer canvas (pp-ctl CSS).
 */
class Switch_ implements EditPlugin
{
    public static function id(): string { return 'Switch'; }
    public static function label(): string { return 'Switch'; }
    public static function version(): string { return '1.0.0'; }
    public static function description(): string { return 'An on/off toggle switch storing a yes/no value.'; }

    public static function render(string $fieldName, mixed $value, array $settings): string
    {
        $size = (string) ($settings['size'] ?? 'md');
        if (!in_array($size, ['sm', 'md', 'lg'], true)) $size = 'md';
        $colors = ['primary', 'info', 'success', 'danger', 'secondary', 'warning', 'dark'];
        $color = (string) ($settings['color'] ?? 'primary');
        if (!in_array($color, $colors, true)) $color = 'primary';
        $designs = ['classic', 'slim', 'text', 'icon', 'circle'];
        $design = (string) ($settings['design'] ?? 'classic');
        if (!in_array($design, $designs, true)) $design = 'classic';
        $disabled = ($settings['state'] ?? '') === 'disabled';
        // Configurable submitted values (defaults 1 / 0)
        $on = trim((string) ($settings['onValue'] ?? '1'));
        if ($on === '') $on = '1';
        $off = trim((string) ($settings['offValue'] ?? '0'));
        $checked = (string) $value === $on
            || ($on === '1' && in_array(strtolower((string) $value), ['1', 'true', 'yes', 'on'], true));
        $onText = htmlspecialchars((string) ($settings['onText'] ?? 'ON'));
        $offText = htmlspecialchars((string) ($settings['offText'] ?? 'OFF'));
        $onIcon = htmlspecialchars((string) ($settings['onIcon'] ?? 'bi-brightness-high'));
        $offIcon = htmlspecialchars((string) ($settings['offIcon'] ?? 'bi-moon'));

        // Optional live state label beside the switch + confirm-on-toggle
        $side = '';
        if (($settings['sideText'] ?? 'none') === 'state') {
            $side = '<span class="pp-sw-side"><span class="on">' . $onText . '</span><span class="off">' . $offText . '</span></span>';
        }
        $confirm = trim((string) ($settings['confirmText'] ?? ''));
        $confirmAttr = $confirm !== ''
            ? ' onclick="return confirm(' . htmlspecialchars(json_encode($confirm), ENT_QUOTES) . ')"'
            : '';

        return '<label class="pp-ctl pp-switch pp-v-' . $design . ' pp-sz-' . $size . ' pp-c-' . $color . ($disabled ? ' pp-disabled' : '') . '">'
            . '<input type="hidden" name="' . $fieldName . '" value="' . htmlspecialchars($off) . '">'
            . '<input type="checkbox" name="' . $fieldName . '" id="field_' . $fieldName . '" value="' . htmlspecialchars($on) . '"'
            . ($checked ? ' checked' : '') . ($disabled ? ' disabled' : '') . $confirmAttr . '>'
            . '<span class="pp-sw">'
            . '<span class="pp-sw-on">' . $onText . '</span><span class="pp-sw-off">' . $offText . '</span>'
            . '<span class="pp-sw-onic"><i class="bi ' . $onIcon . '"></i></span><span class="pp-sw-offic"><i class="bi ' . $offIcon . '"></i></span>'
            . '<span class="pp-sw-thumb"><i class="bi bi-check-lg ic-on"></i><i class="bi bi-x-lg ic-off"></i></span>'
            . '</span>' . $side . '</label>';
    }

    public static function settingsSchema(): array
    {
        return [
            ['key' => 'design', 'label' => 'Design', 'type' => 'select', 'options' => ['classic', 'slim', 'text', 'icon', 'circle'], 'default' => 'classic'],
            ['key' => 'size', 'label' => 'Size', 'type' => 'select', 'options' => ['sm', 'md', 'lg'], 'default' => 'md'],
            ['key' => 'color', 'label' => 'Color', 'type' => 'select', 'options' => ['primary', 'info', 'success', 'danger', 'secondary', 'warning', 'dark'], 'default' => 'primary'],
            ['key' => 'state', 'label' => 'State', 'type' => 'select', 'options' => ['normal', 'disabled'], 'default' => 'normal'],
            ['key' => 'onText', 'label' => 'On text (text design)', 'type' => 'text', 'default' => 'ON'],
            ['key' => 'offText', 'label' => 'Off text (text design)', 'type' => 'text', 'default' => 'OFF'],
            ['key' => 'onIcon', 'label' => 'On icon (icon design)', 'type' => 'text', 'default' => 'bi-brightness-high'],
            ['key' => 'offIcon', 'label' => 'Off icon (icon design)', 'type' => 'text', 'default' => 'bi-moon'],
            ['key' => 'onValue', 'label' => 'On value', 'type' => 'text', 'default' => '1', 'hint' => 'Stored when on. Examples: 1 · Y · active'],
            ['key' => 'offValue', 'label' => 'Off value', 'type' => 'text', 'default' => '0', 'hint' => 'Stored when off. Examples: 0 · N · inactive'],
            ['key' => 'sideText', 'label' => 'State label beside switch', 'type' => 'select', 'options' => ['none', 'state'], 'default' => 'none', 'hint' => 'Shows the On/Off text next to the switch, updating live as it toggles.'],
            ['key' => 'confirmText', 'label' => 'Confirm before toggle', 'type' => 'text', 'default' => '', 'hint' => "Ask before changing. Example: Are you sure you want to change the status?"],
            // How the stored on/off value appears on the list & view pages.
            ['key' => 'displayAs', 'label' => 'Show on list & view', 'type' => 'radiogroup', 'options' => ['label', 'value'], 'default' => 'label', 'hint' => 'label = the On/Off text · value = the raw stored value.'],
        ];
    }

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