import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/view/measure/measure_images_bar.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/pages/index.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/muti_select.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/entitys/report_tag_key.dart';
import 'package:flyinsono/utils/export_status_type.dart';
import 'package:get/get.dart';
import 'package:fis_lib_report/report_info/element_tag_names.dart';

///这些Tag是用来判定手机端写报告需要展示哪些内容，以下内容如果能在模板中获取到，则展示，模板中没有则不展示
const List<String> PETREPORTTAGS = [
  TagNames.HOSPITALNAME, //医院
  TagNames.OWNER, //主人
  TagNames.PATIENT_NAME_TAG, //宠物名
  TagNames.PATIENT_SEX_TAG, //性别： "雄性",   "雌性", "绝育", "去势"
  TagNames.PATIENT_AGE_TAG, //年龄
  TagNames.PATIENT_ID_TAG, //ID
  TagNames.SPECIES, //种类："牛类","犬类","猫类","羊类","猪类"
  TagNames.INSPHYSICIAN, //检查医师
  TagNames.RPHYSICIAN, //报告医师
];

class FConsultationState {
  final Rx<int> _selectIndex = Rx(0);
  final Rx<int> _selectPatientCardIndex = Rx(0);
  final Rx<int> _selectExamCardIndex = Rx(0);
  final Rx<int> _isValid = Rx(0);
  final Rx<int> _patientPageInt = Rx(1);
  final Rx<int> _recordCount = Rx(0);
  final Rx<int> _recordPage = Rx(0);
  final Rx<int> _imageScannedIndex = Rx(0);
  final Rx<int> _examRecordIndex = Rx(0);
  final Rx<int> _selectTemplateIndex = Rx(0);
  final Rx<int> _cloudImageIndex = Rx(0);
  final Rx<int> _referTab = Rx(0);
  final Rx<int> _patientTab = Rx(0);
  final Rx<String> _recordCode = Rx('');
  final Rx<String> _accessingReportCode = Rx('');
  final Rx<String> _reportCode = Rx('');
  final Rx<String> _patientCode = Rx('');
  final Rx<String> _referralRecordCode = Rx('');
  final Rx<String> _keyWord = Rx('');
  final Rx<String> _patientName = Rx('');
  final Rx<String> _rservationCode = Rx('');
  final Rx<String> _conclusion = Rx('');
  final Rx<String> _description = Rx('');
  final Rx<String> _subjectMatter = Rx('');
  final Rx<String> _refuseCause = Rx('');
  final Rx<String> _modifyReferralRecordCode = Rx('');
  final Rx<String?> _referralOrganizationCode = Rx(null);
  final Rx<String?> _referralUserCode = Rx(null);

  final Rx<List<PatientInfo>> _patientInfos = Rx(<PatientInfo>[]);
  final _patientInfo = Rx<PatientInfo>(PatientInfo(patientCode: ''));
  final Rx<List<int>> _selectedExportItems = Rx(<int>[0, 2]);
  final Rx<List<Map<String, dynamic>>> _associatPatientInfos =
      Rx(<Map<String, dynamic>>[]);

