import 'package:doctor_app_flutter/core/model/SOAP/assessment/audit_diagnosis.dart';
import 'package:doctor_app_flutter/core/model/SOAP/assessment/patient_previous_diagnosis.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/assessment/widget/audit_list_bottom_sheet.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/assessment/widget/empty_diagnosis.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/assessment/widget/previous_diagnosis_item.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/chief_complaint/widgets/add_soap_item.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/widgets/SoapDetailItem.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:flutter/material.dart';

import '../../../../../../core/viewModel/SOAP_view_model.dart';

class PreviousDiagnosis extends StatelessWidget {
  final List<PatientPreviousDiagnosis> diagnosisItems;
  final PatiantInformtion patientInfo;

  const PreviousDiagnosis(
      {super.key, required this.diagnosisItems, required this.patientInfo});

  @override
  Widget build(BuildContext context) {
    if (diagnosisItems.isEmpty)
      return Center(
        child: EmptyDiagnosis(
          message: TranslationBase.of(context).noPreviousDiagnosis,
        ),
      );

    return BaseView<SOAPViewModel>(
        onModelReady: (model) async {},
        builder: (_, model, w) {
          if (model.showAudit) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              showAuditBottomSheet(context, model.auditDiagnosislist,
                  model.toggleShowBottomSheetValue);
              model.toggleShowBottomSheetValue(false);
            });
          }

          return Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: [
                Flexible(
                  child: ListView.separated(
                      itemBuilder: (_, index) {
                        return PreviousDiagnosisItem(
                          title: diagnosisItems[index].selectedDisease ?? '',
                          status: diagnosisItems[index].status ?? '',
                          condition: diagnosisItems[index].condition ?? '',
                          remarks: diagnosisItems[index].remarks ?? '',
                          type: model.getDiagnosisTypeValue(
                                  diagnosisItems[index].diagnosisType ?? '') ??
                              '',
                          isPreviousAddedToCurrent:
                              diagnosisItems[index].visitWiseSelected == true,
                          onSoapDetailActionClicked: (action) {
                            switch (action) {
                              case SoapDetailItemActions.AUDIT:
                                model.getAuditOfDiagnosis(
                                    patientInfo,
                                    diagnosisItems[index]
                                            .patientProblemRevisionId
                                            ?.toString() ??
                                        '');
                                break;
                              case SoapDetailItemActions.CHANGE_STATUS:
                                model.convertPreviousDiagnosisCurrent(
                                    patientInfo, diagnosisItems[index]);
                              default:
                            }
                          },
                        );
                      },
                      separatorBuilder: (_, index) => Divider(),
                      itemCount: diagnosisItems.length),
                )
              ],
            ),
          );
        });
  }

  void showAuditBottomSheet(
    BuildContext context,
    List<AuditDiagnosis> diagnosis,
    void Function(bool status) toggleShowBottomSheetValue,
  ) {
    showModalBottomSheet(
      isDismissible: true,
      context: context,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20), topRight: Radius.circular(20))),
      builder: (_) {
        return AuditListBottomSheet(
          auditList: diagnosis,
        );
      },
    ).then((value) => toggleShowBottomSheetValue(false));
  }
}
