import 'base_form_value.dart';
import 'form_info.dart';

class MaternalHealthFormValue extends BaseFormValueChange {
  static MaternalHealthFormValue? _maternalHealthManagementForm;

  MaternalHealthFormValue._internal();

  ///第 1 次产前检查服务记录表的值
  static MaternalHealthFormValue get instance {
    _maternalHealthManagementForm ??= MaternalHealthFormValue._internal();
    return _maternalHealthManagementForm!;
  }

  void initListener() {
    FormInfo.instance.onValueChange.addListener(_onValueChange);
  }

  void dispose() {
    FormInfo.instance.onValueChange.removeListener(_onValueChange);
  }

  void _onValueChange(Object sender, UpdateFormArgs e) {
    String sourceKey = e.sourceKey;
    UpdateFormType type = e.type;
    switch (sourceKey) {
      //末次月经
      case "Last_Period":
        changeLastPeriodDate(e);
        break;
      //既往史
      case "Past_History":
        //既往史选择无
        changePastHistory(e);
        break;
      //家族史
      case "Family_History":
        changeFamilyHistory(e);
        break;
      //妇产科手术史
      case "Operation_History":
        changeOperationHistory(e);
        break;
      //升高、体重计算BMI
      case "Weight":
      case "Height":
        super.onHeightOrWeightChange(e);
        break;
      //心脏
      case "Heart_Abnormal":
        changeHeartAbnormal(e);
        break;
      //肺部
      case "Lung_Abnormal":
        changeLungAbnormal(e);
        break;
      //外阴
      case "Vulvar":
        changeVulvar(e);
        break;
      //阴道
      case "Vagina":
        onValueChange(e, "Vagina_1", "Vagina_Abnormality");
        break;
      //宫颈
      case "Cervical":
        onValueChange(e, "Cervical_1", "Cervical_Abnormality");
        break;
      //子宫
      case "Uteri_Body":
        onValueChange(e, "Uteri_Body_1", "Uteri_Body_Abnormality");
        break;
      //附件
      case "Uteri_Adnexa":
        onValueChange(e, "Uteri_Adnexa_1", "Uteri_Adnexa_Abnormality");
        break;
      //阴道分泌物
      case "Vaginal_Discharge":
        onVaginalDischargeChange(e);
        break;
      case "MaternalHealthManagement_Overall_Assessment":
        onValueChange(e, "M_O_A_No_Abnormality_Detected", "M_O_A_Abnormality");
        break;
      case "Referral":
        onReferralChange(e);
        break;
      case "Last_Period_Date":
        onLastPeriodDateChange(e);
        break;
      case "Health_History":
        onHealthHistoryChange(e);
        break;
    }
  }

