import 'dart:collection';

import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/architecture/types/index.dart';
import 'package:vitalapp/architecture/utils/datetime.dart';
import 'package:vitalapp/pages/controllers/crowd_labels.dart';
import 'package:vitalapp/store/store.dart';

class CreatePatientState {
  final RxBool _isCreateOnly = RxBool(false);

  final RxString _name = RxString("");
  final RxString _personalNo = RxString("");
  final RxString _phoneNo = RxString("");
  final Rx<CardTypeEnum> _cardType = Rx<CardTypeEnum>(CardTypeEnum.Identity);
  final RxString _cardNo = RxString("");
  final RxString _nation = RxString("");
  final Rx<GenderEnum> _gender = Rx<GenderEnum>(GenderEnum.Male);
  final Rx<DateTime?> _birthday = Rx<DateTime?>(null);
  final RxString _age = RxString("");
  final RxString _censusRegister = RxString("");
  final RxString _address = RxString("");
  final RxString _villageCode = RxString("");
  final RxList<String> _crowdLabelCodes = RxList();
  final RxBool _isSyncAddresses = RxBool(true);

  void reset() {
    var crowdLabelController = Get.find<CrowdLabelsController>();
    crowdLabelController.state.selectedNormalCodes = [];
    crowdLabelController.state.selectedDiseaseCodes = [];
    crowdLabelController.state.selectedSpecialCareCodes = [];
    name = "";
    personalNo = "";
    cardType = CardTypeEnum.Identity;
    cardNo = "";
    nation = "";
    gender = GenderEnum.Male;
    birthday = null;
    censusRegister = "";
    address = "";
    villageCode = "";
    phoneNo = "";
    crowdLabelCodes = [];
    isSyncAddresses = true;
  }

  /// 机构名称
  String get organizationName => Store.user.organizationName;

  /// 村选项集合
  List<StringKVModel> get villageOptions => Store.user.residence;

  /// 是否只创建档案
  bool get isCreateOnly => _isCreateOnly.value;
  set isCreateOnly(bool val) => _isCreateOnly.updateValue(val);

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

  /// 个人编号
  String get personalNo => _personalNo.value;
  set personalNo(String val) => _personalNo.updateValue(val);

  /// 手机号
  String get phoneNo => _phoneNo.value;
  set phoneNo(String val) => _phoneNo.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); // 返回一个默认值，表示无法提取出生日期
  }

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

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

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

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

  /// 村/居委会 编号
  String get villageCode => _villageCode.value;
  set villageCode(String val) => _villageCode.updateValue(val);

  /// 村/居委会 名称
  String get villageName {
    String name = "";
    if (villageCode.isNotEmpty) {
      final dto = villageOptions.firstWhereOrNull((e) => e.key == villageCode);
      if (dto != null) {
        name = dto.value;
      }
    }
    return name;
  }

  /// 人群分类集合
  List<String> get crowdLabelCodes =>
      UnmodifiableListView(_crowdLabelCodes.toList());
  set crowdLabelCodes(List<String> val) => _crowdLabelCodes.value = val;

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

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

  /// 是否开启地址同步
  bool get isSyncAddresses => _isSyncAddresses.value;
  set isSyncAddresses(bool val) => _isSyncAddresses.updateValue(val);
}
