import 'dart:convert';

import 'package:zhiying_base_widget/pages/team_details_page/model/team_details_data_model.dart';
import 'package:zhiying_base_widget/pages/team_details_page/model/team_details_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class TeamDetailsRepository {
  TeamDetailsStyleModel _styleModel;
  TeamDetailsDataModel _dataModel;

  /// 初始化数据
  Future<TeamDetailsDataModel> fetchNetData(final Map<String, dynamic> data) async {
    try {
      if (!EmptyUtil.isEmpty(data)) {
        String fansId = data['uid'];
        if (!EmptyUtil.isEmpty(fansId)) {
          var result = await NetUtil.post('/api/v1/user/fan/$fansId', method: NetMethod.GET);
          if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
            _dataModel = TeamDetailsDataModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
            return _dataModel;
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 初始化Style数据
  Future<TeamDetailsStyleModel> fetchNetStyleModelData() async {
    try {
      var result = await NetUtil.post('/api/v1/mod/pub.flutter.my_fan_detail', 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)) {
          _styleModel = TeamDetailsStyleModel.fromJson(jsonDecode(modListData.toString()));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取缓存数据
  Future<TeamDetailsStyleModel> fetchCacheData() async {
    try {
      var result = await NetUtil.getRequestCachedData('/api/v1/mod/pub.flutter.my_fan_detail');
      if (!EmptyUtil.isEmpty(result)) {
        var modListData = result['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          _styleModel = TeamDetailsStyleModel.fromJson(jsonDecode(modListData.toString()));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
