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

part 'bookmark_bot_state.dart';

class BookmarkBotCubit extends Cubit<BookmarkBotState> {
  BookmarkBotCubit() : super(BookmarkBotInitial());

  void getBotBookMarkStstus(bool bookMark) {
    emit(bookMark ? BookmarkedBot() : UnBookMarkedBot());
  }

  void setBotBookMarkStstus({required final int id}) async {
    final oldState = state;
    emit(BookmarkBotLoading());
    try {
      await BotRepository.botMark(
          id: id, marked: oldState is BookmarkedBot ? false : true);
      emit(oldState is BookmarkedBot ? UnBookMarkedBot() : BookmarkedBot());
    } on DioException catch (e) {
      emit(oldState);
      if (kDebugMode) {
        print('Dio Error is: $e');
      }
    }
  }
}
