import 'package:boilerplate/data/data_source/remote/api_provider.dart';
import 'package:boilerplate/data/models/product/get_product_cart_res_model.dart';
import 'package:boilerplate/data/models/product/get_product_detail_res_model.dart';
import 'package:boilerplate/data/models/product/get_product_favorite_res_model.dart';
import 'package:boilerplate/data/models/product/get_product_list_req_model.dart';
import 'package:boilerplate/data/models/product/get_product_list_res_model.dart';
import 'package:boilerplate/data/repositories/base_repository.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

@injectable
class ProductRepository extends BaseRepository {
  final ApiProvider _api;

  ProductRepository(this._api);

  Future<GetProductListResModel> getProductLise(
      GetProductListReqModel req) async {
    // print("parameter =>${req.toJson()}");
    final response =
        await _api.get('/api/product', queryParameters: req.toJson())
          ..responseHandler();
    // print("resAllProduct => ${response.data}");
    return GetProductListResModel.fromJson(response.data);
  }

  Future<GetProductListResModel> getNewProduct(
      GetProductListReqModel req) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    // print(req);
    final response = await _api.get(
        'https://ipmart.ipthailand.go.th/api/product?sort=desc&type=product_new')
      ..responseHandler();
    return GetProductListResModel.fromJson(response.data);
  }

  Future<GetProductDetailResModel> getProductDetail(
    int productId,
  ) async {
    // print("checkID => ${productId}");
    final response = await _api.get('/api/product/$productId')
      ..responseHandler();
    // print("resp => ${response.data}");
    return GetProductDetailResModel.fromJson(response.data);
  }

  Future<bool> addProductFavorite(int userId, int productId) async {
    final response = await _api.post('/api/wishlist/store', body: {
      'user_id': userId,
      'product_id': productId,
    })
      ..responseHandler();

    return response.statusCode == 200;
  }

  Future<bool> removeProductFavorite(int userId, int productId) async {
    final response = await _api.post('/api/wishlist/delete', body: {
      'user_id': userId,
      'product_id': productId,
    })
      ..responseHandler();

    return response.statusCode == 200;
  }

  Future<GetProductFavoriteResModel> getProductFavorites(int userId) async {
    final response = await _api.get('/api/wishlist/${userId}')
      ..responseHandler();

    return GetProductFavoriteResModel.fromJson(response.data);
  }

  Future<bool> addProductCart(int userId, int productId, int storeId) async {
    final response = await _api.post('/api/cart/store', body: {
      'user_id': userId,
      'product_id': productId,
      'store_id': storeId,
    })
      ..responseHandler();

    return response.statusCode == 200;
  }

  Future<bool> removeProductCart(int userId, int productId, int storeId) async {
    final response = await _api.post('/api/cart/delete', body: {
      'user_id': userId,
      'product_id': productId,
      'store_id': storeId,
    })
      ..responseHandler();

    return response.statusCode == 200;
  }

  Future<GetProductCartResModel> getProductCart(int userId) async {
    final response = await _api.get('/api/cart/${userId}')
      ..responseHandler();

    return GetProductCartResModel.fromJson(response.data);
  }
}
