import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/managers/server_config_manager.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/widgets/speech_input.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/controller.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/widgets/appointment_information.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/widgets/patient_age.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/widgets/select_patient_dialog.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/index.dart';
import 'package:get/get.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_theme/theme.dart';

class PatientInformation extends FStatelessWidget implements FPage {
  PatientInformation(this.appointmentFormEditing, this.width);
  @override
  String get pageName => 'PatientInformation';

  /// 是否是编辑状态
  final bool? appointmentFormEditing;
  final double width;

  final controller = Get.find<AppointmentConsultationController>();
  static const C_REMOVE_EXTRA_MARGIN = 60.0;
  static const C_DOUBLE_MUTIPLE = 2.0;
  @override
  FWidget build(BuildContext context) {
    return _buildPatientInfo(context);
  }

  FWidget _buildOnTapInput(String hintText, String value, [Function? onTap]) {
    return FContainer(
      child: FBorderInput(
        contentSize: 16,
        maxLength: 20,
        width: 300,
        borderColor: const Color.fromRGBO(230, 230, 230, 1),
        height: 38,
        hintSize: 16,
        readOnly: true,
        hintText: hintText,
        enableInteractiveSelection: false,
        enabled: appointmentFormEditing ?? true,
        controller: TextEditingController(text: value),
        onTap: onTap!(),
      ),
    );
  }

  List<FSelectModel> handleSource(List source) {
    List<FSelectModel> selectList = [];
    for (int i = 0; i < source.length; i++) {
      selectList.add(
        FSelectModel(
          code: source[i],
          name: controller.realTimeConsultationController.getLanguageValues(
            'consultation',
            source[i],
          ),
        ),
      );
    }
    return selectList;
  }

  FWidget _buildPatientInfo(BuildContext context) {
    return FObx(
      () => AppointmentInformation.buildFormLayout(
        width,
        [
          ...controller.state.patientKeyList.map((e) {
            switch (e.showType) {
              case 'input':
                return FConfigureLayout(
                  title: e.label,
                  widget: FConfigureInput(
                    hintText: i18nBook.common.input.translate(
                      [
                        controller.realTimeConsultationController
                            .getLanguageValues(
                          'consultation',
                          e.label,
                        ),
                      ],
                    ),
                    textController: TextEditingController(
                      text: controller.patient[e.key],
                    ),
                    onChanged: (value) {
                      controller.patient[e.key] = value.toString().trim();
                    },
                    enabled: appointmentFormEditing ?? true,
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(
                        RegExp(r'^[0-9]*$'), //只能输入数字
                      ),
                    ],
                  ),
                  isRequired: e.required,
                );

              case 'select':
                FWidget fConfigureSelect;
                if (appointmentFormEditing ?? true) {
                  fConfigureSelect = FConfigureSelect(
                    source: handleSource(e.source ?? []),
                    onChanged: (value) {
                      controller.patient[e.key] = value;
                    },
                    value: controller.patient[e.key],
                    readOnly: appointmentFormEditing,
                  );
                } else {
                  fConfigureSelect = FConfigureInput(
                    enabled: appointmentFormEditing,
                    textController: TextEditingController(
                      text: handleSource(e.source ?? [])
                          .firstWhereOrNull((element) =>
                              element.code == controller.patient[e.key])
                          ?.name,
                    ),
                  );
                }

                return FConfigureLayout(
                  title: e.label,
                  widget: fConfigureSelect,
                  isRequired: e.required,
                );

              case 'inputButton':
                return FConfigureLayout(
                  title: e.label,
                  isRequired: e.required,
                  widget: FExpanded(
                    child: _buildOnTapInput(
                      i18nBook.realTimeConsultation
                          .pleaseSelectPatientForConsultation.t,
                      controller.state.currentPatient.name ?? '',
                      () => () {
                        if (appointmentFormEditing ?? true) {
                          Get.dialog(
                            FObx(
                              () => FSelectPatient(
                                selectList: controller.state.patientList,
                              ),
                            ),
                          );
                        }
                        return;
                      },
                    ),
                  ),
                );
              case 'AgeType':
                return FConfigureLayout(
                  title: e.label,
                  widget: FExpanded(
                    child: FBuildAgeType(
                        appointmentFormEditing:
                            appointmentFormEditing ?? false),
                  ),
                );

              case 'textarea':
                var textEditingController = TextEditingController(
                  text: controller.patient[e.key],
                );

                return FConfigureLayout(
                  title: e.label,
                  proportion: C_DOUBLE_MUTIPLE,
                  widget: FConfigureTextArea(
                    textEditingController: textEditingController,
                    onChanged: (value) {
                      controller.patient[e.key] = value.toString().trim();
                    },
                    enabled: appointmentFormEditing ?? true,
                    suffixIcon: ServerConfigManager.isASROn
                        ? FIconButton(
                            businessParent: this,
                            name: 'audio recod',
                            icon: FIcon(
                              Icons.mic,
                              color: FTheme.ins.colorScheme.primary,
                            ),
                            onPressed: () async {
                              await router.dialog(
                                () => SpeechInput(
                                  null,
                                  textEditingController: textEditingController,
                                  onSubmit: (v) {
                                    controller.patient[e.key] = v.trim();
                                  },
                                ),
                                dismissible: false,
                              );
                            },
                          )
                        : null,
                  ),
                  isRequired: e.required,
                );

              default:
                return FSizedBox();
            }
          }).toList()
        ],
      ),
    );
  }
}
