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

class SettingSelect<T> extends StatelessWidget {
  final T? value;
  final List<T> options;
  final String Function(T value) labelOf;
  final ValueChanged<T?>? onChanged;
  final bool enabled;

  const SettingSelect({
    super.key,
    required this.value,
    required this.options,
    required this.labelOf,
    required this.onChanged,
    this.enabled = true,
  });

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.maybeSizeOf(context)?.width;
    final maxWidth = screenWidth == null
        ? 240.0
        : (screenWidth * 0.45).clamp(120.0, 240.0).toDouble();

    return ConstrainedBox(
      constraints: BoxConstraints(minWidth: 96, maxWidth: maxWidth),
      child: FSelect<T>.rich(
        enabled: enabled && onChanged != null,
        control: FSelectControl<T>.lifted(
          value: value,
          onChange: onChanged ?? (_) {},
        ),
        format: labelOf,
        children: [
          for (final option in options)
            FSelectItem<T>.item(title: Text(labelOf(option)), value: option),
        ],
      ),
    );
  }
}
