import 'package:flutter/material.dart';

class UsageFrequencyRadioGroup extends StatelessWidget {
  final String? selectedFrequency;
  final String? usageFrequencySelected;
  final Function(String) onFrequencySelected;

  const UsageFrequencyRadioGroup({
    super.key,
    required this.selectedFrequency,
    required this.usageFrequencySelected,
    required this.onFrequencySelected,
  });

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    final text = Theme.of(context)
        .textTheme
        .bodySmall!
        .copyWith(color: Colors.black, fontSize: 13);

    return size.width < 600
        ? Column(
            children: [
              _buildRadioTile(
                context,
                'One-Time',
                selectedFrequency ?? usageFrequencySelected,
                onFrequencySelected,
                text: text,
                fullWidth: true,
              ),
              const SizedBox(height: 8),
              _buildRadioTile(
                context,
                'Periodic',
                selectedFrequency ?? usageFrequencySelected,
                onFrequencySelected,
                text: text,
                fullWidth: true,
              ),
            ],
          )
        : Row(
            children: [
              _buildRadioTile(
                context,
                'One-Time',
                selectedFrequency ?? usageFrequencySelected,
                onFrequencySelected,
                width: size.width * 0.12,
                text: text,
              ),
              _buildRadioTile(
                context,
                'Periodic',
                selectedFrequency ?? usageFrequencySelected,
                onFrequencySelected,
                width: size.width * 0.12,
                text: text,
              ),
            ],
          );
  }

  Widget _buildRadioTile(
    BuildContext context,
    String value,
    String? groupValue,
    Function(String) onChanged, {
    double? width,
    required TextStyle text,
    bool fullWidth = false,
  }) {
    return SizedBox(
      width: fullWidth ? double.infinity : width,
      child: RadioListTile<String>(
        contentPadding: EdgeInsets.zero,
        title: Text(value, style: text),
        value: value,
        groupValue: groupValue,
        onChanged: (String? value) {
          if (value != null) {
            onChanged(value);
          }
        },
      ),
    );
  }
}
