import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_template_details_model.dart';
import 'package:doctor_app_flutter/core/viewModel/prescription/prescription_view_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/prescription/prescription_checkout_screen.dart';
import 'package:doctor_app_flutter/screens/procedures/favorite_procedure/entity_list_fav_procedure.dart';
import 'package:doctor_app_flutter/screens/procedures/favorite_procedure/procedure_checkout_screen.dart';
import 'package:doctor_app_flutter/screens/procedures/procedure_type.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/bottom_sheet/custom_bottom_sheet_container.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/error_message.dart';
import 'package:flutter/material.dart';

class AddFavouriteProcedure extends StatefulWidget {
  final ProcedureViewModel? previousProcedureViewModel;
  final PrescriptionViewModel? prescriptionModel;
  final PatiantInformtion? patient;
  final ProcedureType? procedureType;

  AddFavouriteProcedure({
    Key? key,
    this.previousProcedureViewModel,
    this.prescriptionModel,
    this.patient,
    required this.procedureType,
  });

  @override
  _AddFavouriteProcedureState createState() => _AddFavouriteProcedureState();
}

class _AddFavouriteProcedureState extends State<AddFavouriteProcedure> {
  _AddFavouriteProcedureState({this.patient, this.model});

  ProcedureViewModel? model;
  PatiantInformtion? patient;
  List<ProcedureTempleteDetailsModel> entityList = [];
  ProcedureTempleteDetailsModel groupProcedures = ProcedureTempleteDetailsModel();

  @override
  Widget build(BuildContext context) {
    return BaseView<ProcedureViewModel>(
      builder: (BuildContext context, ProcedureViewModel procedureViewModel, Widget? child) => AppScaffold(
          isShowAppBar: false,
          body: Column(children: [
            (widget.previousProcedureViewModel!.templateList.length != 0)
                ? Expanded(
                    child: EntityListCheckboxSearchFavProceduresWidget(
                      isProcedure: !(widget.procedureType == ProcedureType.PRESCRIPTION),
                      model: widget.previousProcedureViewModel!,
                      removeFavProcedure: (item) {
                        setState(() {
                          entityList.remove(item);
                        });
                      },
                      addFavProcedure: (history) {
                        setState(() {
                          entityList.add(history);
                        });
                      },
                      isEntityFavListSelected: (master) => procedureViewModel.isProcedureEntityListSelected(master, entityList),
                      groupProcedures: groupProcedures,
                      selectProcedures: (selectedProcedure) {
                        setState(() {
                          groupProcedures = selectedProcedure;
                        });
                      },
                    ),
                  )
                : ErrorMessage(
                    error: TranslationBase.of(context).youDoNotHaveFavoriteTemplate,
                  ),
          ]),
          bottomSheet: widget.previousProcedureViewModel!.templateList.length == 0
              ? Container(
                  height: 0,
                )
              : CustomBottomSheetContainer(
                  label: widget.procedureType!.getAddButtonTitle(context) ?? TranslationBase.of(context).addSelectedProcedures,
                  onTap: () async {
                    if (widget.procedureType == ProcedureType.PRESCRIPTION) {
                      if (groupProcedures == null) {
                        DrAppToastMsg.showErrorToast(
                          TranslationBase.of(context).pleaseSelectItem,
                        );
                        return;
                      }

                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => PrescriptionCheckOutScreen(
                                  patient: widget.patient!,
                                  model: widget.prescriptionModel!,
                                  groupProcedures: groupProcedures,
                                ),
                            settings: RouteSettings(name: 'PrescriptionCheckOutScreen')),
                      );
                    } else {
                      if (entityList.isEmpty == true) {
                        DrAppToastMsg.showErrorToast(
                          TranslationBase.of(context).fillTheMandatoryProcedureDetails,
                        );
                        return;
                      }
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ProcedureCheckOutScreen(
                                  items: entityList,
                                  previousProcedureViewModel: widget.previousProcedureViewModel!,
                                  patient: widget.patient!,
                                  addButtonTitle: widget.procedureType!.getAddButtonTitle(context),
                                  toolbarTitle: widget.procedureType!.getToolbarLabel(context),
                                ),
                            settings: RouteSettings(name: 'ProcedureCheckOutScreen')),
                      );
                    }
                  })),
    );
  }
}
