import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';

/// A bottom sheet that allows the user to select their preferred language.
///
/// Displays options for English and Spanish, and updates the application's
/// locale via the [LocaleBloc].
class LanguageSelectorBottomSheet extends StatelessWidget {
  /// Creates a [LanguageSelectorBottomSheet].
  const LanguageSelectorBottomSheet({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(UiConstants.space6),
      decoration: BoxDecoration(
        color: UiColors.background,
        borderRadius: BorderRadius.vertical(top: Radius.circular(UiConstants.radiusBase)),
      ),
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              t.settings.change_language,
              style: UiTypography.headline4m,
              textAlign: TextAlign.center,
            ),
            SizedBox(height: UiConstants.space6),
            _buildLanguageOption(
              context,
              label: 'English',
              locale: AppLocale.en,
            ),
            SizedBox(height: UiConstants.space4),
            _buildLanguageOption(
              context,
              label: 'Español',
              locale: AppLocale.es,
            ),
            SizedBox(height: UiConstants.space6),
          ],
        ),
      ),
    );
  }

  Widget _buildLanguageOption(
    BuildContext context, {
    required String label,
    required AppLocale locale,
  }) {
    // Check if this option is currently selected.
    // We can use LocaleSettings.currentLocale for a quick check,
    // or access the BLoC state if we wanted to be reactive to state changes here directly,
    // but LocaleSettings is sufficient for the initial check.
    final bool isSelected = LocaleSettings.currentLocale == locale;

    return InkWell(
      onTap: () {
        // Dispatch the ChangeLocale event to the LocaleBloc
        Modular.get<LocaleBloc>().add(ChangeLocale(locale.flutterLocale));
        
        // Close the bottom sheet
        Navigator.pop(context);
        
        // Force a rebuild of the entire app to reflect locale change instantly if not handled by root widget
        // (Usually handled by BlocBuilder at the root, but this ensures settings are updated)
      },
      borderRadius: BorderRadius.circular(UiConstants.radiusMdValue),
      child: Container(
        padding: EdgeInsets.symmetric(
          vertical: UiConstants.space4,
          horizontal: UiConstants.space4,
        ),
        decoration: BoxDecoration(
          color: isSelected ? UiColors.primary.withValues(alpha: 0.1) : UiColors.background,
          borderRadius: BorderRadius.circular(UiConstants.radiusMdValue),
          border: Border.all(
            color: isSelected ? UiColors.primary : UiColors.border,
            width: isSelected ? 2 : 1,
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              label,
              style: isSelected
                  ? UiTypography.body1b.copyWith(color: UiColors.primary)
                  : UiTypography.body1r,
            ),
            if (isSelected)
              Icon(
                UiIcons.check,
                color: UiColors.primary,
                size: 24.0,
              ),
          ],
        ),
      ),
    );
  }
}