  final RxList<GetRecordsPageDTO> _checkLists = RxList<GetRecordsPageDTO>();
  final Rx<GetRecordsPageDTO> _examRecord = Rx(GetRecordsPageDTO());
  final Rx<List<PatientInfoExt>> _checkInfoLists = Rx(<PatientInfoExt>[]);
  final Rx<List<OrgModel>> _referOrg = Rx(<OrgModel>[]);
  final RxList<TerminalImage> _selectedImages = RxList(<TerminalImage>[]);
  final Rx<List<UserRoleModel>> _userRoleList = Rx(<UserRoleModel>[]);
  final Rx<List<UserDoctorModel>> _referUserList = Rx(<UserDoctorModel>[]);
  final Rx<DateTime> _startDateTime = Rx(DateTime(1970, 01, 01));
  final Rx<DateTime> _endDateTime = Rx(DateTime.now());
  final Rx<OrganizationPatientTypeEnum> _patientType =
      Rx(OrganizationPatientTypeEnum.Person);
  final Rx<SetValidPatientRequest> _setValidPatientRequest =
      Rx(SetValidPatientRequest());
  final Rx<List<RemedicalListResult>> _imageScanned =
      Rx(<RemedicalListResult>[]);
  final Rx<List<RemedicalMeasuredInfoDTO>> _remedicalMeasured =
      Rx(<RemedicalMeasuredInfoDTO>[]);
  final Rx<List<GetRecordsPageDTO>> _getRecordsPageResult =
      Rx(<GetRecordsPageDTO>[]);
  final Rx<List<ReportDTO>> _reportList = Rx(<ReportDTO>[]);
  final Rx<List<ReferralRecordDetailDTO>> _referralRecordDetail =
      Rx(<ReferralRecordDetailDTO>[]);
  final Rx<GetRecordsPageDTO> _lastRecord = Rx(GetRecordsPageDTO());
  final Rx<PatientInfo?> _patient = Rx(null);
  final Rx<RemedicalListResult> _getScannedImage = Rx(RemedicalListResult());
  final Rx<bool> _isSelectedCloudImage = Rx(true);
  final Rx<bool> _isEmailShare = Rx(false);
  final Rx<ReportDTO> _reportInfo = Rx(ReportDTO());
  final Rx<bool> _isSafeMode = Rx(false);
  final Rx<bool> _isModifyReferralRecord = Rx(false);
  final Rx<bool> _isSelectedAll = Rx(false);
  final Rx<bool> _isExportMode = Rx(false);
  final Rx<bool> _isExporting = Rx(false);
  final Rx<double> _exportProgress = Rx(0);
  final Rx<ExportStatusType> _exportState = Rx(ExportStatusType.Starting);
  final Rx<int> _reportIndex = Rx(0);
  final RxList<String> _selectedPhoneNumbers = RxList(<String>[]);
  final RxList<String> _selectedEmails = RxList(<String>[]);
  final RxMap<ReportTagKey, List<String>> _allReportTags =
      RxMap<ReportTagKey, List<String>>();
  final RxList<String> _selectedTags = RxList<String>();
  final RxBool _isShowValid = RxBool(true);
  final Rx<int> _listTotalCount = Rx(0);
  final Rx<bool> _autoCreateExam = Rx(true);
  final Rx<String?> _currentCreatedPatient = Rx(null);
  final Rx<List<FMutiSelectModel>> _organizationLocatedList =
      Rx(<FMutiSelectModel>[]);
  final Rx<List<FMutiSelectModel>> _personDeviceList = Rx(<FMutiSelectModel>[]);
  final Rx<List<String>> _selectedOrganizationLocated = Rx(<String>[]);
  final Rx<List<String>> _selectedpersonDevice = Rx(<String>[]);

  ///路由参数：病人Code
  String? parameterPatientCode;

  ///报告位置
  int get reportIndex => _reportIndex.value;
  set reportIndex(int index) => _reportIndex.value = index;

  /// 列表中项目总数
  int get listTotalCount => _listTotalCount.value;
  set listTotalCount(int value) => _listTotalCount.value = value;

  ///标签列表
  Map<ReportTagKey, List<String>> get allReportTags => _allReportTags;
  set allReportTags(Map<ReportTagKey, List<String>> value) =>
      _allReportTags.value = value;

  ///选中标签列表
  List<String> get selectedTags => _selectedTags;
  set selectedTags(List<String> value) => _selectedTags.value = value;

  ///是否邮箱分享
  bool get isEmailShare => _isEmailShare.value;
  set isEmailShare(bool isEmailShare) => _isEmailShare.value = isEmailShare;

  ///已添加的邮箱
  List<String> get selectedEmails => _selectedEmails;
  set selectedEmails(List<String> values) => _selectedEmails.value = values;

  ///已添加的手机号码
  List<String> get selectedPhoneNumbers => _selectedPhoneNumbers;
  set selectedPhoneNumbers(List<String> values) =>
      _selectedPhoneNumbers.value = values;

