import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class SidebarCommunitiesList extends StatelessWidget {
  const SidebarCommunitiesList({
    required this.communities,
    required this.itemBuilder,
    required this.scrollController,
    required this.onScrollToEnd,
    super.key,
  });

  final List<CommunityModel> communities;
  final Widget Function(BuildContext context, int index) itemBuilder;
  final ScrollController scrollController;
  final void Function() onScrollToEnd;

  bool _onNotification(ScrollEndNotification notification) {
    final hasReachedEnd = notification.metrics.extentAfter == 0;
    if (hasReachedEnd) {
      onScrollToEnd.call();
      return true;
    }

    return false;
  }

  @override
  Widget build(BuildContext context) {
    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: communities.length,
              controller: scrollController,
              itemBuilder: itemBuilder,
            ),
          ),
        ),
      ),
    );
  }
}
