import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/widgets/home/home_slide_banner/model/home_slide_banner_model.dart';
import 'package:zhiying_comm/util/empty_util.dart';
import 'package:zhiying_comm/util/global_config.dart';
import 'package:zhiying_comm/util/net_util.dart';

class HomeSlideBannerRepository {
  /// 获取缓存数据
  Future<HomeSlideBannerModel> fetchCachedDate({@required int id}) async {
    var cached = await NetUtil.getRequestCachedData('/api/v1/mod', params: {
      'ids': [id]
    });
    if (!EmptyUtil.isEmpty(cached)) {
      try {
        return HomeSlideBannerModel.fromJson(cached);
      }catch(e){
      }

    }
    return null;
  }

  /// 获取父页面传进来的数据
  Future<HomeSlideBannerModel> fetchPreantData(@required Map<String, dynamic> model) async{
    try{
      if(!EmptyUtil.isEmpty(model)){
        return HomeSlideBannerModel.fromJson(jsonDecode(model['data']));
      }
    }catch(e){
    }
    return null;
  }

  /// 获取数据
  Future<HomeSlideBannerModel> fetchData({@required int id}) async {
    var params = await NetUtil.post('/api/v1/mod',
        params: {
          'ids': [id]
        },
        cache: true);
    if (NetUtil.isSuccess(params) && !EmptyUtil.isEmpty(params[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      try{
        return  HomeSlideBannerModel.fromJson(params[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }catch(e){
      }
    }
    return null;
  }
}
