import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/eligibility.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
import 'package:unit2/model/utils/eligibility.dart';
import 'package:unit2/sevices/profile/eligibility_services.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';
import '../../model/location/region.dart';
import '../../model/location/provinces.dart';
import '../../model/location/city.dart';
part 'profile_event.dart';
part 'profile_state.dart';

class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
  ProfileBloc() : super(ProfileInitial()) {
    ProfileInformation? globalProfileInformation;
    List<Country>? globalCountries;
    List<Region>? globalRegions;
    List<Eligibility>? globalEligibilities;
    List<EligibityCert>? eligibilities;
    ////=========================================================================
    on<LoadProfile>((event, emit) async {
      // try {
      emit(ProfileLoading());
      ProfileInformation? profileInformation =
          await ProfileService.instance.getProfile(event.token, event.userID);
      globalProfileInformation = profileInformation;
      emit(ProfileLoaded(profileInformation: globalProfileInformation!));
      // } catch (e) {
      //   emit(ProfileErrorState(mesage: e.toString()));
      // }
    });
////=====================================================================
    on<LoadEligibility>((event, emit) {
      emit(ProfileLoading());
      eligibilities = event.eligibilities;
      emit(EligibilityLoaded(eligibilities: event.eligibilities));
    });
////====================================================================
    on<EditEligibility>((event, emit) async {
      // try{
      emit(ProfileLoading());
      if (globalCountries == null) {
        List<Country> countries = await LocationUtils.instance.getCountries();
        globalCountries = countries;
      }
      if (globalRegions == null) {
        List<Region> regions = await LocationUtils.instance.getRegions();
        globalRegions = regions;
      }
      if (globalEligibilities == null) {
        List<Eligibility> eligibilities =
            await ProfileUtilities.instance.getEligibilities();
        globalEligibilities = eligibilities;
      }
      bool? isOverseas = event.eligibityCert.overseas;

      emit(EditEligibilityState(
          isOverseas: isOverseas!,
          eligibityCert: event.eligibityCert,
          countries: globalCountries!,
          regions: globalRegions!,
          eligibilities: globalEligibilities!));

      // }catch(e){
      //   emit(ProfileErrorState(mesage: e.toString()));
      // }

      ////====================================================================
    });
    on<ShowAddEligibilityForm>((event, emit) async {
      emit(ProfileLoading());
      if (globalRegions == null) {
        List<Region> regions = await LocationUtils.instance.getRegions();
        globalRegions = regions;
      }
      if (globalEligibilities == null) {
        List<Eligibility> eligibilities =
            await ProfileUtilities.instance.getEligibilities();
        globalEligibilities = eligibilities;
      }
      if (globalCountries == null) {
        List<Country> countries = await LocationUtils.instance.getCountries();
        globalCountries = countries;
      }

      emit(AddEligibilityState(
          eligibilities: globalEligibilities!,
          regions: globalRegions!,
          countries: globalCountries!));
    });
    ////====================================================================
    on<DeleteEligibility>((event, emit) async {
      emit(ProfileLoading());
      try {
        final bool success = await EligibilityService.instance.delete(
            eligibilityId: event.eligibilityId,
            profileId: int.parse(event.profileId),
            token: event.token);
        if (success) {
          event.eligibilities.removeWhere(
              ((EligibityCert element) => element.id == event.eligibilityId));
          List<EligibityCert> eligibilities = event.eligibilities;
          emit(DeletedState(success: success, eligibilities: eligibilities));
        } else {
          emit(DeletedState(
              success: success, eligibilities: event.eligibilities));
        }
      } catch (e) {
        emit(ProfileErrorState(mesage: e.toString()));
      }
    });
    on<AddEligibility>(
      (event, emit) async {
        try {
          emit(ProfileLoading());
          Map<dynamic, dynamic> status = await EligibilityService.instance.add(
              eligibityCert: event.eligibityCert,
              token: event.token,
              profileId: int.parse(event.profileId));
          if (status['success']) {
            EligibityCert? eligibityCert =
                EligibityCert.fromJson(status['data']);
            eligibilities!.add(eligibityCert);
            emit(EligibilityAddedState(
                eligibilities: eligibilities!, response: status));
          } else {
            emit(EligibilityAddedState(
                eligibilities: eligibilities!, response: status));
          }
        } catch (e) {
          emit(ProfileErrorState(mesage: e.toString()));
        }
      },
    );
  }
}
