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/ai/bots_model.dart';
import 'package:hoshan/data/repository/bot_repository.dart';

part 'same_assistants_event.dart';
part 'same_assistants_state.dart';

class SameAssistantsBloc
    extends Bloc<SameAssistantsEvent, SameAssistantsState> {
  SameAssistantsBloc() : super(SameAssistantsInitial()) {
    on<SameAssistantsEvent>((event, emit) async {
      if (event is GetSameAssistants) {
        emit(SameAssistantsLoading());
        try {
          final cats = await BotRepository.getGlobalAssistant(
              marked: false, categorieId: event.id);
          if (event.botId != null) {
            try {
              cats.categories!.first.bots!.removeWhere(
                (element) => element.id == event.botId,
              );
              emit(SameAssistantsSuccess(bots: cats.categories!.first.bots!));
            } catch (e) {
              emit(SameAssistantsFail());
              if (kDebugMode) {
                print('Dio Error is: $e');
              }
            }
          }
        } on DioException catch (e) {
          emit(SameAssistantsFail());
          if (kDebugMode) {
            print('Dio Error is: $e');
          }
        }
      }
    });
  }
}
