import 'package:flutter/material.dart';

import '../../../controllers/localization/localization.dart';
import '../../../models/service_request/service_request.dart';
import '../../../models/subtitle.dart';
import '../../pages/user/requests/request_details.dart';
import '../loaders/lazy_loading.dart';
import '../loaders/no_item_found.dart';
import 'service_request_item.dart';

class ServiceRequestsList extends StatelessWidget {
  final List<ServiceRequest> requests;
  final bool nextPage;
  final Future<void> Function() onLazyLoad;

  const ServiceRequestsList({
    Key? key,
    required this.requests,
    required this.nextPage,
    required this.onLazyLoad,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Subtitle? subtitle = AppLocalization.of(context)?.subtitle;
    if (requests.isEmpty) {
      return NoItemFound(message: subtitle?.noServiceRequestFound);
    }
    return LazyLoading(
      nextPage: nextPage,
      onLazyLoad: onLazyLoad,
      onLoadingEnd: () {},
      child: ListView.builder(
        //physics: BouncingScrollPhysics(),
        itemCount: requests.length,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        itemBuilder: (context, itemIndex) {
          return ServiceRequestItem(
            index: itemIndex,
            request: requests[itemIndex],
            onPressed: (request) {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (_) => RequestDetailsPage(
                    serviceRequest: request,
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}
