import 'package:chat/data/UserData.dart';
import 'package:chat/models/gift_item_model.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/TokenMgr.dart';
import 'package:dio/dio.dart';

class GiftMgr {
  static GiftMgr _instance;
  static GiftMgr _getInstance() {
    if (_instance == null) {
      _instance = GiftMgr._();
    }
    return _instance;
  }

  factory GiftMgr() => _getInstance();
  GiftMgr._();

  static Map<int, String> giftMap = {};

  Future<List<GiftItemModel>> getGiftList() async {
    Map data = {
      "userId": UserData().basicInfo.userId,
    };
    data['sign'] = TokenMgr().getSign(data);

    Response res = await HttpUtil().post('prop/get/list', data: data);

    if (res == null) {
      return null;
    }

    Map resData = res.data;
    if (resData['code'] == 0) {
      //领取成功
      List<GiftItemModel> giftList = resData['data'].map<GiftItemModel>((v) {
        var model = GiftItemModel.fromJson(v);
        updateGiftMap(model);
        return model;
      }).toList();

      return giftList;
    }

    return null;
  }

  updateGiftMap(GiftItemModel itemModel) {
    giftMap[itemModel.id] = itemModel.name;
  }

  getGiftName(int id) {
    return giftMap[id] ?? '';
  }
}
