import 'package:didvan/config/design_config.dart';
import 'package:didvan/models/comment/comment.dart';
import 'package:didvan/models/comment/feedback.dart';
import 'package:didvan/models/comment/reply.dart';
import 'package:didvan/models/comment/user.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/providers/core_provider.dart';
import 'package:didvan/providers/user_provider.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:provider/provider.dart';

class CommentsState extends CoreProvier {
  String text = '';
  int? commentId;
  UserOverview? replyingTo;
  bool showReplyBox = false;
  late void Function(int count) onCommentsChanged;
  int _count = 0;
  late String type;

  final List<CommentData> comments = [];
  final Map<int, MapEntry<bool, bool>> _feedbackQueue = {};

  int itemId = 0;

  Future<void> getComments() async {
    final service = RequestService(
      RequestHelper.comments(itemId, type),
    );
    await service.httpGet();
    if (service.isSuccess) {
      final messages = service.result['comments'];
      for (var i = 0; i < messages.length; i++) {
        comments.add(CommentData.fromJson(messages[i]));
        _count++;
        for (var j = 0; j < messages[i]['replies'].length; j++) {
          _count++;
        }
      }
      appState = AppState.idle;
      return;
    }
    appState = AppState.failed;
  }

  Future<void> feedback(int id, bool like, bool dislike) async {
    _feedbackQueue.addAll({id: MapEntry(like, dislike)});
    Future.delayed(const Duration(milliseconds: 500), () async {
      if (!_feedbackQueue.containsKey(id)) return;
      final service = RequestService(
        RequestHelper.feedback(itemId, id, type),
        body: {
          'like': _feedbackQueue[id]!.key,
          'dislike': _feedbackQueue[id]!.value,
        },
      );
      await service.put();
      _feedbackQueue.remove(id);
    });
  }

  Future<void> addComment() async {
    final user = DesignConfig.context!.read<UserProvider>().user;
    if (replyingTo != null) {
      comments.firstWhere((comment) => comment.id == commentId).replies.add(
            Reply(
              id: 0,
              text: text,
              createdAt: DateTime.now().toString(),
              liked: false,
              disliked: false,
              feedback: const FeedbackData(like: 0, dislike: 0),
              toUser: replyingTo!,
              user: UserOverview(
                id: user.id,
                fullName: user.fullName,
                photo: user.photo,
              ),
            ),
          );
    } else {
      comments.insert(
        0,
        CommentData(
          id: 0,
          text: text,
          createdAt: DateTime.now().toString(),
          liked: false,
          disliked: false,
          feedback: const FeedbackData(like: 0, dislike: 0),
          user: UserOverview(
            id: user.id,
            fullName: user.fullName,
            photo: user.photo,
          ),
          replies: [],
        ),
      );
    }

    _count++;
    onCommentsChanged(_count);

    final body = {};

    if (commentId != null) {
      body.addAll({'commentId': commentId});
    }
    if (replyingTo != null) {
      body.addAll({'replyUserId': replyingTo!.id});
    }

    showReplyBox = false;
    update();
    body.addAll({'text': text});
    final service = RequestService(
      RequestHelper.addComment(itemId, type),
      body: body,
    );

    await service.post();
    if (service.isSuccess) {
      if (replyingTo != null) {
        comments
            .firstWhere((comment) => comment.id == commentId)
            .replies
            .firstWhere((reply) => reply.id == 0)
            .id = service.result['comment']['id'];
      } else {
        comments.firstWhere((comment) => comment.id == 0).id =
            service.result['comment']['id'];
      }
      commentId = null;
      replyingTo = null;
    }
  }
}
