import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/PatientERHHCInsertServicesList.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/patient_er_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/home_health_care_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_place_picker/google_maps_place_picker.dart';
import 'package:provider/provider.dart';

class NewHomeHealthCareStepOnePage extends StatefulWidget {
  final PatientERInsertPresOrderRequestModel
      patientERInsertPresOrderRequestModel;
  final Function changePageViewIndex;
  final HomeHealthCareViewModel model;

  const NewHomeHealthCareStepOnePage(
      {Key key,
      this.patientERInsertPresOrderRequestModel,
      this.model,
      this.changePageViewIndex})
      : super(key: key);

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

class _NewHomeHealthCareStepOnePageState
    extends State<NewHomeHealthCareStepOnePage> {
  PickResult _result;

  @override
  void initState() {
    if (widget.patientERInsertPresOrderRequestModel
            .patientERHHCInsertServicesList ==
        null)
      widget.patientERInsertPresOrderRequestModel
          .patientERHHCInsertServicesList = [];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    return AppScaffold(
      isShowAppBar: false,
      baseViewModel: widget.model,
      body: SingleChildScrollView(
        physics: ScrollPhysics(),
        child: Container(
          margin: EdgeInsets.only(
              left: MediaQuery.of(context).size.width * 0.05,
              right: MediaQuery.of(context).size.width * 0.05),
          child: Center(
            child: FractionallySizedBox(
              widthFactor: 1,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 12,
                  ),
                  Texts(
                    TranslationBase.of(context).selectHomeHealthCareServices,
                    textAlign: TextAlign.center,
                  ),
                  Column(
                    children: widget.model.hhcAllServicesList.map((service) {
                      return Container(
                        margin: EdgeInsets.only(top: 15),
                        decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey, width: 1),
                            borderRadius: BorderRadius.circular(12),
                            color: Theme.of(context).primaryColor),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                Checkbox(
                                    value: isServiceSelected(service.serviceID),
                                    activeColor: Colors.red[800],
                                    onChanged: (bool newValue) {
                                      setState(() {
                                        if (!isServiceSelected(
                                            service.serviceID))
                                          widget
                                              .patientERInsertPresOrderRequestModel
                                              .patientERHHCInsertServicesList
                                              .add(PatientERHHCInsertServicesList(
                                                  recordID: widget
                                                      .patientERInsertPresOrderRequestModel
                                                      .patientERHHCInsertServicesList
                                                      .length,
                                                  serviceID: service.serviceID,
                                                  serviceName:
                                                      service.description));
                                        else
                                          removeSelected(service.serviceID);
                                        // widget.patientERInsertPresOrderRequestModel
                                        // isPatientInsured = newValue;
                                      });
                                    }),
                                Expanded(
                                  child: Padding(
                                    padding: const EdgeInsets.all(20.0),
                                    child: Texts(
                                      projectViewModel.isArabic
                                          ? service.descriptionN
                                          : service.description,
                                      fontSize: 15,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      );
                    }).toList(),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
      bottomSheet: Container(
        height: MediaQuery.of(context).size.height * 0.10,
        width: double.infinity,
        child: Column(
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width * 0.9,
              child: SecondaryButton(
                  label: TranslationBase.of(context).next,
                  disabled: this
                              .widget
                              .patientERInsertPresOrderRequestModel
                              .patientERHHCInsertServicesList
                              .length ==
                          0 ||
                      widget.model.state == ViewState.BusyLocal,
                  color: Colors.grey[800],
                  loading: widget.model.state == ViewState.BusyLocal,
                  onTap: () async {
                    await widget.model.getCustomerInfo();
                    if (widget.model.state == ViewState.ErrorLocal) {
                      Utils.showErrorToast();
                    } else {
                      widget.changePageViewIndex(1);
                    }
                  },
                  textColor: Theme.of(context).backgroundColor),
            ),
          ],
        ),
      ),
    );
  }

  isServiceSelected(int serviceId) {
    Iterable<PatientERHHCInsertServicesList> patientERHHCInsertServicesList =
        widget
            .patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList
            .where((element) => serviceId == element.serviceID);
    if (patientERHHCInsertServicesList.length > 0) {
      return true;
    }
    return false;
  }

  removeSelected(int serviceId) {
    Iterable<PatientERHHCInsertServicesList> patientERHHCInsertServicesList =
        widget
            .patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList
            .where((element) => serviceId == element.serviceID);

    if (patientERHHCInsertServicesList.length > 0)
      setState(() {
        widget
            .patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList
            .remove(patientERHHCInsertServicesList.first);
      });
  }
}