  ///当前报告
  ReportDTO get reportInfo => _reportInfo.value;
  set reportInfo(ReportDTO v) => _reportInfo.value = v;

  int get selectTemplateIndex => _selectTemplateIndex.value;
  set selectTemplateIndex(int value) => _selectTemplateIndex.value = value;

  ///手机端是否选中超声图像
  set isSelectedCloudImage(value) => _isSelectedCloudImage.value = value;
  bool get isSelectedCloudImage => _isSelectedCloudImage.value;

  ///选中的云端图像集合
  set selectedImages(List<TerminalImage> v) => _selectedImages.value = v;
  List<TerminalImage> get selectedImages => _selectedImages;

  ///选中的测量图像集合
  List<String> selectedRemedicalImages = [];

  ///搜索病人名称
  set patientName(value) => _patientName.value = value;
  String get patientName => _patientName.value;

  /// 搜索病人
  set patient(PatientInfo? value) => _patient.value = value;
  PatientInfo? get patient => _patient.value;

  /// keyWord 根据关键字加载
  set keyWord(value) => _keyWord.value = value;
  get keyWord => _keyWord.value;

  ///描述
  set description(String v) => _description.value = v;
  String get description => _description.value;

  ///结论
  set conclusion(String v) => _conclusion.value = v;
  String get conclusion => _conclusion.value;

  /// 病人详情、检查报告、检查记录、扫查图像 tab跳转
  set selectIndex(value) => _selectIndex.value = value;
  get selectIndex => _selectIndex.value;

  /// 病人Card
  set selectPatientCardIndex(value) => _selectPatientCardIndex.value = value;
  int get selectPatientCardIndex => _selectPatientCardIndex.value;

  /// 病人详情
  set patientInfo(PatientInfo value) => _patientInfo(value);
  PatientInfo get patientInfo => _patientInfo.value;

  /// 病人卡片集合
  set patientInfos(List<PatientInfo> value) => _patientInfos(value);
  List<PatientInfo> get patientInfos => _patientInfos.value;

  /// 病人Code
  set patientCode(value) => _patientCode.value = value;
  String get patientCode => _patientCode.value;

  /// 转诊Code
  set referralRecordCode(value) => _referralRecordCode.value = value;
  String get referralRecordCode => _referralRecordCode.value;

  /// 选中病例Code,用于编辑报告时获取图像
  set recordCode(String value) => _recordCode.value = value;
  String get recordCode => _recordCode.value;

  /// 选中待打印、下载的报告Code,
  set accessingReportCode(String value) => _accessingReportCode.value = value;
  String get accessingReportCode => _accessingReportCode.value;

  /// 选中报告Code
  set reportCode(String value) => _reportCode.value = value;
  String get reportCode => _reportCode.value;

  ///手机端选中病例
  set examRecord(GetRecordsPageDTO value) => _examRecord.value = value;
  GetRecordsPageDTO get examRecord => _examRecord.value;

  /// 检查列表
  set checkLists(List<GetRecordsPageDTO> value) => _checkLists(value);
  List<GetRecordsPageDTO> get checkLists => _checkLists;

  /// 病例Card
  set selectExamCardIndex(value) => _selectExamCardIndex.value = value;
  get selectExamCardIndex => _selectExamCardIndex.value;

  /// 筛选项的初始时间
  set startDateTime(value) => _startDateTime.value = value;
  get startDateTime => _startDateTime.value;

  /// 筛选项的结束时间
  set endDateTime(value) => _endDateTime.value = value;
  DateTime get endDateTime => _endDateTime.value;

  /// 是否登记 搜索病人用
  set isValid(value) => _isValid.value = value;
  get isValid => _isValid.value;

  /// 是否登记 记住当前选中tab是否已登记 用来处理部分按钮的显示或隐藏
  set isShowValid(value) => _isShowValid.value = value;
  get isShowValid => _isShowValid.value;

