import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:hoshan/data/model/personal_assistants_bots.dart';
import 'package:hoshan/data/repository/bot_repository.dart';

part 'personal_assistants_event.dart';
part 'personal_assistants_state.dart';

class PersonalAssistantsBloc
    extends Bloc<PersonalAssistantsEvent, PersonalAssistantsState> {
  PersonalAssistantsBloc() : super(PersonalAssistantsInitial()) {
    on<PersonalAssistantsEvent>((event, emit) async {
      if (event is GetAll) {
        emit(PersonalAssistantsLoading());
        try {
          final response = await BotRepository.getPersonalAssistant();
          if (response.personalAssistants != null &&
              response.personalAssistants!.isNotEmpty) {
            emit(PersonalAssistantsSuccess(
                personalAssistants: response.personalAssistants!));
          } else {
            emit(PersonalAssistantsEmpty());
          }
        } on DioException catch (e) {
          if (kDebugMode) {
            print('Dio Error is: $e');
          }
        }
      }
    });
  }
}
