import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_template_details_model.dart';
import 'package:doctor_app_flutter/core/viewModel/prescription_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_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/entity_list_fav_procedure.dart';
import 'package:doctor_app_flutter/screens/procedures/procedure_checkout_screen.dart';
import 'package:doctor_app_flutter/util/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'ProcedureType.dart';

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

  AddFavouriteProcedure({
    Key key,
    this.model,
    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 = List();
  ProcedureTempleteDetailsModel groupProcedures;

  @override
  Widget build(BuildContext context) {
    return BaseView<ProcedureViewModel>(
      onModelReady: (model) => model.getProcedureTemplate(categoryID: widget.procedureType.getCategoryId()),
      builder: (BuildContext context, ProcedureViewModel model, Widget child) => AppScaffold(
        isShowAppBar: false,
        baseViewModel: model,
        body: Column(
          children: [
            Container(
              height: MediaQuery.of(context).size.height * 0.070,
            ),
            (model.templateList.length != 0)
                ? Expanded(
                    child: EntityListCheckboxSearchFavProceduresWidget(
                      isProcedure: !(widget.procedureType == ProcedureType.PRESCRIPTION),
                      model: model,
                      removeFavProcedure: (item) {
                        setState(() {
                          entityList.remove(item);
                        });
                      },
                      addFavProcedure: (history) {
                        setState(() {
                          entityList.add(history);
                        });
                      },
                      isEntityFavListSelected: (master) => isEntityListSelected(master),
                      groupProcedures: groupProcedures,
                      selectProcedures: (selectedProcedure) {
                        setState(() {
                          groupProcedures = selectedProcedure;
                        });
                      },
                    ),
                  )
                : Container(
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 50.0),
                      child: AppText('You Don\'t have favorite prescription'),
                    ),
                  ),
          ],
        ),
        bottomSheet: Container(
          margin: EdgeInsets.all(SizeConfig.widthMultiplier * 5),
          child: Wrap(
            alignment: WrapAlignment.center,
            children: <Widget>[
              AppButton(
                title: widget.procedureType.getAddButtonTitle(context) ??
                    TranslationBase.of(context).addSelectedProcedures,
                color: Color(0xff359846),
                disabled: model.templateList.length == 0 ? true : false,
                fontWeight: FontWeight.w700,
                onPressed: () {
                  if (widget.procedureType == ProcedureType.PRESCRIPTION) {
                    if (groupProcedures == null) {
                      DrAppToastMsg.showErrorToast(
                        'Please Select item ',
                      );
                      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,
                                model: model,
                                patient: widget.patient,
                                addButtonTitle: widget.procedureType.getAddButtonTitle(context),
                                toolbarTitle: widget.procedureType.getToolbarLabel(context),
                              ),
                          settings: RouteSettings(name: 'ProcedureCheckOutScreen')),
                    );
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  bool isEntityListSelected(ProcedureTempleteDetailsModel masterKey) {
    Iterable<ProcedureTempleteDetailsModel> history = entityList.where(
        (element) => masterKey.templateID == element.templateID && masterKey.procedureName == element.procedureName);
    if (history.length > 0) {
      return true;
    }
    return false;
  }
}
