import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/HHC_get_all_services_response_model.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/pages/AlHabibMedicalService/HomeHealthCare/NewHomeHealthCare/new_Home_health_care_step_tow_page.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.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: Column(
        children: [
          Expanded(
            child: ListView.separated(
                physics: BouncingScrollPhysics(),
                shrinkWrap: true,
                padding: EdgeInsets.only(left: 21, right: 21, top: 12, bottom: 12),
                itemBuilder: (context, index) {
                  HHCGetAllServicesResponseModel service = widget.model.hhcAllServicesList[index];

                  return Padding(
                    padding: const EdgeInsets.only(top: 4, bottom: 4),
                    child: Row(
                      children: [
                        Checkbox(
                            value: isServiceSelected(num.tryParse(service.serviceID)),
                            activeColor: Color(0xffD02127),
                            tristate: false,
                            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                            onChanged: (bool newValue) {
                              setState(() {
                                if (!isServiceSelected(num.tryParse(service.serviceID)))
                                  widget.patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList.add(PatientERHHCInsertServicesList(
                                      recordID: widget.patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList.length,
                                      serviceID: num.tryParse(service.serviceID),
                                      serviceName: projectViewModel.isArabic ? service.textN : service.text));
                                else
                                  removeSelected(num.tryParse(service.serviceID));
                                // widget.patientERInsertPresOrderRequestModel
                                // isPatientInsured = newValue;
                              });
                            }),
                        SizedBox(width: 6),
                        Expanded(
                          child: Text(
                            projectViewModel.isArabic ? service.textN : service.text.toLowerCase()?.capitalize(),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.64),
                          ),
                        ),
                      ],
                    ),
                  );
                },
                separatorBuilder: (context, index) => Divider(
                      height: 1,
                      color: Color(0xffE5E5E5),
                    ),
                itemCount: widget.model.hhcAllServicesList.length),
          ),
          Container(
            color: Colors.white,
            padding: EdgeInsets.only(top: 16, bottom: 16, right: 21, left: 21),
            child: DefaultButton(
              TranslationBase.of(context).next,
              (this.widget.patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList.length == 0 || widget.model.state == ViewState.BusyLocal)
                  ? null
                  : () async {
                      widget.model.setState(ViewState.Busy);
                      await widget.model.getCustomerInfo();
                      Navigator.push(
                        context,
                        FadePage(
                          page: NewHomeHealthCareStepTowPage(
                            patientERInsertPresOrderRequestModel: widget.patientERInsertPresOrderRequestModel,
                            model: widget.model,
                          ),
                        ),
                      );
                      // }
                    },
              disabledColor: Color(0xff575757),
            ),
          ),
        ],
      ),
    );
  }

  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);
      });
  }
}
