import 'package:auto_route/annotations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/features/notificatins/presentation/widgets/notification_list_item.dart';

import '../../../core/presentation/gen/assets.gen.dart';
import '../../../core/presentation/styles/kw_text_styles.dart';
import '../../../core/presentation/styles/theme.dart';
import '../domain/bloc/notifications_bloc.dart';

@RoutePage()
class NotificationsListScreen extends StatefulWidget {
  const NotificationsListScreen({super.key});

  @override
  State<NotificationsListScreen> createState() =>
      _NotificationsListScreenState();
}

class _NotificationsListScreenState extends State<NotificationsListScreen> {
  @override
  void initState() {
    super.initState();
  }

  Future<void> _refreshNotifications() async {
    context.read<NotificationsBloc>().add(NotificationsInitEvent());
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<NotificationsBloc, NotificationsState>(
      builder: (context, state) {
        final notifications = state.notifications;

        final todayNotifications =
            notifications.where((n) => isToday(n.dateTime)).toList();
        final thisWeekNotifications =
            notifications.where((n) => isThisWeek(n.dateTime)).toList();

        return Scaffold(
          appBar: KwAppBar(
            centerTitle: false,
            titleText: 'Alerts',
          ),
          body: RefreshIndicator(
            onRefresh: _refreshNotifications,
            child: Padding(
              padding: const EdgeInsets.only(left: 16, right: 16),
              child: CustomScrollView(
                slivers:state.notifications.isEmpty?[
                  SliverToBoxAdapter(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Gap(100),
                        Assets.images.icons.calendar.svg(height: 36,width: 36, colorFilter: const ColorFilter.mode(AppColors.blackGray, BlendMode.srcIn)),
                        Gap(16),
                        Text(
                          'No notifications yet',
                          style: AppTextStyles.headingH3.copyWith(color: AppColors.blackGray),
                        ),
                      ],
                    ),
                  ),
                ]: [
                  SliverGap(24),
                  if (todayNotifications.isNotEmpty)
                    SliverToBoxAdapter(
                      child: const Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Text(
                          'Today',
                          style: AppTextStyles.bodySmallMed,
                        ),
                      ),
                    ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        final notification = todayNotifications[index];
                        return NotificationListItem(notification: notification);
                      },
                      childCount: todayNotifications.length,
                    ),
                  ),
                  if (thisWeekNotifications.isNotEmpty)
                    SliverToBoxAdapter(
                      child: const Padding(
                        padding: EdgeInsets.only(bottom: 8),
                        child: Text(
                          'This week',
                          style: AppTextStyles.bodySmallMed,
                        ),
                      ),
                    ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        final notification = thisWeekNotifications[index];
                        return NotificationListItem(notification: notification);
                      },
                      childCount: thisWeekNotifications.length,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  bool isToday(DateTime date) {
    final now = DateTime.now();
    return date.year == now.year &&
        date.month == now.month &&
        date.day == now.day;
  }

  bool isThisWeek(DateTime date) {
    final now = DateTime.now();
    final startOfWeek = now.subtract(Duration(days: now.weekday - 1));
    final endOfWeek = startOfWeek.add(Duration(days: 6));
    return date.isAfter(startOfWeek) &&
        date.isBefore(endOfWeek) &&
        !isToday(date);
  }
}
