import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/widgets/home/home_banner/model/HomeBannerModel.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_comm/util/net_util.dart';

class HomeBannerRepository {
  /// 读取缓存数据
  Future<HomeBannerModel> fetchCacheData({@required Map<String, dynamic> model}) async {
    var result = await NetUtil.getRequestCachedData('/api/v1/mod', params: {
      'ids': [model['mod_id']]
    });
    if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])){
      return HomeBannerModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
    }
    return null;
  }


  /// 获取父类传进来的数据
  Future<HomeBannerModel> fetchParentData({@required Map<String, dynamic> model}) async{
    try {
      String jsonInfo = model['data'];
      if (!EmptyUtil.isEmpty(jsonInfo)) {
        return HomeBannerModel.fromJson(jsonDecode(jsonInfo));
      }
    }catch(e){
      Logger.log(e);
    }
    return null;
    
  }

  /// 获取网路数据
  Future<HomeBannerModel> fetchNetData({@required Map<String, dynamic> model}) async {
    // print('请求 modId = $model['mod_id']');
    var result = await NetUtil.post('/api/v1/mod', params: {'ids': [model['mod_id']]}, cache: true);
    if (NetUtil.isSuccess(result)) {
      return HomeBannerModel.fromJson(result);
    }
    return null;
  }
}
