import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.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/models/subtitle.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/pages/user/notifications/notifications_list.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
class NotificationsPage extends StatefulWidget {
  static final String id = "/notifications";
  @override
  _NotificationsPageState createState() => _NotificationsPageState();
}

class _NotificationsPageState extends State<NotificationsPage>
    with TickerProviderStateMixin{
  NotificationsProvider _notificationsProvider;
  UserProvider _userProvider;
  SettingProvider _settingProvider;
  Subtitle _subtitle;
  @override
  Widget build(BuildContext context) {
    _notificationsProvider = Provider.of<NotificationsProvider>(context);
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    _subtitle = AppLocalization.of(context).subtitle;
    return Scaffold(
      body: SafeArea(
        child: LoadingManager(
          isLoading: _notificationsProvider.isLoading,
          isFailedLoading: _notificationsProvider.notifications == null,
          stateCode: _notificationsProvider.stateCode,
          onRefresh: () async {
            _notificationsProvider.reset();
            await _notificationsProvider.getNotifications(
                user: _userProvider.user,
                host: _settingProvider.host,
                hospitalId: _userProvider.user.hospital.id,
            );
          },
          child: Stack(
            children: [
              Column(
                children: [
                  Container(
                    color:AColors.primaryColor,
                    padding: const EdgeInsets.symmetric(horizontal: 0,vertical: 4),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            ABackButton(),
                            Expanded(
                              child: Center(
                                child: Text(
                                  _subtitle.notifications,
                                  style: Theme.of(context).textTheme.headline6.copyWith(
                                      color: AColors.white,
                                      fontStyle: FontStyle.italic
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(width: 48,)
                          ],
                        ),

                      ],
                    ),
                  ),
                  Expanded(
                    child: NotificationsList(
                      nextPage: _notificationsProvider.nextPage,
                      onLazyLoad: () async {
                        await _notificationsProvider.getNotifications(
                          user: _userProvider.user,
                          host: _settingProvider.host,
                          hospitalId: _userProvider.user.hospital.id,
                        );
                      },
                      notifications: _notificationsProvider.notifications,
                    ),
                  ),
                ],
              ),

            ],
          ),
        ),
      ),
    );
  }
}
