import 'dart:convert';

import 'package:zhiying_base_widget/pages/privacy_settings_page/model/privacy_settings_data_model.dart';
import 'package:zhiying_base_widget/pages/privacy_settings_page/model/privacy_settings_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class PrivacySettingsRepository {
  // 样式
  PrivacySettingsModel _styleModel;

  // 数据
  PrivacySettingsDataModel _dataModel;

  /// 获取style
  PrivacySettingsModel getStyleModel() {
    return _styleModel;
  }

  /// 获取data
  Future<PrivacySettingsDataModel> fetchNetData() async {
    try {
      var result = await NetUtil.post('/api/v1/settings/privacy_settings', method: NetMethod.GET);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        _dataModel = PrivacySettingsDataModel();
        _dataModel.data = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
        return _dataModel;
      }
    } catch (e, s) {
      Logger.error(e, s);
    }

    return null;
  }

  /// 更新data
  Future<PrivacySettingsDataModel> updateData({String key, bool value}) async {
    try {

      /// 刷新
      _dataModel.data[key] = value ? '1' : '0';

      String hidOrder = _dataModel.data['hid_order'];
      String hidContact = _dataModel.data['hid_contact'];
      var result = await NetUtil.post('/api/v1/settings/privacy_settings', params: {'hid_order': hidOrder, 'hid_contact': hidContact}, method: NetMethod.POST);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        // Map<String, dynamic> data = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
        //
        // data.forEach((key, value) {
        //   _dataModel.data[key] = value;
        // });
        return _dataModel;
      }
    } catch (e, s) {
      Logger.error(e, s);
    }

    try{
      /// 失败
      _dataModel.data[key] = value ? '0' : '1';
    }catch(e){}
    return null;
  }

  /// 返回父页面传进来的数据
  Future<PrivacySettingsModel> fetchParentStyleData(final Map<String, dynamic> data) async {
    try {
      if (!EmptyUtil.isEmpty(data) && !EmptyUtil.isEmpty(data['data'])) {
        if (data['data'] is String) {
          _styleModel = PrivacySettingsModel.fromJson(jsonDecode(data['data']));
          return _styleModel;
        }

        if (data['data'] is Map) {
          _styleModel = PrivacySettingsModel.fromJson(data['data']);
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取网络数据
  Future<PrivacySettingsModel> fetchNetStyleData(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.privacy_settings';
      var result = await NetUtil.post('/api/v1/mod/$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 = PrivacySettingsModel.fromJson(jsonDecode(modListData));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取缓存数据
  Future<PrivacySettingsModel> fetchCacheStyleData(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.privacy_settings';
      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)) {
          _styleModel = PrivacySettingsModel.fromJson(jsonDecode(modListData));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
