import 'dart:convert';

import 'package:zhiying_comm/zhiying_comm.dart';

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

  /// 获取网络数据
  Future<List<Map<String, dynamic>>> fetchInit(final Map<String, dynamic> model) async {
    String keywords = model.containsKey('keywords') ? model['keywords'] : '';
    String type = model.containsKey('type') ? model['type'] : 'taobao';
    var result = await NetUtil.post('/api/v1/mod/pub.flutter.search_result', method: NetMethod.GET, cache: true);
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result)) {
      try {
        List<Map<String, dynamic>> modList = List.from(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list']);
        modList.forEach((Map<String, dynamic> item) {
          var data = jsonDecode(item['data']);
          data['keywords'] = keywords;
          data['type'] = type;
          item['data'] = jsonEncode(data);
          _pageData.add(item);
        });

        return _pageData;
      } catch (e) {
        Logger.error(e.toString());
      }
    }
    return null;
  }

  /// 获取缓存数据
  Future<List<Map<String, dynamic>>> fetchCachedData(final Map<String, dynamic> model) async {
    String keywords = model.containsKey('keywords') ? model['keywords'] : '';
    String type = model.containsKey('type') ? model['type'] : 'taobao';
    var result = await NetUtil.getRequestCachedData('/api/v1/mod/pub.flutter.search_result');
    try {
      if (!EmptyUtil.isEmpty(result)) {
        // return List.from(result['mod_list']);
        List<Map<String, dynamic>> modList = List.from(result['mod_list']);
        List<Map<String, dynamic>> _pageData = [];

        modList.forEach((Map<String, dynamic> item) {
          var data = jsonDecode(item['data']);
          data['keywords'] = keywords;
          data['type'] = type;
          item['data'] = jsonEncode(data);
          _pageData.add(item);
        });
        return _pageData;
      }
    } catch (e) {}
    return null;
  }

  /// 搜索的方法
  Future<void> fetchSearchSubmit(final String model) async {
    var result = await NetUtil.post('');
  }
}
