import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../core/resources/data_state.dart';
import '../../../../core/usecase/use_case.dart';
import '../../domain/entities/leaderboard.dart';
import '../../domain/use_cases/get_leaderboard_usecase.dart';

abstract class LeaderboardEvent {}

class LoadLeaderboardEvent extends LeaderboardEvent {}

abstract class LeaderboardState {}

class LeaderboardInitial extends LeaderboardState {}

class LeaderboardLoading extends LeaderboardState {}

class LeaderboardLoaded extends LeaderboardState {
  final Leaderboard data;
  LeaderboardLoaded(this.data);
}

class LeaderboardError extends LeaderboardState {
  final String message;
  LeaderboardError(this.message);
}

class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
  final GetLeaderboardUseCase getLeaderboardUseCase;
  LeaderboardBloc(this.getLeaderboardUseCase) : super(LeaderboardInitial()) {
    on<LoadLeaderboardEvent>((event, emit) async {
      emit(LeaderboardLoading());
      final res = await getLeaderboardUseCase(const NoParams());
      if (res is DataSuccess) {
        emit(LeaderboardLoaded(res.data!));
      } else {
        emit(LeaderboardError(res.error!));
      }
    });
  }
}
