import 'package:fis_jsonrpc/rpc.dart';
import 'package:vnoteapp/rpc.dart';
import 'package:vnoteapp/store/store.dart';
import 'package:fis_common/logger/logger.dart';

import 'interfaces/doctor.dart';

class DoctorManager implements IDoctorManager {
  @override
  Future<void> syncInfo() async {
    try {
      final userInfo = await rpc.user.getUserDetailAsync(
        GetUserRequest(token: Store.user.token),
      );
      await Store.user.updateUserInfo(userInfo);
    } catch (e) {
      logger.e("DoctorManager sync user info error.", e);
    }
  }

  @override
  Future<bool> setSignature(String signature) async {
    try {
      final request = SetSignatureRequest(
        token: Store.user.token,
        code: Store.user.userCode,
        signature: signature,
      );
      final result = await rpc.user.setSignatureAsync(request);
      if (result) {
        // 更新全局状态
        await Store.user.updateSignature(signature);
      }
      return result;
    } catch (e) {
      logger.e("DoctorManager set signature error.", e);
      return false;
    }
  }
}
