import 'package:flutter/material.dart';

import '../../../../utils/haptics.dart';
import '../../../../widget/centered_leading.dart';

/// Settings row with a trailing checkbox. Fires [Haptics.selection] before
/// invoking [onChanged] (with the resolved non-null value), so the sections
/// don't repeat that. The leading icon is vertically centered when a [subtitle]
/// is present, matching the surrounding settings styling.
class SettingsCheckboxTile extends StatelessWidget {
  final IconData icon;
  final String title;
  final String? subtitle;
  final bool value;
  final ValueChanged<bool> onChanged;

  /// Optional widget rendered just before the checkbox (e.g. a status icon).
  final Widget? beforeCheckbox;

  const SettingsCheckboxTile({
    required this.icon,
    required this.title,
    required this.value,
    required this.onChanged,
    this.subtitle,
    this.beforeCheckbox,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final leadingIcon = Icon(icon);
    final checkbox = Checkbox(
      value: value,
      onChanged: (e) {
        Haptics.selection();
        onChanged(e ?? false);
      },
    );
    return ListTile(
      leading: subtitle == null ? leadingIcon : CenteredLeading(leadingIcon),
      title: Text(title),
      subtitle: subtitle == null ? null : Text(subtitle!),
      trailing: beforeCheckbox == null
          ? checkbox
          : Row(
              mainAxisSize: MainAxisSize.min,
              children: [beforeCheckbox!, checkbox],
            ),
    );
  }
}
