/// Created by Haoyi on 2021/7/28

part of '../firestore_manager.dart';

extension AccountExtension on FirestoreManager {
  String get userCollection {
    if (kReleaseMode) {
      return "users";
    } else {
      return "test_users";
    }
  }

  Future<AccountProfile?> modifyProfile(Map<String, dynamic> modifyJson) async {
    final uid = AccountDataStore.instance.uid;
    if (uid == null || modifyJson.isEmpty) {
      Log.i("modifyProfile error! uid is null!");
      return null;
    }
    modifyJson.remove(AccountProfile.dirtyField);
    // if (Settings.instance.debugMode.get() != true) {
    await FirebaseFirestore.instance
        .collection(userCollection)
        .doc(uid)
        .set(modifyJson, SetOptions(merge: true /*mergeFields: modifyJson.keys.toList()*/));
    // }
    modifyJson[AccountProfile.dirtyField] = false;
    return AccountDataStore.instance.accountProfile?.merge(modifyJson) ??
        AccountProfile.fromJson(modifyJson);
  }
}
