
import 'package:provider/provider.dart';
import 'package:twong/providers/message.dart';

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 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;
  }

  Future<List<VipConfig>> getVipConfig() async {
    var resp = await Request.get("user/level/grade");
    var configs = List<VipConfig>();
    for(var item in resp["list"] as List) {
      configs.add(VipConfig.fromJson(item));
    }
    return configs;
  }

  Future<VipInfo> getVipInfo() async {
    var resp = await Request.get("user/vip/info");
    return VipInfo.fromJson(resp);
  }

  Future getCashRecord({int type = 4, int page = 1, int limit = 10}) async {
    var resp = await Request.get("spread/commission/$type?page=$page&limit=$limit");

  }

  Future<List<Message>> getMessage() async {
    var resp = await Request.get("user/mails");
    List<Message> list = List<Message>();
    for(var item in resp as List) {
      list.add(Message.fromJson(item));
    }

    var context = Cache.navigator.currentState.overlay.context;
    Provider.of<MessageModel>(context, listen: false).update(list);
    return list;
  }

  Future<PromotionPeopleData> getPromotionPeople({int grade = 0,
    String keyword = "", int limit = 15, int page = 1, String sort = "" }) async {
    var params = {
      "grade": grade,
      "keyword": keyword,
      "limit": limit,
      "page": page,
      "sort": sort
    };
    var resp = await Request.post("spread/people", params: params);
    return PromotionPeopleData.fromJson(resp);
  }

  Future getPromotionCommission() async {
    var resp = await Request.get("commission");
    return resp;
  }

  Future<List<PromoteRecordList>> getPromotionRecord({int type, int page = 1, int limit = 3}) async {
    var resp = await Request.get("spread/commission/$type?page=$page&limit=$limit");
    var list = List<PromoteRecordList>();
    for(var item in resp as List){
      list.add(PromoteRecordList.fromJson(item));
    }
    return list;
  }

  Future<PromotionOrderData> getPromotionOrder({int page = 1, int limit = 15}) async {
    var params = {
      "page": page,
      "limit": limit
    };
    var resp = await Request.post("spread/order", params: params);
    return PromotionOrderData.fromJson(resp);
  }
  // get user address list
  Future<List<Address>> getAddressList({int page = 1, int limit = 20}) async {
    var resp = await Request.get("address/list?page=$page&limit=$limit");
    List<Address> list = List<Address>();
    for(var item in resp as List) {
      list.add(Address.fromJson(item));
    }
    return list;
  }

  Future editAddress(Address address) async {
    var params = {
      "detail": address.detail,
      "is_default": address.is_default,
      "phone": address.phone,
      "real_name": address.real_name,
      "post_code": address.post_code,
      "address": {
        "province": address.province,
        "city": address.city,
        "city_id": address.city_id,
        "district": address.district
      }
    };
    return Request.post("address/edit", params: params);
  }

  Future delAddress(num id) async {
    return Request.post("address/del", params: { "id": id });
  }
  
  Future<RechargeIndex> getRecharge() async {
    var resp = await Request.get("recharge/index");
    return RechargeIndex.fromJson(resp);
  }
  
  Future recharge({ num id, String price, String type }) async {
    var params = {
      "from": "app",
      "payType": type,
      "price": pragma,
      "rechargeId": id
    };
    var resp = await Request.post("recharge/wechat", params: params);
    return OrderInfo.fromJson(resp);
  }

  Future editUser({ String nickname , String avatar }) async {
    var params = {
      "avatar": avatar == null ? Cache.user.avatar : avatar,
      "nickname": nickname == null ? Cache.user.nickname : nickname
    };
    return await Request.post("user/edit", params: params);
  }
}