import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:initial_folder/models/notification.dart';
import 'package:initial_folder/services/notification_service.dart';

enum resultState { Loading, NoData, HasData, Error }

enum countState { Loading, NoData, HasData, Error }

class NotificationProvider with ChangeNotifier {
  final NotificationServices notificationServices;
  NotificationProvider({required this.notificationServices}) {
    getMyNotification();
  }

  List<NotificationData> _notificationData = [];
  List<NotificationDataAnnouncementUser> _notificationDataAnnouncementUser = [];

  resultState? _state;
  countState? _cState;

  int _notificationCount = 0;
  String _message = '';

  List<NotificationData> get result => _notificationData;
  List<NotificationDataAnnouncementUser> get resultAnnouncement =>
      _notificationDataAnnouncementUser;

  int get notificationCount => _notificationCount;
  String get message => _message;

  resultState? get state => _state;
  countState? get cState => _cState;

  changeIsRead(List<NotificationData> list, int index) {
    list[index].isRead = "1";
    if (_notificationCount > 0) {
      _notificationCount--;
    }
    notifyListeners();
  }

  changeAllRead() {
    if (_state == resultState.HasData) {
      for (final notif in _notificationData) {
        notif.isRead = "1";
      }
      _notificationCount = 0;
      notifyListeners();
    }
  }

  Future<dynamic> getMyNotification() async {
    try {
      _state = resultState.Loading;
      notifyListeners();
      var notifications = await notificationServices.getNotification();
      List<NotificationData> myNotification = notifications[0];
      List<NotificationDataAnnouncementUser> myNotificationAnnouncement =
          notifications[1];

      if (myNotification.isEmpty && myNotificationAnnouncement.isEmpty) {
        _state = resultState.NoData;
        notifyListeners();
        return _message = 'Empty Data';
      } else {
        _state = resultState.HasData;
        _notificationData = myNotification;
        _notificationDataAnnouncementUser = myNotificationAnnouncement;
        _notificationData
            .sort((a, b) => b.timestamps!.compareTo(a.timestamps!));
        _notificationDataAnnouncementUser
            .sort((a, b) => b.timestamps!.compareTo(a.timestamps!));
        _notificationCount =
            _notificationData.where((notif) => notif.isRead == "0").length;
        notifyListeners();
        return [_notificationData, _notificationDataAnnouncementUser];
      }
    } catch (e, stacktrace) {
      print(stacktrace.toString());
      _state = resultState.Error;
      notifyListeners();
      return _message = 'Error --> $e';
    }
  }

  Future markAsRead() async {
    try {
      await notificationServices.readAllNotification();
      notifyListeners();
    } catch (e) {
      if (kDebugMode) {
        print("Error: --> $e");
      }
    }
  }

  Future getNotificationCount() async {
    try {
      _cState = countState.Loading;
      notifyListeners();

      int notificationCount =
          _notificationData.where((notif) => notif.isRead == "0").length;
      if (notificationCount == 0) {
        _cState = countState.NoData;
        _notificationCount = 0;
        notifyListeners();
      } else {
        _cState = countState.HasData;
        _notificationCount = notificationCount;
        notifyListeners();
      }
    } catch (e) {
      _cState = countState.Error;
      notifyListeners();
      return _message = 'Error --> $e';
    }
  }

  decreaseNotificationCount() {
    if (_notificationCount > 0) {
      _notificationCount--;
    }
    notifyListeners();
  }
}
