import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import '../../../data/dtos/hero_dto.dart';
import '../../../data/repositories/hero_repository.dart';

// Events
abstract class HeroDetailEvent extends Equatable {
  @override
  List<Object> get props => [];
}

class FetchHeroDetails extends HeroDetailEvent {
  final int heroId;

  FetchHeroDetails(this.heroId);

  @override
  List<Object> get props => [heroId];
}

// States
abstract class HeroDetailState extends Equatable {
  @override
  List<Object> get props => [];
}

class HeroDetailInitial extends HeroDetailState {}

class HeroDetailLoading extends HeroDetailState {}

class HeroDetailLoaded extends HeroDetailState {
  final HeroDto hero;

  HeroDetailLoaded(this.hero);

  @override
  List<Object> get props => [hero];
}

class HeroDetailError extends HeroDetailState {
  final String message;

  HeroDetailError(this.message);

  @override
  List<Object> get props => [message];
}

// BLoC
class HeroDetailBloc extends Bloc<HeroDetailEvent, HeroDetailState> {
  final HeroRepository heroRepository;

  HeroDetailBloc(this.heroRepository) : super(HeroDetailInitial()) {
    on<FetchHeroDetails>((event, emit) async {
      emit(HeroDetailLoading());
      try {
        final hero = await heroRepository.getHeroDetails(event.heroId);
        emit(HeroDetailLoaded(hero));
      } catch (e) {
        emit(HeroDetailError(e.toString()));
      }
    });
  }
}
