import 'dart:convert';

import 'package:zhiying_comm/pages/empty_page/model/empty_page_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class EmptyPageRepository {
  /// 获取网络数据
  Future<EmptyPageModel> fetchNetData() async {
    try {
      var result = await NetUtil.post('/api/v1/mod/pub.flutter.empty_page', method: NetMethod.GET, cache: true);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        var modListData = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          return EmptyPageModel.fromJson(jsonDecode(modListData));
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取缓存的数据
  Future<EmptyPageModel> fetchCacheData() async {
    try {
      var result = await NetUtil.getRequestCachedData('/api/v1/mod/pub.flutter.empty_page');
      if (!EmptyUtil.isEmpty(result)) {
        var modListData = result['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          return EmptyPageModel.fromJson(jsonDecode(modListData));
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
