import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/enum/viewstate.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/notifications_service.dart';
import 'package:hmg_patient_app/models/Appointments/toDoCountProviderModel.dart';
import 'package:hmg_patient_app/services/authentication/auth_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../locator.dart';
import 'base_view_model.dart';

class NotificationViewModel extends BaseViewModel {
  NotificationService _notificationService = locator<NotificationService>();
  ToDoCountProviderModel model = Provider.of<ToDoCountProviderModel>(AppGlobal.context);

  List<GetNotificationsResponseModel> get notifications => _notificationService.notificationsList;

  Future getNotifications(GetNotificationsRequestModel getNotificationsRequestModel, BuildContext context) async {
    if (getNotificationsRequestModel.currentPage == 0) setState(ViewState.Busy);

    await _notificationService.getAllNotifications(getNotificationsRequestModel);
    if (_notificationService.hasError) {
      error = _notificationService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future markAsRead(id) async {
    // setState(ViewState.Busy);
    MarkMessageAsReadRequestModel markMessageAsReadRequestModel = new MarkMessageAsReadRequestModel(notificationPoolID: id);
    final authService = new AuthProvider();
    await _notificationService.markAsRead(markMessageAsReadRequestModel);
    await authService.getDashboard().then((value) {
      var notificationCount = '';
      notificationCount = value['List_PatientDashboard'][0]['UnreadPatientNotificationCount'] > 99 ? '99+' : value['List_PatientDashboard'][0]['UnreadPatientNotificationCount'].toString();
      model.setState(model.count, 0, true, notificationCount);
      sharedPref.setString(NOTIFICATION_COUNT, notificationCount);

    });
    setState(ViewState.Idle);
  }
}
