import 'package:flutter/material.dart';

class Apply {
  int? relation;
  String? applicant, recipient, hello, createdAt;
  String? groupChatId;
  Map<String, dynamic>? setting;

  Apply.fromJson(Map<String, dynamic> json) {
    relation = json['relation'];
    applicant = json['applicant'];
    recipient = json['recipient'];
    hello = json['hello'];
    groupChatId = json['groupChatId'];
    setting = json['setting'];
  }
}

class ApplyList extends ChangeNotifier {
  List<Apply> applyList = [];
  List<String> applicantIds = [];
  int count = 0;

  void init(dynamic json) {
    for (var element in json) {
      Apply apply = Apply.fromJson(element);
      applyList.add(apply);
      applicantIds.add(element['applicant']);
      count += 1;
    }
  }

  void removeAt(int index) {
    applyList.removeAt(index);
    applicantIds.removeAt(index);
    count--;
    notifyListeners();
  }

  void addJson(Map<String, dynamic> json) {
    Apply apply = Apply.fromJson(json);
    int index = applicantIds.indexOf(json['applicant']);
    if (index != -1) {
      applyList.removeAt(index);
      count--;
    }
    applyList.add(apply);
    applicantIds.add(json['applicant']);
    count++;
    notifyListeners();
  }

  void clear() {
    applyList.clear();
    applicantIds.clear();
    count = 0;
  }
}