import 'package:flutter_bloc/flutter_bloc.dart';

import 'choose_language_event.dart';
import 'choose_language_state.dart';

class ChooseLanguageBloc
    extends Bloc<ChooseLanguageEvent, ChooseLanguageState> {
  ChooseLanguageBloc() : super(ChooseLanguageInitial()) {
    on<ChooseLanguageSelected>(_onRadioSelected);
    on<ResetRadioSelection>(_onResetRadioSelection);
  }

  void _onRadioSelected(
      ChooseLanguageSelected event, Emitter<ChooseLanguageState> emit) {
    emit(ChooseLanguageSelectionChanged(event.selectedIndex));
  }

  void _onResetRadioSelection(
      ResetRadioSelection event, Emitter<ChooseLanguageState> emit) {
    emit(ChooseLanguageInitial());
  }

  void resetSelection() {
    add(ResetRadioSelection());
  }

  int get selectedCountry {
    if (state is ChooseLanguageSelectionChanged) {
      return (state as ChooseLanguageSelectionChanged).selectedIndex;
    }
    return 0;
  }
}