  /// 检查详情
  List<PatientInfoExt> get checkInfoLists => _checkInfoLists.value;
  set checkInfoLists(List<PatientInfoExt> value) => _checkInfoLists(value);

  /// 转诊组织
  List<OrgModel> get referOrg => _referOrg.value;
  set referOrg(List<OrgModel> value) => _referOrg(value);

  /// 角色信息
  List<UserRoleModel> get userRoleList => _userRoleList.value;
  set userRoleList(List<UserRoleModel> value) => _userRoleList(value);

  /// 转诊医生
  List<UserDoctorModel> get referUserList => _referUserList.value;
  set referUserList(List<UserDoctorModel> value) => _referUserList(value);

  /// 机构服务类型 人或动物
  set patientType(OrganizationPatientTypeEnum value) =>
      _patientType.value = value;
  OrganizationPatientTypeEnum get patientType => _patientType.value;

  /// 关联病人列表集合
  set associatPatientInfos(List<Map<String, dynamic>> value) =>
      _associatPatientInfos.value = value;
  List<Map<String, dynamic>> get associatPatientInfos =>
      _associatPatientInfos.value;

  /// 关联病人
  set setValidPatientRequest(value) => _setValidPatientRequest.value = value;
  get setValidPatientRequest => _setValidPatientRequest.value;

  /// pageInt 病人页数
  set patientPageInt(int value) => _patientPageInt.value = value;
  int get patientPageInt => _patientPageInt.value;

  /// pageInt 检查页数
  set recordCount(value) => _recordCount.value = value;
  get recordCount => _recordCount.value;

  /// pageInt 病人页数
  set recordPage(value) => _recordPage.value = value;
  get recordPage => _recordPage.value;

  /// 扫查图像
  set imageScanned(List<RemedicalListResult> value) => _imageScanned(value);
  List<RemedicalListResult> get imageScanned => _imageScanned.value;

  /// 测量图像
  set remedicalMeasured(List<RemedicalMeasuredInfoDTO> value) =>
      _remedicalMeasured(value);
  List<RemedicalMeasuredInfoDTO> get remedicalMeasured =>
      _remedicalMeasured.value;

  /// 当前扫查图像的第几个记录
  set imageScannedIndex(int value) => _imageScannedIndex.value = value;
  int get imageScannedIndex => _imageScannedIndex.value;

  //手机端检查记录页数
  set examRecordIndex(value) => _examRecordIndex.value = value;
  get examRecordIndex => _examRecordIndex.value;

  /// 检查记录的查看扫查图像
  set getScannedImage(RemedicalListResult value) =>
      _getScannedImage.value = value;
  RemedicalListResult get getScannedImage => _getScannedImage.value;

  /// 云端图像或者本地图像
  set cloudImageIndex(value) => _cloudImageIndex.value = value;
  get cloudImageIndex => _cloudImageIndex.value;

  /// 转诊申请或者转诊历史
  set referTab(value) => _referTab.value = value;
  get referTab => _referTab.value;

  /// 我的病人或者转入病人
  set patientTab(value) => _patientTab.value = value;
  get patientTab => _patientTab.value;

  /// 扫查图像
  set getRecordsPageResult(List<GetRecordsPageDTO> value) =>
      _getRecordsPageResult(value);
  List<GetRecordsPageDTO> get getRecordsPageResult =>
      _getRecordsPageResult.value;

  /// 关联的检查
  set rservationCode(String value) => _rservationCode.value = value;
  String get rservationCode => _rservationCode.value;

  /// 转诊事由
  set subjectMatter(String value) => _subjectMatter.value = value;
  String get subjectMatter => _subjectMatter.value;

  /// 拒绝原因
  set refuseCause(String value) => _refuseCause.value = value;
  String get refuseCause => _refuseCause.value;

  ///修改时的转诊code
  set modifyReferralRecordCode(String value) =>
      _modifyReferralRecordCode.value = value;
  String get modifyReferralRecordCode => _modifyReferralRecordCode.value;

