import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/datetime.dart';
import 'package:vitalapp/architecture/utils/sensitive.dart';
import 'package:vitalapp/consts/rpc_enum_labels.dart';
import 'package:vitalapp/store/store.dart';

class PatientDetailState {
  final Rx<PatientDTO> _dto = Rx<PatientDTO>(
    PatientDTO(
      patientName: "",
      patientGender: GenderEnum.Unknown,
      phone: "",
      birthday: DateTime(1900, 1, 1),
      patientAddress: "",
      crowdLabels: null,
      contractedDoctorName: '',
    ),
  );

  /// 居民编号
  String code = "";

  /// 姓名
  String get name {
    if (Store.app.enableEncryptSensitiveInfo) {
      return SensitiveUtils.desensitizeChineseName(_dto.value.patientName!);
    } else {
      return _dto.value.patientName!;
    }
  }

  /// 年龄
  String get age => DataTimeUtils.calculateAge(_dto.value.birthday!);

  /// 手机号
  String get phone {
    if (Store.app.enableEncryptSensitiveInfo) {
      return SensitiveUtils.desensitizeMobilePhone(_dto.value.phone!);
    } else {
      return _dto.value.phone!;
    }
  }

  /// 地址
  String get address {
    if (Store.app.enableEncryptSensitiveInfo) {
      return SensitiveUtils.desensitizeAddress(_dto.value.patientAddress!);
    } else {
      return _dto.value.patientAddress!;
    }
  }

  /// 头像URL
  String? get avatarUrl => null; //TODO:

  /// 性别
  GenderEnum get gender => _dto.value.patientGender;

  /// 性别描述
  String get genderDesc => RpcEnumLabels.gender[gender]!;

  /// 人群分类名称集合
  List<String>? get labelNames => _dto.value.labelNames;

  List<String>? get crowdLabels => _dto.value.crowdLabels;

  /// 签约医生
  String get contractedDoctorName => _dto.value.contractedDoctorName!;

  /// 证件号
  String get cardNo {
    if (_dto.value.cardNo == null) {
      return "";
    }
    if (Store.app.enableEncryptSensitiveInfo) {
      return SensitiveUtils.desensitizeIdCard(_dto.value.cardNo!);
    } else {
      return _dto.value.cardNo!;
    }
  }

  /// 签约状态
  ContractStateEnum get contractState => _dto.value.contractState;

  void updateDto(PatientDTO dto) {
    _dto.value = dto;
  }

  void updateCrowd(List<String> crowdCodes, List<String> crowdNames) {
    final newDto = PatientDTO.fromJson(_dto.value.toJson());
    newDto.crowdLabels = crowdCodes;
    newDto.labelNames = crowdNames;
    _dto.value = newDto;
  }
}
