import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/procedures/procedure_card.dart';
import 'package:doctor_app_flutter/screens/procedures/procedure_type.dart';
import 'package:doctor_app_flutter/screens/procedures/update_procedure.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_service_title.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/add-order/addNewOrder.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/app_bar/patient-profile-app-bar.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/transitions/slide_up_page.dart';
import 'package:flutter/material.dart';

import '../../widgets/shared/app_texts_widget.dart';
import '../../widgets/shared/errors/error_message.dart';
import 'base_add_procedure_tab_page.dart';

class ProcedureScreen extends StatelessWidget {
  int? doctorNameP;

  void initState() async {
    Map<String, dynamic> profile = await sharedPref.getObj(DOCTOR_PROFILE);
    DoctorProfileModel doctorProfile = DoctorProfileModel.fromJson(profile);
    doctorNameP = doctorProfile.doctorID;
  }

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    String patientType = routeArgs['patientType'];
    bool isFromLiveCare = routeArgs['isFromLiveCare'];
    bool isInpatient = routeArgs['isInpatient'];
    return BaseView<ProcedureViewModel>(
      onModelReady: (model) {
        model.getProcedure(mrn: patient.patientId, patientType: patientType, appointmentNo: patient.appointmentNo);
        model.isPrincipalCovered(patient: patient);
      },
      builder: (BuildContext context, ProcedureViewModel model, Widget? child) => AppScaffold(
        isShowAppBar: true,
        backgroundColor: Colors.grey[100]!,
        baseViewModel: model,
        appBar: PatientProfileAppBar(
          patient,
          isInpatient: isInpatient,
        ),
        body: SingleChildScrollView(
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 12,
                ),
                if ((model.procedureList.length == 0 && patient.patientStatusType != 43) || patient.patientStatusType != null && patient.patientStatusType == 43)
                  ServiceTitle(
                    title: TranslationBase.of(context).orderTestOr,
                    subTitle: TranslationBase.of(context).procedure,
                  ),
                if ((patient.patientStatusType != null && patient.patientStatusType == 43) || (isFromLiveCare && patient.appointmentNo != null))
                  AddNewOrder(
                    onTap: () async {
                      GifLoaderDialogUtils.showMyDialog(context);
                      await model.getProcedureTemplate(categoryID: ProcedureType.PROCEDURE.getCategoryId(), isLocalBusy: true);
                      GifLoaderDialogUtils.hideDialog(context);

                      Navigator.push(
                        context,
                        SlideUpPageRoute(
                            widget: BaseAddProcedureTabPage(
                              patient: patient,
                              previousProcedureViewModel: model,
                              procedureType: ProcedureType.PROCEDURE,
                            ),
                            settingRoute: 'AddProcedureTabPage'),
                      );
                    },
                    label: TranslationBase.of(context).addMoreProcedure,
                  ),
                !model.isPrincipalCovered_
                    ? Center(
                        child: AppText(
                        TranslationBase.of(context).principalCoveredOrNot,
                        color: Colors.red,
                        textAlign: TextAlign.center,
                      ))
                    : SizedBox(),
                SizedBox(
                  height: 20,
                ),
                if (model.procedureList.isNotEmpty)
                  ListView.builder(
                      scrollDirection: Axis.vertical,
                      itemCount: model.procedureList[0].rowcount,
                      shrinkWrap: true,
                      physics: BouncingScrollPhysics(),
                      itemBuilder: (BuildContext ctxt, int index) {
                        return ProcedureCard(
                          categoryID: model.procedureList[0].entityList![index].categoryID,
                          entityList: model.procedureList[0].entityList![index],
                          onTap: () {
                            if (model.procedureList[0].entityList![index].categoryID == 2 || model.procedureList[0].entityList![index].categoryID == 4)
                              updateProcedureForm(
                                context,
                                model: model,
                                patient: patient,
                                remarks: model.procedureList[0].entityList![index].remarks!,
                                orderType: model.procedureList[0].entityList![index].orderType.toString(),
                                orderNo: model.procedureList[0].entityList![index].orderNo!,
                                procedureName: model.procedureList[0].entityList![index].procedureName!,
                                categoreId: model.procedureList[0].entityList![index].categoryID.toString(),
                                procedureId: model.procedureList[0].entityList![index].procedureId!,
                                limetNo: model.procedureList[0].entityList![index].lineItemNo!,
                              );
                          },
                          patient: patient,
                          doctorID: model.doctorProfile?.doctorID,
                        );
                      }),
                if (model.state == ViewState.ErrorLocal || (model.procedureList.isNotEmpty && model.procedureList[0].entityList!.isEmpty))
                  Center(
                      child: ErrorMessage(
                    error: TranslationBase.of(context).noDataAvailable,
                  )),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
