import 'package:zhiying_comm/zhiying_comm.dart';

class CustomPageRepository {
  Map<String, dynamic> data;

  CustomPageRepository({this.data});

  /// 初始化
  Future<List<Map<String, dynamic>>> fetchInitData() async {
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey(GlobalConfig.SKIP_IDENTIFIER) && !EmptyUtil.isEmpty(data[GlobalConfig.SKIP_IDENTIFIER]) ? data[GlobalConfig.SKIP_IDENTIFIER] : null;
      // String skipIdentifier = 'pub.flutter.index.84';
      if (!EmptyUtil.isEmpty(skipIdentifier)) {
        var result = await NetUtil.post('/api/v1/mod/$skipIdentifier', method: NetMethod.GET, cache: true);
        if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
          List mobList = List.from(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list']);
          if (!EmptyUtil.isEmpty(mobList)) {
             return mobList.map((e) => Map<String, dynamic>.from(e)).toList();
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 缓存数据
  Future<List<Map<String, dynamic>>> fetchCacheData() async {
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey(GlobalConfig.SKIP_IDENTIFIER) && !EmptyUtil.isEmpty(data[GlobalConfig.SKIP_IDENTIFIER]) ? data[GlobalConfig.SKIP_IDENTIFIER] : null;
      // String skipIdentifier = 'pub.flutter.index.84';
      if (!EmptyUtil.isEmpty(skipIdentifier)) {
        var result = await NetUtil.getRequestCachedData('/api/v1/mod/$skipIdentifier');
        if (!EmptyUtil.isEmpty(result)) {
          List mobList = result['mod_list'];
          if (!EmptyUtil.isEmpty(mobList)) {
            return mobList.map((e) => Map<String, dynamic>.from(e)).toList();
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
