/// 用户能力权限模型
class CapabilityMap {
  final Map<String, bool> _capabilities;

  CapabilityMap(this._capabilities);

  factory CapabilityMap.fromJson(Map<String, dynamic> json) {
    final map = <String, bool>{};
    json.forEach((key, value) {
      if (value is bool) {
        map[key] = value;
      }
    });
    return CapabilityMap(map);
  }

  /// 默认全部开启
  factory CapabilityMap.defaultAll() {
    return CapabilityMap({
      'LOGIN': true,
      'TRADING': true,
      'C2C': true,
      'TRANSFER_IN': true,
      'TRANSFER_OUT': true,
      'P2P_SEND': true,
      'P2P_RECEIVE': true,
      'MINING_CLAIM': true,
      'KYC': true,
      'PROFILE_EDIT': true,
      'VIEW_ASSET': true,
      'VIEW_TEAM': true,
      'VIEW_RECORDS': true,
    });
  }

  bool isEnabled(String capability) => _capabilities[capability] ?? true;

  bool get loginEnabled => isEnabled('LOGIN');
  bool get tradingEnabled => isEnabled('TRADING');
  bool get c2cEnabled => isEnabled('C2C');
  bool get transferInEnabled => isEnabled('TRANSFER_IN');
  bool get transferOutEnabled => isEnabled('TRANSFER_OUT');
  bool get p2pSendEnabled => isEnabled('P2P_SEND');
  bool get p2pReceiveEnabled => isEnabled('P2P_RECEIVE');
  bool get miningClaimEnabled => isEnabled('MINING_CLAIM');
  bool get kycEnabled => isEnabled('KYC');
  bool get profileEditEnabled => isEnabled('PROFILE_EDIT');
  bool get viewAssetEnabled => isEnabled('VIEW_ASSET');
  bool get viewTeamEnabled => isEnabled('VIEW_TEAM');
  bool get viewRecordsEnabled => isEnabled('VIEW_RECORDS');

  Map<String, dynamic> toJson() => Map<String, dynamic>.from(_capabilities);
}
