import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../Api_Helper/base_manager.dart';
import '../../domain/repositories/language_api.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));
  }

  Future<String> langaugeChangeApiCall(String languageCode) async {
    Map<String, String> languageData = {"languageCode": languageCode};

    ResponseData response = await LanguageApi().updateLanguage(languageData);
    if (response.status == ResponseStatus.SUCCESS) {
      return "success";
    } else {
      return "failed";
    }
  }

  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;
  }
}