  void changeLastPeriodDate(UpdateFormArgs e) {
    UpdateFormType type = e.type;

    if (type == UpdateFormType.Add) {
      if (FormInfo.instance.formValue.containsKey("Last_Period_Date")) {
        FormInfo.instance.formValue.remove("Last_Period_Date");
      }

      ///选中了末次月经的不详,则禁用日期选择器
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Last_Period_Date",
          isDisabledValue: true,
        ),
      );
    } else if (type == UpdateFormType.Remove) {
      ///取消选中了末次月经的不详,则取消禁用日期选择器
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Last_Period_Date",
          isDisabledValue: false,
        ),
      );
    }
  }

  void changePastHistory(UpdateFormArgs e) {
    UpdateFormType type = e.type;
    bool isDisabledValue = false;
    if (type == UpdateFormType.Add && e.sourceValue == "Past_History_1") {
      ///选中了无,则禁用其他选项
      isDisabledValue = true;
    } else if (type == UpdateFormType.Remove &&
        e.sourceValue == "Past_History_1") {
      ///取消选中无,则取消禁用其他选项
      isDisabledValue = false;
    } else {
      return;
    }
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_2",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_3",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_4",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_5",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_6",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_7",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Past_History_Other",
        isDisabledValue: isDisabledValue,
      ),
    );
  }

  void changeFamilyHistory(UpdateFormArgs e) {
    UpdateFormType type = e.type;
    bool isDisabledValue = false;

    if (type == UpdateFormType.Add && e.sourceValue == "Family_History_1") {
      ///选中了无,则禁用其他选项
      isDisabledValue = true;
    } else if (type == UpdateFormType.Remove &&
        e.sourceValue == "Family_History_1") {
      ///取消选中无,则取消禁用其他选项
      isDisabledValue = false;
    } else if (type == UpdateFormType.Add &&
        e.sourceValue == "Family_History_2") {
      //选中了遗传性病史,则显示遗传性病史详情
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Family_History_2_Detail",
          isHidden: false,
        ),
      );
      return;
    } else if (type == UpdateFormType.Remove &&
        e.sourceValue == "Family_History_2") {
      //取消选中了遗传性病史,则隐藏遗传性病史详情
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Family_History_2_Detail",
          isHidden: true,
        ),
      );
      return;
    } else if (type == UpdateFormType.Add &&
        e.sourceValue == "Family_History_3") {
      //选中了精神疾病史,则显示精神疾病史详情
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Family_History_3_Detail",
          isHidden: false,
        ),
      );
      return;
    } else if (type == UpdateFormType.Remove &&
        e.sourceValue == "Family_History_3") {
      //取消选中了精神疾病史
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Family_History_3_Detail",
          isHidden: true,
        ),
      );
      return;
    } else {
      return;
    }
    if (isDisabledValue) {
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Family_History_3_Detail",
          isHidden: true,
        ),
      );
      FormInfo.instance.onChangeTargetValue.emit(
        this,
        TargetFormArgs(
          "Family_History_2_Detail",
          isHidden: true,
        ),
      );
    }
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Family_History_2",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Family_History_3",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Family_History_Other",
        isDisabledValue: isDisabledValue,
      ),
    );
  }

  void changeOperationHistory(UpdateFormArgs e) {
    UpdateFormType type = e.type;
    if (e.sourceValue == "Operation_History_null") {
      if (type == UpdateFormType.Add) {
        FormInfo.instance.onChangeTargetValue.emit(
          this,
          TargetFormArgs(
            "Operation_History_True",
            targetValue: "cancelSelection",
          ),
        );
        FormInfo.instance.onChangeTargetValue.emit(
          this,
          TargetFormArgs(
            "Operation_History_True_Detail",
            isHidden: true,
          ),
        );
      }

      FormInfo.instance.formValue[e.sourceKey] = [e.sourceValue];
    } else if (e.sourceValue == "Operation_History_True") {
      FormInfo.instance.formValue[e.sourceKey] = [e.sourceValue];
      if (type == UpdateFormType.Add) {
        FormInfo.instance.onChangeTargetValue.emit(
          this,
          TargetFormArgs(
            "Operation_History_null",
            targetValue: "cancelSelection",
          ),
        );
        FormInfo.instance.onChangeTargetValue.emit(
          this,
          TargetFormArgs(
            "Operation_History_True_Detail",
            isHidden: false,
          ),
        );
      } else if (type == UpdateFormType.Remove) {
        FormInfo.instance.onChangeTargetValue.emit(
          this,
          TargetFormArgs(
            "Operation_History_True_Detail",
            isHidden: true,
          ),
        );
      }
    }
  }

  void changeHeartAbnormal(UpdateFormArgs e) {
    onValueChange(e, "Heart_Abnormal_1", "Heart_Abnormal_Abnormality");
  }

  void changeLungAbnormal(UpdateFormArgs e) {
    onValueChange(e, "Lung_Abnormal_1", "Lung_Abnormal_Abnormality");
  }

  void changeVulvar(UpdateFormArgs e) {
    onValueChange(e, "Vulvar_1", "Vulvar_Abnormality");
  }

  void onVaginalDischargeChange(UpdateFormArgs e) {
    UpdateFormType type = e.type;
    bool isDisabledValue = false;
    if (type == UpdateFormType.Add && e.sourceValue == "Vaginal_Discharge_1") {
      ///选中了无,则禁用其他选项
      isDisabledValue = true;
    } else if (type == UpdateFormType.Remove &&
        e.sourceValue == "Vaginal_Discharge_1") {
      ///取消选中无,则取消禁用其他选项
      isDisabledValue = false;
    }
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Vaginal_Discharge_2",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Vaginal_Discharge_3",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Vaginal_Discharge_4",
        isDisabledValue: isDisabledValue,
      ),
    );
  }

  void onLastPeriodDateChange(UpdateFormArgs e) {
    var targetValue = calculateDueDateString(e.sourceValue);
    FormInfo.instance.formValue["EstimatedDeliveryDate"] = targetValue;
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "EstimatedDeliveryDate",
        isDisabledValue: false,
        targetValue: targetValue,
      ),
    );
  }

  String calculateDueDateString(String lastMenstrualPeriodStr) {
    // 首先将输入字符串转换为DateTime对象
    DateTime lastMenstrualPeriod = DateTime.parse(lastMenstrualPeriodStr);

    // 计算预产期
    DateTime dueDate = DateTime(
      lastMenstrualPeriod.year,
      lastMenstrualPeriod.month + 9,
      lastMenstrualPeriod.day + 6,
    );
    dueDate = DateTime(
      dueDate.year + (dueDate.month > 12 ? 1 : 0), // 如果月份大于12,年份加一
      dueDate.month > 12
          ? dueDate.month - 12
          : dueDate.month, // 如果月份大于12,月份减去12
      dueDate.day, // 日子不变
    );
    dueDate = DateTime(
      dueDate.year,
      dueDate.month,
      dueDate.day > DateTime(dueDate.year, dueDate.month + 1, 0).day
          ? DateTime(dueDate.year, dueDate.month + 1, 0).day
          : dueDate.day,
    );

    // 将预产期DateTime对象转换回字符串格式
    String dueDateString =
        "${dueDate.year}-${dueDate.month.toString().padLeft(2, '0')}-${dueDate.day.toString().padLeft(2, '0')}";

    return dueDateString;
  }

  void onHealthHistoryChange(UpdateFormArgs e) {
    UpdateFormType type = e.type;
    bool isDisabledValue = false;
    if (type == UpdateFormType.Add && e.sourceValue == "Health_History_1") {
      ///选中了无,则禁用其他选项
      isDisabledValue = true;
    } else if (type == UpdateFormType.Remove &&
        e.sourceValue == "Health_History_1") {
      ///取消选中无,则取消禁用其他选项
      isDisabledValue = false;
    }
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Health_History_2",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Health_History_3",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Health_History_4",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Health_History_5",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Health_History_6",
        isDisabledValue: isDisabledValue,
      ),
    );
    FormInfo.instance.onChangeTargetValue.emit(
      this,
      TargetFormArgs(
        "Health_History_7",
        isDisabledValue: isDisabledValue,
      ),
    );
  }
}