import 'package:didvan/config/design_config.dart';
import 'package:didvan/models/mention/mention.dart';
import 'package:didvan/models/comment/user.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/users_mention.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

class MentionsState extends CoreProvier {
  TextEditingController textFieldController = TextEditingController();
  TextEditingController searchUsers = TextEditingController();
  List<UsersMention> mentionedUsers = [];
  int? commentId;
  UserOverview? replyingTo;
  bool showReplyBox = false;
  bool showUsersForMentionsLayout = false;
  bool showPrivates = false;
  bool hideMentionedUser = false;
  late String type;

  final List<MentionData> comments = [];
  final List<UsersMention> users = [];

  int itemId = 0;

  Future<void> getComments() async {
    final service = RequestService(
      RequestHelper.mention(itemId, type),
    );

    await service.httpGet();

    if (service.isSuccess) {
      comments.clear();
      final List<dynamic> messages = service.result['comments'];

      for (var i = 0; i < messages.length; i++) {
        comments.add(MentionData.fromJson(messages[i]));
      }

      appState = AppState.idle;

      return;
    }
    appState = AppState.failed;
  }

  Future<void> getUsersMention(int id) async {
    final service = RequestService(
      RequestHelper.usersMentions(searchUsers.text),
    );

    await service.httpGet();

    if (service.isSuccess) {
      users.clear();
      final List<dynamic> resUsers = service.data('users');

      users.addAll(mentionedUsers);

      for (var i = 0; i < resUsers.length; i++) {
        final user = UsersMention.fromJson(resUsers[i]);

        if (!mentionedUsers.map((e) => e.id).contains(user.id) &&
            user.id != id) {
          users.add(user);
        }
      }

      appState = AppState.idle;
      return;
    }
    appState = AppState.failed;
  }

  Future<void> addMention() async {
    late List<MentionData> cList = comments;
    final user = DesignConfig.context!.read<UserProvider>().user;

    cList.insert(
      0,
      MentionData(
        id: 0,
        user: UserOverview(
          id: user.id,
          fullName: user.fullName,
          photo: user.photo,
        ),
        text: textFieldController.text,
        createdAt: DateTime.now().toString(),
        mentions: mentionedUsers.map((user) => user.name).toList(),
      ),
    );

    final body = {};

    body.addAll({
      'text': textFieldController.text,
      "mentions": mentionedUsers.map((user) => user.id).toList(),
    });

    final service = RequestService(
      RequestHelper.mention(itemId, type),
      body: body,
    );

    await service.post();

    if (service.isSuccess) {
      cList.firstWhere((comment) => comment.id == 0).id =
          service.result['comment']['id'];

      mentionedUsers.clear();
      users.clear();
      searchUsers.text = '';
      textFieldController.text = '';
      showUsersForMentionsLayout = false;

      update();
    }
  }

  void deleteMention(int id) async {
    final service = RequestService(RequestHelper.deleteComment(id));
    await service.delete();

    if (service.isSuccess) {
      comments.removeWhere((element) => element.id == id);
    }

    notifyListeners();
  }
}
