import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class SpaceManagementSidebarCommunitiesList extends StatefulWidget {
  const SpaceManagementSidebarCommunitiesList({
    required this.communities,
    required this.itemBuilder,
    this.onLoadMore,
    this.isLoadingMore = false,
    this.hasNext = false,
    super.key,
  });

  final List<CommunityModel> communities;
  final Widget Function(BuildContext context, int index) itemBuilder;
  final VoidCallback? onLoadMore;
  final bool isLoadingMore;
  final bool hasNext;

  @override
  State<SpaceManagementSidebarCommunitiesList> createState() =>
      _SpaceManagementSidebarCommunitiesListState();
}

class _SpaceManagementSidebarCommunitiesListState
    extends State<SpaceManagementSidebarCommunitiesList> {
  late final ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    _scrollController.addListener(_onScroll);
  }

  void _onScroll() {
    if (_scrollController.position.pixels >=
        _scrollController.position.maxScrollExtent - 100) {
      if (widget.hasNext && !widget.isLoadingMore && widget.onLoadMore != null) {
        widget.onLoadMore!();
      }
    }
  }

  bool _onNotification(ScrollEndNotification notification) {
    final hasReachedEnd = notification.metrics.extentAfter == 0;
    if (hasReachedEnd &&
        widget.hasNext &&
        !widget.isLoadingMore &&
        widget.onLoadMore != null) {
      widget.onLoadMore!();
      return true;
    }

    return false;
  }

  @override
  void dispose() {
    _scrollController
      ..removeListener(_onScroll)
      ..dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final itemCount = widget.communities.length + (widget.isLoadingMore ? 1 : 0);

    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: SizedBox(
        width: context.screenWidth * 0.5,
        child: Scrollbar(
          scrollbarOrientation: ScrollbarOrientation.left,
          thumbVisibility: true,
          controller: _scrollController,
          child: NotificationListener<ScrollEndNotification>(
            onNotification: _onNotification,
            child: ListView.builder(
              shrinkWrap: true,
              padding: const EdgeInsetsDirectional.only(start: 16),
              itemCount: itemCount,
              controller: _scrollController,
              itemBuilder: (context, index) {
                if (index == widget.communities.length) {
                  return const Padding(
                    padding: EdgeInsets.all(16.0),
                    child: Center(
                      child: CircularProgressIndicator(),
                    ),
                  );
                }

                return widget.itemBuilder(context, index);
              },
            ),
          ),
        ),
      ),
    );
  }
}
