import 'dart:io';

import 'package:zhiying_comm/util/net_util.dart';
import 'package:zhiying_comm/util/shared_prefe_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class BaseSettingModel {
  String fileProvider;
  String fileBucketHost;
  String fileBucketHostProtocol;
  String isIosReview;
  Map<String, dynamic> tab;

  static BaseSettingModel _setting;

  static BaseSettingModel get setting {
    if (_setting == null) {
      Logger.error('暂未初始化');
    }
    return _setting;
  }

  BaseSettingModel(
      {this.fileProvider, this.fileBucketHost, this.fileBucketHostProtocol});

  BaseSettingModel.fromJson(Map<String, dynamic> json) {
    fileProvider = json['file_provider'];
    fileBucketHost = json['file_bucket_host'];
    fileBucketHostProtocol = json['file_bucket_host_protocol'];
    isIosReview = json['is_ios_review']?.toString();
    tab = Map<String, dynamic>.from(json['bottom_nav']);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['file_provider'] = this.fileProvider;
    data['file_bucket_host'] = this.fileBucketHost;
    data['file_bucket_host_protocol'] = this.fileBucketHostProtocol;
    data['bottom_nav'] = this.tab;
    data['is_ios_review'] = this.isIosReview;
    return data;
  }

  static Future<BaseSettingModel> init() async {
    Map result =
        await NetUtil.post('/api/v1/new/config.json', method: NetMethod.GET);
    try {
      var data = result['data'];
      _setting = BaseSettingModel.fromJson(Map<String, dynamic>.from(data));
      if(Platform.isIOS) {
        if (null != _setting && _setting.isIosReview == '1') {
          SharedPreferencesUtil.setStringValue(GlobalConfig.IS_IOS_REVIEW, '1');
        } else {
          SharedPreferencesUtil.setStringValue(GlobalConfig.IS_IOS_REVIEW, '0');
        }
      }
      Logger.debug('基础设置初始化');
      return _setting;
    } catch (err) {
      Logger.error(err);
    }
    return null;
  }
}
