import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/utils/network/resource.dart';

import 'r_shimmer_list.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,
    this.onRefresh,
    required this.onLoadMore,
    this.isPaginating = false,
    this.hasMore = true,
    this.loadingWidget,
    this.emptyWidget,
    this.errorWidget,
    this.scrollController,
    this.listType = ListType.builder,
  });

  final Resource<List<T>> resource;
  final NullableIndexedWidgetBuilder itemBuilder;
  final IndexedWidgetBuilder? separatorBuilder;
  final Future<void> Function()? onRefresh;
  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;

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

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

    if (resource.isEmpty || resource.data?.isEmpty == true) {
      return emptyWidget ?? const Center(child: Text('آیتمی یافت نشد'));
    }

    final controller = scrollController ?? ScrollController();

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