import 'package:bloc/bloc.dart';
import 'package:tanami_app/core/styles/app_text.dart';

import 'checkbox_event.dart';
import 'checkbox_state.dart';

class CheckboxBloc extends Bloc<CheckboxEvent, CheckBoxState> {
  CheckboxBloc() : super(CheckboxUnchecked()) {
    on<ToggleCheckbox>(_onToggleCheckbox);
    on<ValidateCheckbox>(_onValidateCheckbox);
  }

  void _onToggleCheckbox(ToggleCheckbox event, Emitter<CheckBoxState> emit) {
    if (state is CheckboxUnchecked || state is CheckboxError) {
      emit(CheckboxChecked());
    } else {
      emit(CheckboxUnchecked());
    }
  }

  void _onValidateCheckbox(
      ValidateCheckbox event, Emitter<CheckBoxState> emit) {
    if (state is! CheckboxChecked) {
      emit(const CheckboxError(AppText.pleaseCheckThisField));
    }
  }
}
