import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

enum ListType { builder, separated }

class RPaginatedListView<T> extends StatelessWidget {
  const RPaginatedListView({
    super.key,
    required this.resource,
    required this.itemBuilder,
    required this.itemCount,
    this.separatorBuilder,
    required this.onLoadMore,
    this.isPaginating = false,
    this.hasMore = true,
    this.loadingWidget,
    this.emptyWidget,
    this.errorWidget,
    this.scrollController,
    this.padding = const EdgeInsets.all(8.0),
    this.listType = ListType.builder,
    this.physics = const BouncingScrollPhysics(),
  });

  final Resource<PaginationModel<T>> resource;
  final NullableIndexedWidgetBuilder itemBuilder;
  final IndexedWidgetBuilder? separatorBuilder;
  final Future<void> Function() onLoadMore;
  final bool isPaginating;
  final bool hasMore;
  final int itemCount;
  final Widget? loadingWidget;
  final Widget? emptyWidget;
  final Widget? errorWidget;
  final ScrollController? scrollController;
  final ListType listType;
  final EdgeInsets padding;
  final ScrollPhysics physics;

  @override
  Widget build(BuildContext context) {
    /*   if (resource.isLoading) {
      return loadingWidget ?? RShimmerList(isSeparated: listType == ListType.separated);
    }*/

    if (resource.isLoading) {
      return loadingWidget ?? LoadingWidget();
    }

    if (resource.isError) {
      return errorWidget ?? Center(child: Text(resource.message ?? 'خطا'));
    }

    if (resource.isEmpty || resource.data?.results?.isEmpty == true) {
      return emptyWidget ?? EmptyWidget();
    }

    final controller = scrollController ?? ScrollController();

    return NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification scrollInfo) {
        if (!isPaginating &&
            hasMore &&
            scrollInfo.metrics.pixels >= scrollInfo.metrics.maxScrollExtent - 100) {
          onLoadMore();
        }
        return false;
      },
      child: listType == ListType.separated
          ? ListView.separated(
              padding: padding,
              controller: controller,
              shrinkWrap: true,
              itemCount: itemCount + (isPaginating ? 1 : 0),
              itemBuilder: (context, index) {
                if (isPaginating && index == itemCount) {
                  return SizedBox(
                    height: 50,
                    child: const Padding(
                      padding: EdgeInsets.all(16),
                      child: Center(child: CupertinoActivityIndicator()),
                    ),
                  );
                }
                return itemBuilder(context, index);
              },
              separatorBuilder: separatorBuilder ?? (_, __) => const SizedBox(height: 8),
            )
          : ListView.builder(
              padding: padding,
              controller: controller,
              shrinkWrap: true,
              itemCount: itemCount + (isPaginating ? 1 : 0),
              itemBuilder: (context, index) {
                if (isPaginating && index == itemCount) {
                  return SizedBox(
                    height: 50,
                    child: const Padding(
                      padding: EdgeInsets.all(16),
                      child: Center(child: CupertinoActivityIndicator()),
                    ),
                  );
                }
                return itemBuilder(context, index);
              },
            ),
    );
  }
}
