import 'dart:convert';

import 'package:zhiying_base_widget/pages/favorites_page/model/favorites_style_model.dart';
import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class FavoritesRepository {
  /// 请求网络样式
  Future<FavoritesStyleModel> fetchNetStyle(final Map<String, dynamic> data) async {
    try {
      String skip_identifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.my_fav';
      var result = await NetUtil.post('/api/v1/mod/$skip_identifier', cache: true, method: NetMethod.GET);
      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 FavoritesStyleModel.fromJson(jsonDecode(modListData));
        }
      }
    } catch (e, s) {
      Logger.log(e, s);
    }
    return null;
  }

  /// 获取缓存样式
  Future<FavoritesStyleModel> fetchCachedStyle(final Map<String, dynamic> data) async {
    try {
      String skip_identifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.my_fav';
      var result = await NetUtil.getRequestCachedData('/api/v1/mod/$skip_identifier');
      if (!EmptyUtil.isEmpty(result)) {
        var modListData = result['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          return FavoritesStyleModel.fromJson(jsonDecode(modListData));
        }
      }
    } catch (e, s) {
      Logger.log(e, s);
    }
    return null;
  }
}
