import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/work_history.dart';
import 'package:unit2/model/utils/agency.dart';
import 'package:unit2/model/utils/agency_position.dart';
import 'package:unit2/model/utils/position.dart';
import 'package:unit2/sevices/profile/work_history_services.dart';
import 'package:unit2/utils/profile_utilities.dart';
import '../../../model/profile/attachment.dart';

import '../../../model/utils/category.dart';
import '../../../utils/attachment_services.dart';
part 'workHistory_event.dart';
part 'workHistory_state.dart';

class WorkHistoryBloc extends Bloc<WorkHistorytEvent, WorkHistoryState> {
  List<WorkHistory> workExperiences = [];
  List<PositionTitle> agencyPositions = [];
  List<Agency> agencies = [];
  List<AppoinemtStatus> appointmentStatus = [];
  List<Category> agencyCategory = [];
  List<AttachmentCategory> attachmentCategories = [];
  WorkHistoryBloc() : super(EducationInitial()) {
    ////GET WORK HISTORIES
    on<GetWorkHistories>((event, emit) async {
      emit(WorkHistoryLoadingState());
      try {
        if (attachmentCategories.isEmpty) {
          attachmentCategories =
              await AttachmentServices.instance.getCategories();
        }
        if (workExperiences.isEmpty) {
          List<WorkHistory> works = await WorkHistoryService.instance
              .getWorkExperiences(event.profileId, event.token);
          workExperiences = works;
        }
        emit(WorkHistoryLoaded(
            workExperiences: workExperiences,
            attachmentCategory: attachmentCategories));
      } catch (e) {
        emit(WorkHistoryErrorState(message: e.toString()));
      }
    });
///// LOAD WORK HISTORIES
    on<LoadWorkHistories>((event, emit) {
      emit(WorkHistoryLoadingState());
      emit(WorkHistoryLoaded(
          workExperiences: workExperiences,
          attachmentCategory: attachmentCategories));
    });
    ////DELETE
    on<DeleteWorkHistory>((event, emit) async {
      try {
        final bool success = await WorkHistoryService.instance.delete(
            profileId: event.profileId,
            token: event.token,
            work: event.workHistory);
        if (success) {
          workExperiences.removeWhere(
              (WorkHistory element) => element.id == event.workHistory.id);
          emit(DeletedState(success: success));
        } else {
          emit(DeletedState(success: success));
        }
      } catch (e) {
        emit(WorkHistoryErrorState(message: e.toString()));
      }
    });
    //// ADD WORK HISTORIES
    on<AddWorkHostory>((event, emit) async {
      try {
        Map<dynamic, dynamic> status = await WorkHistoryService.instance.add(
          accomplishment: event.accomplishment,
          actualDuties: event.actualDuties,
            isPrivate: event.isPrivate,
            workHistory: event.workHistory,
            token: event.token,
            profileId: event.profileId);
        if (status['success']) {
          WorkHistory workHistory = WorkHistory.fromJson(status['data']);
          workExperiences.add(workHistory);
          emit(WorkHistoryAddedState(response: status));
        } else {
          emit(WorkHistoryAddedState(response: status));
        }
      } catch (e) {
        emit(WorkHistoryErrorState(message: e.toString()));
      }
    });

////UPDATE WORK HISTORY
    on<UpdateWorkHistory>((event, emit) async {
      // try {
        Map<dynamic, dynamic> status = await WorkHistoryService.instance.update(
          isPrivate: event.isPrivate,
           workHistory: event.workHistory,
            token: event.token,
            profileId: event.profileId);
        if (status['success']) {
          WorkHistory workHistory = WorkHistory.fromJson(status['data']);
          // workExperiences.removeWhere((WorkHistory work) {
          //   return work.id == event.workHistory.id;
          // });
          workExperiences.add(workHistory);
          emit(WorkHistoryEditedState(response: status));
        } else {
          emit(WorkHistoryEditedState(
            response: status,
          ));
        }
      // } catch (e) {
      //   emit(WorkHistoryErrorState(message: e.toString()));
      // }
    });

////SHOW EDIT WORK HISTORIES
    on<ShowEditWorkHistoryForm>((event, emit) async {
      try {
        /////POSITIONS------------------------------------------
        if (agencyPositions.isEmpty) {
          List<PositionTitle> positions =
              await WorkHistoryService.instance.getAgencyPosition();
          agencyPositions = positions;
        }

        /////AGENCIES------------------------------------------
        if (agencies.isEmpty) {
          List<Agency> newAgencies =
              await ProfileUtilities.instance.getAgecies();
          agencies = newAgencies;
        }

        /////Category Agency------------------------------------------
        if (agencyCategory.isEmpty) {
          List<Category> categoryAgencies =
              await ProfileUtilities.instance.agencyCategory();
          agencyCategory = categoryAgencies;
        }
        /////////-------------------------------------
        if (appointmentStatus.isEmpty) {
          List<AppoinemtStatus> status =
              WorkHistoryService.instance.getAppointmentStatusList();
          appointmentStatus = status;
        }

        emit(EditWorkHistoryState(
            workHistory: event.workHistory,
            agencyPositions: agencyPositions,
            appointmentStatus: appointmentStatus,
            agencyCategory: agencyCategory,
            agencies: agencies));
      } catch (e) {
        emit(WorkHistoryErrorState(message: e.toString()));
      }
    });
    ////SHOW ADD FORM WORK HISTORIES
    on<ShowAddWorkHistoryForm>((event, emit) async {
      emit(WorkHistoryLoadingState());
      try {
        /////POSITIONS------------------------------------------
        if (agencyPositions.isEmpty) {
          List<PositionTitle> positions =
              await WorkHistoryService.instance.getAgencyPosition();
          agencyPositions = positions;
        }

        /////AGENCIES------------------------------------------
        if (agencies.isEmpty) {
          List<Agency> newAgencies =
              await ProfileUtilities.instance.getAgecies();
          agencies = newAgencies;
        }

        /////Category Agency------------------------------------------
        if (agencyCategory.isEmpty) {
          List<Category> categoryAgencies =
              await ProfileUtilities.instance.agencyCategory();
          agencyCategory = categoryAgencies;
        }
        /////////-------------------------------------
        if (appointmentStatus.isEmpty) {
          List<AppoinemtStatus> status =
              WorkHistoryService.instance.getAppointmentStatusList();
          appointmentStatus = status;
        }

        emit(AddWorkHistoryState(
            agencyPositions: agencyPositions,
            appointmentStatus: appointmentStatus,
            agencyCategory: agencyCategory,
            agencies: agencies));
      } catch (e) {
        emit(WorkHistoryErrorState(message: e.toString()));
      }
    });
    ////Add Attachment
    on<AddWorkHistoryAttachment>((event, emit) async {
      emit(WorkHistoryLoadingState());
      List<Attachment> attachments = [];
      WorkHistory workHistory = workExperiences.firstWhere(
          (element) => element.id.toString() == event.attachmentModule);
      try {
        Map<dynamic, dynamic> status = await AttachmentServices.instance
            .attachment(
                categoryId: event.categoryId,
                module: event.attachmentModule,
                paths: event.filePaths,
                token: event.token,
                profileId: event.profileId);
        if (status['success']) {
          status['data'].forEach((element) {
            Attachment newAttachment = Attachment.fromJson(element);
            attachments.add(newAttachment);
          });
          // workHistory.attachments == null
          //     ? workHistory.attachments = attachments
          //     : workHistory.attachments = [
          //         ...workHistory.attachments!,
          //         ...attachments
          //       ];
          emit(WorkHistoryDevAttachmentAddedState(response: status));
        } else {
          emit(WorkHistoryDevAttachmentAddedState(response: status));
        }
      } catch (e) {
        emit(WorkHistoryErrorState(message: e.toString()));
      }
    });
    //     ////Delete Attachment
    //     on<DeleteWorkHistoryAttachment>((event, emit) async {
    //   emit(WorkHistoryLoadingState());
    //   try {
    //     final bool success = await AttachmentServices.instance.deleteAttachment(
    //         attachment: event.attachment,
    //         moduleId: event.moduleId,
    //         profileId: event.profileId.toString(),
    //         token: event.token);
    //     if (success) {
    //       final WorkHistory workHistory =
    //           workExperiences
    //               .firstWhere((element) => element.id == event.moduleId);
    //       workHistory.attachments
    //           ?.removeWhere((element) => element.id == event.attachment.id);
    //       workExperiences
    //           .removeWhere((element) => element.id == event.moduleId);
    //       workExperiences.add(workHistory);
    //       emit(WorkHistoryDevAttachmentDeletedState(success: success));
    //     } else {
    //    emit(WorkHistoryDevAttachmentDeletedState(success: success));
    //     }
    //   } catch (e) {
    //     emit(WorkHistoryErrorState(message: e.toString()));
    //   }
    // });
  }
}
