import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
import 'package:unit2/sevices/profile/profile_other_info.dart';
import 'package:unit2/sevices/profile/profile_service.dart';

import '../../model/profile/basic_information/primary-information.dart';
import '../../screens/profile/components/basic_information/profile_other_info.dart';
part 'profile_event.dart';
part 'profile_state.dart';

class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
  ProfileBloc() : super(ProfileInitial()) {
    Profile? currentProfileInformation;
    List<ProfileOtherInfo> religions = [];
    List<ProfileOtherInfo> ethnicities = [];
    List<ProfileOtherInfo> indigencies = [];
    List<ProfileOtherInfo> disabilities = [];
    List<ProfileOtherInfo> genders = [];
    List<String> bloodType = [
      "A+",
      "B+",
      "A-",
      "B-",
      "AB+",
      "AB-",
      "O+",
      "O-"
    ];
    List<String> nameExtensions = [
      "NONE",
      "N/A",
      "SR.",
      "JR.",
      "I",
      "II",
      "III",
      "IV",
      "V",
      "VI",
      "VII",
      "VIII",
      "IX",
      "X"
    ];
    List<String> sexes = ["MALE", "FEMALE"];
    List<String> civilStatus = [
      "NONE",
      "SINGLE",
      "MARRIED",
      "SEPARATED",
      "WIDOWED"
    ];

    ProfileInformation? globalProfileInformation;
    on<LoadProfile>((event, emit) async {
      emit(ProfileLoading());
      try {
        ProfileInformation? profileInformation =
            await ProfileService.instance.getProfile(event.token, event.userID);
        globalProfileInformation = profileInformation;
        emit(ProfileLoaded(profileInformation: globalProfileInformation!));
      } catch (e) {
        emit(ProfileErrorState(mesage: e.toString()));
      }
    });
    on<GetPrimaryBasicInfo>((event, emit) {
      currentProfileInformation = event.primaryBasicInformation;
      emit(BasicInformationProfileLoaded(
          primaryBasicInformation: event.primaryBasicInformation));
    });
    on<LoadBasicPrimaryInfo>((event,emit){
   emit(BasicInformationProfileLoaded(
          primaryBasicInformation: currentProfileInformation!));
    });
    on<ShowPrimaryInfoEditForm>((event, emit) async {
      try {
        emit(BasicPrimaryInformationLoadingState());
        if (religions.isEmpty) {
          religions = await ProfileOtherInfoServices.instace
              .getReligions(token: event.token);
        }
        if (genders.isEmpty) {
          genders = await ProfileOtherInfoServices.instace
              .getGenders(token: event.token);
        }
        if (ethnicities.isEmpty) {
          ethnicities = await ProfileOtherInfoServices.instace
              .getEthnicity(token: event.token);
        }
        if (disabilities.isEmpty) {
          disabilities = await ProfileOtherInfoServices.instace
              .getDisability(token: event.token);
        }
        if (indigencies.isEmpty) {
          indigencies = await ProfileOtherInfoServices.instace
              .getIndigency(token: event.token);
        }
        emit(BasicInformationEditingState(
            primaryInformation: currentProfileInformation!,
            extensions: nameExtensions,
            sexes: sexes,
            bloodTypes: bloodType,
            genders: genders,
            civilStatus: civilStatus,
            disability: disabilities,
            ethnicity: ethnicities,
            indigenous: indigencies,
            religion: religions));
      } catch (e) {
        emit(BasicPrimaryInformationErrorState(message: e.toString()));
      }
    });
    on<EditBasicProfileInformation>((event, emit) async {
      try {
        Map<dynamic, dynamic> status = await ProfileService.instance
            .updateBasicProfileInfo(
                token: event.token,
                profileId: event.profileId,
                profileInfo: event.profileInformation,
                genderId: event.genderId,
                indigencyId: event.indigencyId,
                disabilityId: event.disabilityId,
                ethnicityId: event.ethnicityId,
                reqligionId: event.religionId);
        if (status['success']) {
          Profile profile = Profile.fromJson(status['data']);
          currentProfileInformation = profile;
          emit(BasicProfileInfoEditedState(response: status));
        }
        emit(BasicProfileInfoEditedState(response: status));
      } catch (e) {
        emit(BasicPrimaryInformationErrorState(message: e.toString()));
      }
    });
  }
}
