import 'package:zhiying_comm/zhiying_comm.dart';

class SearchRepository {
  List<Map<String, dynamic>> _pageData = [];

  /// 获取网络数据
  Future<List<Map<String, dynamic>>> fetchInit(final Map<String, dynamic> data) async {
    try {
      String skipIdentifier = !EmptyUtil.isEmpty(data) && data.containsKey(GlobalConfig.SKIP_IDENTIFIER) && !EmptyUtil.isEmpty(data[GlobalConfig.SKIP_IDENTIFIER])
          ? data[GlobalConfig.SKIP_IDENTIFIER]
          : 'pub.flutter.search_index';

      var result = await NetUtil.post('/api/v1/mod/$skipIdentifier', method: NetMethod.GET, cache: true);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result)) {
        _pageData = List.from(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list']);
        return _pageData;
      }
    } catch (e, s) {
      Logger.error(e.toString(), s);
    }
    return null;
  }

  /// 获取缓存数据
  Future<List<Map<String, dynamic>>> fetchCachedData(final Map<String, dynamic> data) async {
    try {
      String skipIdentifier = !EmptyUtil.isEmpty(data) && data.containsKey(GlobalConfig.SKIP_IDENTIFIER) && !EmptyUtil.isEmpty(data[GlobalConfig.SKIP_IDENTIFIER])
          ? data[GlobalConfig.SKIP_IDENTIFIER]
          : 'pub.flutter.search_index';

      var result = await NetUtil.getRequestCachedData('/api/v1/mod/$skipIdentifier');
      if (!EmptyUtil.isEmpty(result)) {
        return List.from(result['mod_list']);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }


}
