import 'package:flutter_bloc/flutter_bloc.dart';

import 'choose_country_event.dart';
import 'choose_country_state.dart';

class RadioBloc extends Bloc<RadioEvent, RadioState> {
  RadioBloc() : super(RadioInitial()) {
    on<RadioSelected>(_onRadioSelected);
    on<ResetRadioSelection>(_onResetRadioSelection);
  }

  void _onRadioSelected(RadioSelected event, Emitter<RadioState> emit) {
    emit(RadioSelectionChanged(event.selectedIndex));
  }

  void _onResetRadioSelection(
      ResetRadioSelection event, Emitter<RadioState> emit) {
    emit(RadioInitial());
  }

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

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