
import 'index.dart';
import '../utils/index.dart';
import '../models/index.dart';

extension UserAPI on Network {
  // get user balance
  Future<Balance> getBalance() async {
    var resp = await Request.get('user/balance');
    return Balance.fromJson(resp);
  }
  // get user address list
  Future<List<Address>> getAddress() async {
    var resp = await Request.get('address/list');
    List<dynamic> list = resp as List<dynamic>;
    List<Address> result = List<Address>();
    for(var address in list) {
      result.add(Address.fromJson(address));
    }
    return result;
  }
  // get user cart list
  Future<CartList> getCartList() async {
    var resp = await Request.get("cart/list");
    return CartList.fromJson(resp);
  }
  Future cartNum({int id, int number}) async {
    var data = {
      "id": id,
      "number": number
    };
    return Request.post("cart/num", params: data);
  }
  // add product ro cart
  Future<dynamic> addCart({dynamic cartNum = 1, dynamic news = 1, dynamic productId, String uniqueId}) async {
    var data = {
      "cartNum" : cartNum,
      "new": news,
      "productId": productId,
      "uniqueId": uniqueId
    };
    var resp = await Request.post("cart/add", params: data);
    var cart = resp as Map<String, dynamic>;
    return cart['cartId'];
  }
  // add a product to collect
  Future addCollect(dynamic id) async {
    var params = {
      "id": id,
      "category": 'product'
    };
    return await Request.post('collect/add', params: params);
  }
  // remove a product from collect
  Future delCollect(dynamic id) async {
    var params = {
      "id": id,
      "category": 'product'
    };
    return await Request.post('collect/del', params: params);
  }
  // get user collect list
  Future<List<Collect>> getCollect ({dynamic page = "1", dynamic limit = "20"}) async {
    var resp = await Request.get('collect/user?page=$page&limit=$limit');

    List<Collect> list = List<Collect>();
    var collects = resp as List;
    for(var item in collects) {
      list.add(Collect.fromJson(item));
    }
    return list;
  }
}