import 'dart:convert';

import 'package:zhiying_base_widget/pages/team_page/model/team_data_model.dart';
import 'package:zhiying_base_widget/pages/team_page/model/team_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class TeamRepository {
  TeamStyleModel _styleModel;
  TeamDataModel _dataModel;

  /// 获取网络style
  Future<dynamic> fetchNetStyleData(final Map<String, dynamic> data) async {
    try {
      var result = await NetUtil.post('/api/v1/mod/${data['skip_identifier']}', 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)) {
          _styleModel = TeamStyleModel.fromJson(jsonDecode(modListData.toString()));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.log('e = $e, s = $s');
    }
    return null;
  }

  /// 获取网络数据
  Future<TeamDataModel> fetchNetData() async {
    var data = await NetUtil.post('/api/v1/user/myteam', method: NetMethod.GET);
    try {
      if (NetUtil.isSuccess(data) && !EmptyUtil.isEmpty(data[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        _dataModel = TeamDataModel.fromJson(data[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
        return _dataModel;
      }
    } catch (e, s) {
      Logger.log('e = $e, s = $s');
    }
    return null;
  }

  /// 获取缓存的数据
  Future<dynamic> fetchCacheStyleData(final Map<String, dynamic> data) async {
    try {
      var cache = await NetUtil.getRequestCachedData('/api/v1/mod/${data['skip_identifier']}');
      if (!EmptyUtil.isEmpty(cache)) {
        var modListData = cache['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          _styleModel = TeamStyleModel.fromJson(jsonDecode(modListData.toString()));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.log('e = $e, s = $s');
    }
    return null;
  }
}
