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 'assistant_info_event.dart';
part 'assistant_info_state.dart';

class AssistantInfoBloc extends Bloc<AssistantInfoEvent, AssistantInfoState> {
  AssistantInfoBloc() : super(AssistantInfoInitial()) {
    on<AssistantInfoEvent>((event, emit) async {
      if (event is Getinfo) {
        emit(AssistantInfoLoading());
        try {
          final response = await BotRepository.getAssistantGlobalInfo(event.id);
          emit(AssistantInfoSuccess(assistantInfo: response));
        } on DioException catch (e) {
          emit(AssistantInfoFail());
          if (kDebugMode) {
            print('Dio Error is: $e');
          }
        }
      }
      if (event is ChangeInfo) {
        emit(AssistantInfoLoading());

        final info = event.info;
        emit(AssistantInfoSuccess(assistantInfo: info));
      }
    });
  }
}
