import 'package:flutter/material.dart';
import 'package:forui/forui.dart';


class ServerFilterBar extends StatelessWidget {
  final List<({String id, String name})> servers;
  final String? selected;
  final ValueChanged<String?> onSelect;
  final EdgeInsetsGeometry padding;

  const ServerFilterBar({
    super.key,
    required this.servers,
    required this.selected,
    required this.onSelect,
    this.padding = const EdgeInsets.fromLTRB(24, 0, 24, 12),
  });

  @override
  Widget build(BuildContext context) {
    if (servers.length < 2) return const SizedBox.shrink();
    return Padding(
      padding: padding,
      child: Align(
        alignment: Alignment.centerLeft,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Row(
            children: [
              _chip(label: '全部', value: null),
              for (final s in servers) _chip(label: s.name, value: s.id),
            ],
          ),
        ),
      ),
    );
  }

  Widget _chip({required String label, required String? value}) {
    final active = selected == value;
    final child = Text(label);
    return Padding(
      padding: const EdgeInsets.only(right: 8),
      child: FButton(
        variant: active ? FButtonVariant.primary : FButtonVariant.ghost,
        onPress: () => onSelect(value),
        child: child,
      ),
    );
  }
}
