import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/basic_info.dart';
import 'package:unit2/model/profile/basic_information/primary-information.dart';
import 'package:unit2/model/profile/eligibility.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
import 'package:unit2/model/utils/eligibilities_choices.dart';
import 'package:unit2/sevices/profile/profile_service.dart';
import 'package:unit2/utils/location_utilities.dart';
import 'package:unit2/utils/profile_utilities.dart';
import '../../model/location/country.dart' as country;
import '../../model/location/region.dart' as region;

part 'profile_event.dart';
part 'profile_state.dart';

class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
  ProfileBloc() : super(ProfileInitial()) {
    ProfileInformation? _profileInformation;
    on<LoadProfile>((event, emit) async {
      // try {
      emit(ProfileLoading());
      ProfileInformation? profileInformation =
          await ProfileService.instance.getProfile(event.token, event.userID);
      _profileInformation = profileInformation;
      emit(ProfileLoaded(profileInformation: _profileInformation!));
      // } catch (e) {
      //   emit(ProfileErrorState(mesage: e.toString()));
      // }
    });

    on<LoadEligibility>((event, emit) {
      emit(ProfileLoading());
      emit(EligibilityLoaded(eligibilities: event.eligibilities));
    });
    on<EditEligibility>((event, emit) async{
      // try{
        List<country.Country> countries = await LocationUtils.instance.getCountries();
        List<region.Region> regions = await LocationUtils.instance.getRegions();
        List<EligibilityList> eligibilities = await ProfileUtilities.instance.getEligibilities();
    emit(EditEligibilityState(eligibityCert: event.eligibityCert,countries: countries,regions: regions,eligibilities: eligibilities));
      // }catch(e){
      //   emit(ProfileErrorState(mesage: e.toString()));
      // }
  
    });
  }
}
