import 'package:cheminova/models/place_order_list_model.dart';
import '../models/oder_place_model.dart';
import '../utils/common_api_service.dart';
import '../utils/show_snackbar.dart';

class GetOrderPlacedService {
  int _currentPage = 1; // Initialize with the first page
  //int _limit = 100; // Start with a fixed limit, can be adjusted
  final List<PlacedOrderList> _allOrders = []; // To store all fetched orders
  bool _hasMoreOrders = true; // To check if there are more orders to fetch

  Future<List<PlacedOrderList>?> getPlacedOrders() async {
    try {
      while (_hasMoreOrders) {
        // Construct the API URL with pagination parameters
        String url = "/api/get-placed-order-pd?page=$_currentPage";

        final response = await commonApiService<List<PlacedOrderList>>(
          method: "GET",
          url: url,
          fromJson: (json) {
            if (json['plcaedOrders'] != null) {
              final List<PlacedOrderList> orders = (json['plcaedOrders'] as List)
                  .map((orderJson) => PlacedOrderList.fromJson(orderJson as Map<String, dynamic>))
                  .toList();

              if (orders.isNotEmpty) {
                _allOrders.addAll(orders);
                _currentPage++;
               // _limit += orders.length; // Adjust limit based on the number of fetched orders
              } else {
                _hasMoreOrders = false; // Stop fetching if no more orders are returned
              }

              return orders;
            } else {
              _hasMoreOrders = false; // Stop if there are no orders at all
              return [];
            }
          },
        );

        if (response == null || response.isEmpty) {
          _hasMoreOrders = false; // Stop fetching if the response is empty
        }
      }

      return _allOrders;
    } catch (e) {
      showSnackbar(e.toString());
      return null;
    }
  }

  // Optional: Reset the pagination and orders when needed
  void resetPagination() {
    _currentPage = 1;
    //_limit = 100; // Reset the limit to the initial value
    _allOrders.clear(); // Clear the list of orders
    _hasMoreOrders = true; // Reset the flag to allow fetching again
  }






}



//
// import 'package:cheminova/models/place_order_list_model.dart';
//
// import '../models/oder_place_model.dart';
// import '../utils/common_api_service.dart';
// import '../utils/show_snackbar.dart';
//
// class GetOrderPlacedService {
//   int _currentPage = 1; // Initialize with the first page
//   int _limit = 10; // Fixed limit, you can change this as needed
//   final List<PlacedOrderList> _allOrders = [];
//   int? totalOrders ;
//   Future<List<PlacedOrderList>?> getPlacedOrders() async {
//     try {
//       // Construct the API URL with pagination parameters
//       String url = "/api/get-placed-order-pd?page=$_currentPage&limit=$_limit";
//
//       final response = await commonApiService<List<PlacedOrderList>>(
//         method: "GET",
//         url: url,
//         fromJson: (json) {
//           if (json['plcaedOrders'] != null) {
//             final List<PlacedOrderList> orders = (json['plcaedOrders'] as List)
//                 .map((orderJson) => PlacedOrderList.fromJson(orderJson as Map<String, dynamic>))
//                 .toList();
//             // Automatically increase the page number for the next request
//             if (orders.isNotEmpty) {
//               _currentPage++;
//               _limit+= orders.length;
//
//             }
//             return orders;
//           } else {
//             return [];
//           }
//         },
//       );
//
//       return response;
//     } catch (e) {
//       showSnackbar(e.toString());
//       return null;
//     }
//   }
//
//   // Optional: Reset the pagination when needed
//   void resetPagination() {
//     _currentPage = 1;
//     _limit = 100;
//   }
// }
