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/global.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: '',
    ),
  );
  final Rx<List<List<String>>> _currentDiagnosis = Rx<List<List<String>>>([]);
  final RxBool _isExistLocalData = RxBool(false);

  // final Rx<Map<String, dynamic>> _diagnosisDataValue = Rx({});

  List<List<String>> get currentDiagnosis => _currentDiagnosis.value;
  set currentDiagnosis(List<List<String>> val) => _currentDiagnosis(val);

  bool get isExistLocalData => _isExistLocalData.value;
  set isExistLocalData(bool val) => _isExistLocalData(val);

  final RxBool _isOnline = RxBool(kIsOnline);

  /// 当前在线状态
  bool get isOnline => _isOnline.value;
  set isOnline(bool val) => _isOnline.value = val;

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

  /// 姓名
  String get name {
    return _dto.value.patientName!;
  }

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

  /// 手机号
  String get phone {
    return _dto.value.phone!;
  }

  /// 地址
  String get address {
    return _dto.value.patientAddress ?? '';
  }

  /// 头像URL
  String? get avatarUrl {
    if (_dto.value.photos != null && _dto.value.photos!.isNotEmpty) {
      return _dto.value.photos![0];
    }
    return null;
  }

  /// 性别
  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!;
    }
  }

  String? get phoneNo => _dto.value.phone;

  String? get nation => _dto.value.nationality;

  DateTime? get birthday => _dto.value.birthday;

  CardTypeEnum get cardType => _dto.value.cardType;

  /// 签约状态
  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;
  }
}
