import 'package:boilerplate/data/models/product/get_product_list_req_model.dart';
import 'package:boilerplate/data/repositories/product_repository.dart';
import 'package:boilerplate/domain/entities/product/product_data.dart';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';

import '../../../core/di/injection_init.dart';
import '../../../core/utils/print_utils.dart';
import '../../../core/utils/shared_preferences_util.dart';
import '../../../data/models/app/failure.dart';

@injectable
class GetProductListUseCase {
  final ProductRepository _productRepo;

  GetProductListUseCase(this._productRepo);

  Future<Either<List<ProductData>, Failure>> execute(
      GetProductListReqModel req) async {
    try {
      print(req);
      print(req.sort);
      final data = await _productRepo.getProductLise(req);
      // print("data.result.data ${data.result.data.length}");
      final pref = getIt.get<SharedPreferencesUtil>();
      pref.putInt(SharedPreferencesUtil.keyListCount, 0);

      pref.putInt(SharedPreferencesUtil.keyListCount, data.result.data.length);
      // print(
      //     "aklsdpakspdkasp ${pref.getInt(SharedPreferencesUtil.keyListCount)}");
      return Left(data.result.data);
    } catch (err) {
      print('GetProductListUseCase Error : $err');
      final pref = getIt.get<SharedPreferencesUtil>();

      pref.putInt(SharedPreferencesUtil.keyListCount, 0);

      return Right(Failure(
        errMessage: '$err',
      ));
    }
  }
}
