import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../api/login_api_client.dart';
import '../../../../controllers/localization/localization.dart';
import '../../../../controllers/providers/api/notifications_provider.dart';
import '../../../../controllers/providers/settings/setting_provider.dart';
import '../../../../controllers/providers/user_provider.dart';
import '../../../../models/subtitle.dart';
import '../../../../models/user.dart';
import '../../../app_style/colors.dart';
import '../../../widgets/buttons/app_back_button.dart';
import '../../../widgets/loaders/loading_manager.dart';
import 'notifications_list.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 ?? false,
          isFailedLoading: _notificationsProvider?.notifications == null,
          stateCode: _notificationsProvider?.stateCode ?? 0,
          onRefresh: () async {
            _notificationsProvider?.reset();
            await _notificationsProvider?.getNotifications(
              user: UserApiClient().user ?? User(),
              host: _settingProvider?.host ?? "",
              hospitalId: UserApiClient().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: UserApiClient().user ?? User(),
                          host: _settingProvider?.host ?? "",
                          hospitalId: UserApiClient().user?.hospital?.id ?? "",
                        );
                      },
                      notifications: _notificationsProvider?.notifications,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
