import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/notifications/get_notifications_request_model.dart';
import 'package:hmg_patient_app/core/model/notifications/get_notifications_response_model.dart';
import 'package:hmg_patient_app/core/model/notifications/mark_message_as_read_request_model.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';

class NotificationService extends BaseService {
  List<GetNotificationsResponseModel> notificationsList =[];

  Future getAllNotifications(GetNotificationsRequestModel getNotificationsRequestModel ) async {
    hasError = false;
    await baseAppClient.post(PUSH_NOTIFICATION_GET_ALL_NOTIFICATIONS,
        onSuccess: (dynamic response, int statusCode) {
          if(getNotificationsRequestModel.currentPage ==0)
          notificationsList.clear();
          response['List_GetAllNotificationsFromPool'].forEach((appoint) {
            notificationsList.add(GetNotificationsResponseModel.fromJson(appoint));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: getNotificationsRequestModel.toJson());
  }
  Future markAsRead(MarkMessageAsReadRequestModel markMessageAsReadRequestModel ) async {
    hasError = false;
    await baseAppClient.post(PUSH_NOTIFICATION_SET_MESSAGES_FROM_POOL_AS_READ,
        onSuccess: (dynamic response, int statusCode) {
          updateNotification(markMessageAsReadRequestModel.notificationPoolID);
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: markMessageAsReadRequestModel.toJson());
  }
  
  updateNotification(id) {
   int index = notificationsList.indexWhere((element) => element.id == id);
   notificationsList[index].isRead = true;

  }
}
