import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../library/pagewise/flutter_pagewise.dart';
import '../../app.dart';

class HorizontalScrollableProductsController extends GetxController {
  final Map<String, dynamic> params;

  HorizontalScrollableProductsController({required this.params});

  late PagewiseLoadController<ProductModel> pagewiseLoadController;

  @override
  void onInit() {
    debugPrint('HorizontalScrollableProductsController: onInit()');
    initPageWiseController();
    super.onInit();
  }

  Future<void> initPageWiseController() async {
    // WidgetsBinding.instance?.addPostFrameCallback((_) {
    debugPrint('initPageWiseController');
    pagewiseLoadController = PagewiseLoadController(
      pageFuture: (pageIndex) => fetchProducts(pageIndex! + 1),
      pageSize: Constants.PAGE_SIZE,
    );
    // });
  }

  Future<List<ProductModel>> fetchProducts(int currPage) async {
    debugPrint('fetchProducts');
    final Map<String, dynamic> apiParams = {
      'locale': await getLocale(),
      'currency': 'TMT',
      'page': currPage + 1,
      'limit': Constants.PAGE_SIZE,
      this.params.keys.first: this.params.values.first,
    };

    return await ProductApi.getHomePageProducts(apiParams, params['url']);
  }
}
