import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/functions/base_finctions.dart';
import 'package:cargocalculaterapp/core/utils/app_utils.dart';
import 'package:cargocalculaterapp/core/widgets/loading/progress_hud.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../../../../../core/widgets/loading/custom_loading.dart';
import '../../bloc/notification/notification_bloc.dart';
import '../../mixin/notification_mixin.dart';

class NotificationPage extends StatefulWidget {
  const NotificationPage({super.key});

  @override
  State<NotificationPage> createState() => _NotificationPageState();
}

class _NotificationPageState extends State<NotificationPage>
    with NotificationMixin {
  @override
  void initState() {
    context.read<NotificationBloc>().add(const GetNotificationsEvent());
    initControllers();
    super.initState();
    _scrollListener();
  }

  void _scrollListener() {
    scrollController.addListener(() {
      final state = context.read<NotificationBloc>().state;
      if (scrollController.position.pixels >=
              scrollController.position.maxScrollExtent - 200 &&
          !state.paginationLoading &&
          state.currentPage <= (state.pageCount ?? 1)) {
        context.read<NotificationBloc>().add(
          NotificationPaginationEvent(page: state.currentPage + 1),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<NotificationBloc, NotificationState>(
      builder: (context, state) {
        return Scaffold(
          appBar: AppBar(
            title: Text(AppLocalization.current.notification),
            elevation: 0.2,
          ),
          body: ModalProgressHUD(
            inAsyncCall: state.paginationLoading,
            child: CustomScrollView(
              controller: scrollController,
              slivers: [
                SliverPadding(
                  padding: AppUtils.kPaddingAll16,
                  sliver: SliverList.separated(
                    itemBuilder: (context, index) => Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Ink(
                          width: 36,
                          height: 36,
                          decoration: BoxDecoration(
                            color: context.color.iconBackground,
                            borderRadius: AppUtils.kBorderRadius24,
                          ),
                          padding: AppUtils.kPaddingAll8,
                          child: SvgPicture.asset(
                            "assets/svg/ic_check_box.svg",
                          ),
                        ),
                        AppUtils.kBoxWith8,
                        Expanded(
                          child: Text(
                            Functions.getTranslatedNameModel(
                              state.notifications?[index].text,
                              context,
                            ),
                            style: context.text.authDesc,
                          ),
                        ),
                      ],
                    ),
                    separatorBuilder: (_, _) => Padding(
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      child: Divider(
                        thickness: 1,
                        height: 1,
                        color: context.color.lightBorder,
                      ),
                    ),
                    itemCount: state.notifications?.length ?? 0,
                  ),
                ),
                if (state.paginationLoading)
                  const SliverPadding(
                    padding: AppUtils.kPaddingAll24,
                    sliver: SliverToBoxAdapter(
                      child: Center(child: CustomLoadingWidget()),
                    ),
                  ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    disposeControllers();
    super.dispose();
  }
}
