import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/architecture/utils/datetime.dart';
import 'package:vnoteapp/consts/rpc_enum_labels.dart';

class PatientInfoState {
  final Rx<NavAnchorType> _anchor = NavAnchorType.base.obs;

  /// 导航锚点
  NavAnchorType get anchor => _anchor.value;
  set anchor(NavAnchorType val) => _anchor.updateValue(val);

  final Rx<PatientDTO> _dto = Rx<PatientDTO>(
    PatientDTO(),
  );

  final RxString _code = RxString("");
  final RxString _name = RxString("");
  final Rx<CardTypeEnum> _cardType = Rx<CardTypeEnum>(CardTypeEnum.Identity);
  final RxString _cardNo = RxString("");
  final Rx<GenderEnum> _gender = Rx<GenderEnum>(GenderEnum.Male);
  final RxString _nation = RxString("");
  final Rx<DateTime?> _birthday = Rx<DateTime?>(null);
  final RxString _age = RxString("");
  final RxString _phoneNo = RxString("");
  final RxString _address = RxString("");
  final RxString _permanentResidenceAddress = RxString("");
  final RxString _createdOrgName = RxString("");
  final RxString _createdDoctorName = RxString("");
  final RxString _contractedDoctorName = RxString("");
  final RxString _createTime = RxString("");

  /// 病人编号
  String get code => _code.value;
  set code(String val) => _code.updateValue(val);

  /// 姓名
  String get name => _name.value;
  set name(String val) => _name.updateValue(val);

  /// 年龄
  String get age => _age.value;

  /// 手机号
  String get phoneNo => _phoneNo.value;
  set phoneNo(String val) => _phoneNo.updateValue(val);

  /// 地址
  String get address => _address.value;
  set address(String val) => _address.updateValue(val);

//户籍地址
  String get permanentResidenceAddress => _permanentResidenceAddress.value;
  set permanentResidenceAddress(String val) =>
      _permanentResidenceAddress.updateValue(val);

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

  /// 性别
  GenderEnum get gender => _gender.value;
  set gender(GenderEnum val) => _gender.updateValue(val);

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

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

  /// 民族
  String get nation => _nation.value;
  set nation(String val) => _nation.updateValue(val);

  /// 证件类型
  CardTypeEnum get cardType => _cardType.value;
  set cardType(CardTypeEnum val) => _cardType.updateValue(val);

  /// 证件号
  String get cardNo => _cardNo.value;
  set cardNo(String val) {
    _cardNo.updateValue(val);
    if (cardType == CardTypeEnum.Identity) {
      birthday = extractBirthDateFromIDCard(val);
    }
  }

  DateTime extractBirthDateFromIDCard(String idCardNumber) {
    final idCardRegex = RegExp(r'^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$');
    final match = idCardRegex.firstMatch(idCardNumber);

    if (match != null) {
      final year = int.parse(match.group(2)!);
      final month = int.parse(match.group(3)!);
      final day = int.parse(match.group(4)!);

      final birthDate = DateTime(year, month, day);

      return birthDate;
    }

    return DateTime(1970, 1, 1); // 返回一个默认值，表示无法提取出生日期
  }

  /// 生日
  DateTime? get birthday => _birthday.value;
  set birthday(DateTime? val) {
    if (val != _birthday.value && val != null) {
      _birthday.value = val;
      _age.value = DataTimeUtils.calculateAge(val);
    }
  }

  ///建档医院
  String get createdOrgName => _createdOrgName.value;
  set createdOrgName(String val) => _createdOrgName.updateValue(val);

  ///建档医师
  String get createdDoctorName => _createdDoctorName.value;
  set createdDoctorName(String val) => _createdDoctorName.updateValue(val);

  //建档时间
  String get createTime => _createTime.value;
  set createTime(String val) => _createTime.updateValue(val);

  ///责任医师
  String get contractedDoctorName => _contractedDoctorName.value;
  set contractedDoctorName(String val) =>
      _contractedDoctorName.updateValue(val);

  void updateDto(PatientDTO dto) {
    _dto.value = dto;
    code = dto.code ?? "";
    name = dto.patientName ?? "";
    cardType = dto.cardType;
    cardNo = dto.cardNo ?? "";
    gender = dto.patientGender;
    nation = dto.nationality ?? "";
    birthday = dto.birthday ?? DateTime(1970, 1, 1);
    phoneNo = dto.phone ?? "";
    address = dto.patientAddress ?? "";
    permanentResidenceAddress = dto.permanentResidenceAddress ?? "";
    createdOrgName = dto.createdOrgName ?? "";
    createdDoctorName = dto.createdDoctorName ?? "";
    contractedDoctorName = dto.contractedDoctorName ?? "";
    createTime = DateFormat("yyyy-MM-dd")
        .format(dto.createTime?.toLocal() ?? DateTime.now());
  }
}

enum NavAnchorType {
  base,
  detail,
  health,
}
