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

part 'tools_event.dart';
part 'tools_state.dart';

class ToolsBloc extends Bloc<ToolsEvent, ToolsState> {
  ToolsBloc() : super(ToolsInitial()) {
    on<ToolsEvent>((event, emit) async {
      if (event is GetAllTools) {
        emit(ToolsLoading());
        try {
          final response = await BotRepository.getToolsCategories();
          if (response.categories == null || response.categories!.isEmpty) {
            emit(ToolsEmpty());
          } else {
            emit(ToolsSuccess(categories: response.categories!));
          }
        } on DioException catch (e) {
          emit(ToolsFail());
          if (kDebugMode) {
            print("Dio Error is : $e");
          }
        }
      }
    });
  }
}
