import 'dart:convert';

import 'package:zhiying_base_widget/pages/message_settings_page/model/message_settings_data_model.dart';
import 'package:zhiying_base_widget/pages/message_settings_page/model/message_settings_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MessageSettingsRepository {
  // 样式
  MessageSettingsStyleModel _styleModel;

  // 数据
  MessageSettingsDataModel _dataModel;

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

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

    return null;
  }

  /// 更新data
  Future<MessageSettingsDataModel> updateData({String key, bool value}) async {
    try {
      /// 刷新
      _dataModel.data[key] = value ? '1' : '0';

      String newMsgNotice = _dataModel.data['new_msg_notice'];
      var result = await NetUtil.post('/api/v1/settings/message_settings', params: {'new_msg_notice': newMsgNotice}, method: NetMethod.POST);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        return _dataModel;
      }
    } catch (e, s) {
      Logger.error(e, s);
    }

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

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

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

  /// 获取网络数据
  Future<MessageSettingsStyleModel> 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 = MessageSettingsStyleModel.fromJson(jsonDecode(modListData));
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

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