import 'package:flyinsono/views/desktop/appointment_consultation/controller.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/state.dart';

class CheckRequiredItemController {
  late final AppointmentConsultationController controller;
  late final AppotintmentConsultationState state;

  CheckRequiredItemController(AppointmentConsultationController controller) {
    this.controller = controller;
    this.state = controller.state;
  }

  /// 是否能够申请预约单
  bool isCanApplyConsultation() {
    // 遍历预约信息中的必填项列表
    for (int i = 0; i < state.appointmentKeyList.length; i++) {
      // 如果当前项目是必填项
      if (state.appointmentKeyList[i].required) {
        // 检查对应的预约信息字段是否为空
        switch (state.appointmentKeyList[i].key) {
          case 'Device':
            if (state.appointmentInformation.appointmentDevice?.isEmpty ??
                true) {
              // 如果必填字段为空，返回 false
              return false;
            }
            break;
          case 'ConsultationTime':
            if (state.appointmentInformation.appointmentTime == null) {
              return false;
            }
            break;
          case 'ScanUser':
            if (state.appointmentInformation.appointmentDocter?.isEmpty ??
                true) {
              return false;
            }
            break;
          case 'Diseases':
            if (state.appointmentInformation.appointmentDiseaseName?.isEmpty ??
                true) {
              return false;
            }
            break;
          case 'ScanPosition':
            if (state.appointmentInformation.appointmentScanningPosition
                    ?.isEmpty ??
                true) {
              return false;
            }
            break;
          case 'ExpertOrganization':
            if (state.appointmentInformation.appointmentConsultationInstitution
                    ?.isEmpty ??
                true) {
              return false;
            }
            break;
          case 'Expert':
            if (state.appointmentInformation.appointmentCompetentExpert
                    ?.isEmpty ??
                true) {
              return false;
            }
            break;
          case 'ApplyUser':
            if (state.appointmentInformation.appointmentApplicant?.isEmpty ??
                true) {
              return false;
            }
            break;

          case 'PrimaryDiagnosis':
            if (state.appointmentInformation.appointmentApplicant?.isEmpty ??
                true) {
              return false;
            }
            break;
          case 'ApplyOrganization':
            if (state.appointmentInformation.appointmentApplicant?.isEmpty ??
                true) {
              return false;
            }
            break;
        }
      }
    }
    // 遍历病人信息中的必填项列表
    for (int i = 0; i < state.patientKeyList.length; i++) {
      // 如果当前项目是必填项
      if (state.patientKeyList[i].required) {
        // 检查对应的预约信息字段是否为空
        switch (state.patientKeyList[i].key) {
          case 'Name':
            if (state.currentPatient.name?.isEmpty ?? true) {
              // 如果必填字段为空，返回 false
              return false;
            }
            break;
          case 'Phone':
            state.currentPatient.phone = controller.patient['Phone'];
            if (state.currentPatient.phone?.isEmpty ?? true) {
              return false;
            }
            break;
          case 'AgeType':
            if (state.currentPatient.ageUnits?.isEmpty ?? true) {
              return false;
            }
            break;
          case 'Sex':
            if (state.currentPatient.sex?.isEmpty ?? true) {
              return false;
            }
            break;
          case 'Anamnesis':
            state.currentPatient.anamnesis = controller.patient['Anamnesis'];
            if (state.currentPatient.anamnesis?.isEmpty ?? true) {
              return false;
            }
            break;
        }
      }
    }

    // 如果所有必填字段都已填写，返回 true
    return true;
  }
}
