//Older code
// import 'package:flutter/material.dart';
// import 'package:provider/provider.dart';
// import 'package:test_sa/controllers/notification/firebase_notification_manger.dart';
// import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
// import 'package:test_sa/controllers/providers/api/notifications_provider.dart';
// import 'package:test_sa/controllers/providers/api/user_provider.dart';
// import 'package:test_sa/extensions/context_extension.dart';
// import 'package:test_sa/extensions/text_extensions.dart';
// import 'package:test_sa/extensions/widget_extensions.dart';
// import 'package:test_sa/new_views/app_style/app_color.dart';
// import 'package:test_sa/views/widgets/notifications/notification_item.dart';
//
// class RecentActivitiesFragment extends StatelessWidget {
//   RecentActivitiesFragment({Key? key}) : super(key: key);
//
//   //NotificationsProvider _notificationsProvider;
//
//   @override
//   Widget build(BuildContext context) {
//     //_notificationsProvider ??= Provider.of<NotificationsProvider>(context, listen: false);
//     return Consumer<NotificationsProvider>(builder: (context, _notificationsProvider, _) {
//       return RefreshIndicator(
//         onRefresh: () {
//           Provider.of<AllRequestsProvider>(context, listen: false).getRequests();
//           _notificationsProvider.getSystemNotifications(user: Provider.of<UserProvider>(context, listen: false).user,resetProvider: true);
//           return Future.delayed(const Duration(microseconds: 250));
//         },
//         child: SingleChildScrollView(
//           padding: const EdgeInsets.all(16),
//           child: Container(
//             decoration: ShapeDecoration(
//               color: AppColor.background(context),
//               shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
//               shadows: [boxShadowR14],
//             ),
//             child: Column(
//               mainAxisSize: MainAxisSize.min,
//               crossAxisAlignment: CrossAxisAlignment.start,
//               children: [
//                 context.translation.recentActivities
//                     .heading5(context)
//                     .toShimmer(
//                       isShow: _notificationsProvider.isLoading,
//                     )
//                     .paddingOnly(top: 16, start: 16, end: 16),
//                 ListView.separated(
//                   shrinkWrap: true,
//                   physics: const NeverScrollableScrollPhysics(),
//                   padding: const EdgeInsets.all(16),
//                   itemCount: _notificationsProvider.notifications.length,
//                   separatorBuilder: (context, itemIndex) => const Divider().defaultStyle(context).paddingOnly(top: 16, bottom: 16),
//                   itemBuilder: (context, itemIndex) {
//                     // todo add priority & progress tag to show chip
//                     return NotificationItem(
//                       isLoading: _notificationsProvider.isLoading,
//                       notification: _notificationsProvider.notifications[itemIndex],
//                       onPressed: (notification) {
//                         FirebaseNotificationManger.handleMessage(context, notification.toNotificationJson());
//                       },
//                     );
//                   },
//                 ),
//               ],
//             ),
//           ),
//         ),
//       );
//     });
//   }
// }

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/notification/firebase_notification_manger.dart';
import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/notifications_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/notifications/notification_item.dart';

class RecentActivitiesFragment extends StatelessWidget {
  const RecentActivitiesFragment({Key? key}) : super(key: key);

  //NotificationsProvider _notificationsProvider;

  @override
  Widget build(BuildContext context) {
    //_notificationsProvider ??= Provider.of<NotificationsProvider>(context, listen: false);
    return Consumer<NotificationsProvider>(builder: (context, notificationsProvider, _) {
      UserProvider   userProvider = Provider.of<UserProvider>(context);
      bool isCurrentUserNotEngineer = (userProvider.user!.type != UsersTypes.engineer);
      return RefreshIndicator(
        onRefresh: () {
          Provider.of<AllRequestsProvider>(context, listen: false).getRequests();
          notificationsProvider.getSystemNotifications(user: Provider.of<UserProvider>(context, listen: false).user!,resetProvider: true);
          return Future.delayed(const Duration(microseconds: 250));
        },
        child: SingleChildScrollView(
          // padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: notificationsProvider.notifications.length,
                // separatorBuilder: (context, itemIndex) => const Divider().defaultStyle(context).paddingOnly(top: 16, bottom: 16),
                itemBuilder: (context, itemIndex) {
                  // todo add priority & progress tag to show chip
                  return Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16,vertical: 12),
                    margin:const EdgeInsets.symmetric(vertical: 8),
                    decoration: ShapeDecoration(
                      color: AppColor.background(context),
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                      shadows: [boxShadowR14],
                    ),
                    child: NotificationItem(
                      isLoading: notificationsProvider.isLoading,
                      notification: notificationsProvider.notifications[itemIndex],
                      onPressed: (notification) {
                        FirebaseNotificationManger.handleMessage(context, notification.toNotificationJson());
                      },
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      );
    });
  }
}
