import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/widgets/custom/slide_banner/bloc/bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

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

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

  /// 获取数据
  Future<CustomSlideBannerModel> 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 CustomSlideBannerModel.fromJson(params[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      } catch (e) {}
    }
    return null;
  }
}
