import 'package:flutter/material.dart';
import 'package:provider/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/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/pages/user/notifications/notifications_list.dart';

import '../../../widgets/loaders/loading_manager.dart';

class NotificationsPage extends StatefulWidget {
  static const String id = "/notifications";

  const NotificationsPage({Key? key}) : super(key: key);

  @override
  _NotificationsPageState createState() => _NotificationsPageState();
}

class _NotificationsPageState extends State<NotificationsPage> with TickerProviderStateMixin {
  late NotificationsProvider _notificationsProvider;
  late UserProvider _userProvider;
  late SettingProvider _settingProvider;

  @override
  Widget build(BuildContext context) {
    _notificationsProvider = Provider.of<NotificationsProvider>(context);
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.notifications),
      body: SafeArea(
        child: LoadingManager(
          isLoading: (_notificationsProvider.isLoading ?? false) && _notificationsProvider.notifications.isEmpty,
          isFailedLoading: _notificationsProvider.notifications == null,
          stateCode: _notificationsProvider.stateCode,
          onRefresh: () async {
            _notificationsProvider.reset();
            await _notificationsProvider.getSystemNotifications(user: _userProvider.user!, resetProvider: true

                // host: _settingProvider.host,
                // hospitalId: _userProvider.user.clientId,
                );
          },
          child: NotificationsList(
            nextPage: _notificationsProvider.nextPage,
            onLazyLoad: () async {
              await _notificationsProvider.getSystemNotifications(
                user: _userProvider.user!,
                // host: _settingProvider.host,
                // hospitalId: _userProvider.user.clientId,
              );
            },
            notifications: _notificationsProvider.notifications,
          ),
        ),
      ),
    );
  }
}
