import 'dart:async';

import 'package:boilerplate/domain/usecases/notification/get_notifications_use_case.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';

import '../../../../domain/entities/notification/notification_data.dart';

part 'notification_event.dart';
part 'notification_state.dart';

@injectable
class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
  final GetNotificationsUseCase _getNotificationsUseCase;
  NotificationsBloc(
    this._getNotificationsUseCase,
  ) : super(InitialState(NotificationsStateData())) {
    on<GetNotificationsEvent>(_onGetNotifications);
    on<SelectNotificationEvent>(_onSelectNotification);
  }

  Future<FutureOr<void>> _onGetNotifications(
    GetNotificationsEvent event,
    Emitter<NotificationsState> emit,
  ) async {
    emit(LoadingState(state.data));
    final result = await _getNotificationsUseCase.execute();

    result.fold((data) {
      emit(GetNotificationsSuccessState(state.data.copyWith(
        newNotis: data,
      )));
    }, (fail) {
      emit(GetNotificationsFailState(state.data));
    });
  }

  FutureOr<void> _onSelectNotification(
    SelectNotificationEvent event,
    Emitter<NotificationsState> emit,
  ) {}
}