  /// 转入机构
  set referralOrganizationCode(String? value) =>
      _referralOrganizationCode.value = value;
  String? get referralOrganizationCode => _referralOrganizationCode.value;

  /// 转入医生
  set referralUserCode(String? value) => _referralUserCode.value = value;
  String? get referralUserCode => _referralUserCode.value;

  /// 报告列表
  set reportList(List<ReportDTO> value) => _reportList(value);
  List<ReportDTO> get reportList => _reportList.value;

  /// 转诊历史记录列表
  set referralRecordDetail(List<ReferralRecordDetailDTO> value) =>
      _referralRecordDetail(value);
  List<ReferralRecordDetailDTO> get referralRecordDetail =>
      _referralRecordDetail.value;

  /// 近期检查
  set lastRecord(GetRecordsPageDTO value) => _lastRecord.value = value;
  GetRecordsPageDTO get lastRecord => _lastRecord.value;

  ///隐私保护模式
  set isSafeMode(bool v) => _isSafeMode.value = v;
  bool get isSafeMode => _isSafeMode.value;

  ///是否修改转诊
  set isModifyReferralRecord(bool v) => _isModifyReferralRecord.value = v;
  bool get isModifyReferralRecord => _isModifyReferralRecord.value;

  ///是否全选
  set isSelectAll(bool v) => _isSelectedAll.value = v;
  bool get isSelectAll => _isSelectedAll.value;

  ///是否是批量导出模式
  set isExportMode(bool v) => _isExportMode.value = v;
  bool get isExportMode => _isExportMode.value;

  /// 导出项目列表
  set selectedExportItems(List<int> value) => _selectedExportItems(value);
  List<int> get selectedExportItems => _selectedExportItems.value;

  ///是否正在导出
  set isExporting(bool v) => _isExporting.value = v;
  bool get isExporting => _isExporting.value;

  ///导出进度
  set exportProgress(double v) => _exportProgress.value = v;
  double get exportProgress => _exportProgress.value;

  ///导出状态
  set exportState(ExportStatusType v) => _exportState.value = v;
  ExportStatusType get exportState => _exportState.value;

  ///是否自动创建检查
  set autoCreateExam(v) => _autoCreateExam.value = v;
  bool get autoCreateExam => _autoCreateExam.value;

  ///最新创建的病人
  set currentNewlyCreatedPatient(String? v) => _currentCreatedPatient.value = v;
  String? get currentNewlyCreatedPatient => _currentCreatedPatient.value;

  /// 所属医院
  set organizationLocatedList(List<FMutiSelectModel> value) =>
      _organizationLocatedList(value);
  List<FMutiSelectModel> get organizationLocatedList =>
      _organizationLocatedList.value;

  /// 个人设备列表
  set personDeviceList(List<FMutiSelectModel> value) =>
      _personDeviceList(value);
  List<FMutiSelectModel> get personDeviceList => _personDeviceList.value;

  /// 选中的所属医院
  set selectedOrganizationLocated(List<String> value) =>
      _selectedOrganizationLocated(value);
  List<String> get selectedOrganizationLocated =>
      _selectedOrganizationLocated.value;

  /// 选中的个人设备
  set selectedpersonDevice(List<String> value) => _selectedpersonDevice(value);
  List<String> get selectedpersonDevice => _selectedpersonDevice.value;

  updateSelectAllState() {
    var selectAll = true;
    if (patientInfos.isEmpty) {
      selectAll = false;
    }
    for (var c in patientInfos) {
      if (!c.isChecked) {
        selectAll = false;
      }
      c.exportMode = isExportMode;
    }

    isSelectAll = isExportMode ? selectAll : false;
  }
}

class UserDoctorModel {
  UserDoctorModel({
    required this.name,
    required this.code,
    this.roleCodes,
    this.fieldList,
    this.headImageUrl,
  });
  final String name;
  final String code;
  final List<String>? roleCodes;
  final List<String>? fieldList;
  final String? headImageUrl;
}

class UserRoleModel {
  UserRoleModel({
    required this.name,
    required this.code,
  });
  final String name;
  final String code;
}
