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/forum_repository.dart';

part 'comment_like_state.dart';

class CommentLikeCubit extends Cubit<CommentLikeState> {
  CommentLikeCubit() : super(const CommentLikeInitial());

  void getLike(int? like) {
    switch (like) {
      case 0:
        emit(const CommentDisLiked());

        break;
      case 1:
        emit(const CommentLiked());

        break;
      default:
        emit(const CommentLikeInitial());
    }
  }

  void setLiked({required final int id, required int status}) async {
    final oldState = state;
    emit(CommentLikeLoading());
    try {
      await ForumRepository.likedMessage(id: id, status: status);

      switch (status) {
        case 0:
          emit(CommentDisLiked(
              disLike: 1, like: oldState is CommentLikeInitial ? 0 : -1));

          break;
        case 1:
          emit(CommentLiked(
              like: 1, disLike: oldState is CommentLikeInitial ? 0 : -1));

          break;
        default:
          emit(CommentLikeInitial(
              like: oldState is CommentLiked ? -1 : 0,
              disLike: oldState is CommentDisLiked ? -1 : 0));
      }
    } on DioException catch (e) {
      emit(oldState);
      if (kDebugMode) {
        print("Dio Error: $e");
      }
    }
  }
}
