import 'dart:async';

import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HomeHealthCare/patient_er_insert_pres_order_request_model.dart';
import 'package:hmg_patient_app/core/viewModels/AlHabibMedicalService/home_health_care_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/AlHabibMedicalService/ComprehensiveMedicalCheckup/Dialog/confirm_dialog.dart';
import 'package:hmg_patient_app/pages/AlHabibMedicalService/HomeHealthCare/home_health_care_page.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';

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

  NewHomeHealthCareStepThreePage({Key? key, this.patientERInsertPresOrderRequestModel,  this.changePageViewIndex, required this.model});

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

class _NewHomeHealthCareStepThreePageState extends State<NewHomeHealthCareStepThreePage> {
  Completer<GoogleMapController> _controller = Completer();

  static CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );
  final Set<Marker> markers = new Set();

  @override
  void initState() {
    if (widget.patientERInsertPresOrderRequestModel!.latitude != null) {
      markers.clear();
      markers.add(
        Marker(
            markerId: MarkerId(
              widget.patientERInsertPresOrderRequestModel!.latitude.hashCode.toString(),
            ),
            position: LatLng(widget.patientERInsertPresOrderRequestModel!.latitude!, widget.patientERInsertPresOrderRequestModel!.longitude!)),
      );
      _kGooglePlex = CameraPosition(
        target: LatLng(widget.patientERInsertPresOrderRequestModel!.latitude!, widget.patientERInsertPresOrderRequestModel!.longitude!),
        zoom: 14.4746,
      );
    }
    super.initState();
  }

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

    return AppScaffold(
      isShowDecPage: false,
      baseViewModel: widget.model,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      appBarTitle: TranslationBase.of(context).homeHealthCare,
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Container(
          width: double.maxFinite,
          margin: EdgeInsets.only(left: 12, right: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                  padding: EdgeInsets.only(top: 12.0, bottom: 12.0),
                  child: Texts(
                    TranslationBase.of(context).RRTSummary,
                    fontWeight: FontWeight.bold,
                    fontSize: 22,
                  )),
              Container(
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(12)),
                padding: EdgeInsets.all(12),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      TranslationBase.of(context).selectedLocation,
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w600,
                        color: CustomColors.textColor,
                        letterSpacing: -0.56,
                      ),
                    ),
                    SizedBox(
                      height: 12,
                    ),
                    Container(
                        height: 200,
                        decoration: containerColorRadiusBorderWidth(Colors.transparent, 12,Colors.transparent,0.5),
                        clipBehavior: Clip.antiAlias,
                        child: Image.network(
                          "https://maps.googleapis.com/maps/api/staticmap?center=" +
                              widget.patientERInsertPresOrderRequestModel!.latitude.toString() +
                              "," +
                              widget.patientERInsertPresOrderRequestModel!.longitude.toString() +
                              "&zoom=16&size=600x300&maptype=roadmap&markers=color:red%7C" +
                              widget.patientERInsertPresOrderRequestModel!.latitude.toString() +
                              "," +
                              widget.patientERInsertPresOrderRequestModel!.longitude.toString() +
                              "&key=AIzaSyCyDbWUM9d_sBUGIE8PcuShzPaqO08NSC8",
                          width: double.infinity,
                          height: double.infinity,
                          fit: BoxFit.cover,
                        )),
                    SizedBox(
                      height: 12,
                    ),
                    Text(
                      TranslationBase.of(context).selectedService,
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w600,
                        color: CustomColors.textColor,
                        letterSpacing: -0.56,
                      ),
                    ),
                    ...List.generate(
                      widget.patientERInsertPresOrderRequestModel!.patientERHHCInsertServicesList!.length,
                      (index) => Container(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // Texts(
                            //   TranslationBase.of(context).serviceName,
                            //   fontSize: 12,
                            // ),
                            SizedBox(
                              height: 5,
                            ),
                            Row(
                              children: [
                                Text(
                                  (index + 1).toString() + '. ',
                                  style: TextStyle(
                                    fontSize: 12,
                                    fontWeight: FontWeight.w600,
                                    color: CustomColors.black,
                                    letterSpacing: -0.48,
                                  ),
                                ),
                                Text(
                                  widget.patientERInsertPresOrderRequestModel!.patientERHHCInsertServicesList![index].serviceName!,
                                  style: TextStyle(
                                    fontSize: 12,
                                    fontWeight: FontWeight.w600,
                                    color: CustomColors.black,
                                    letterSpacing: -0.48,
                                  ),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 57,
              ),
            ],
          ),
        ),
      ),
      bottomSheet: Container(
        color: Theme.of(context).scaffoldBackgroundColor,
        width: double.infinity,
        child: Padding(
          padding: const EdgeInsets.all(12.0),
          child: DefaultButton(
            TranslationBase.of(context).confirm,
            () {
              widget.model.setState(ViewState.Busy);
              widget.model.insertPresPresOrder(order: widget.patientERInsertPresOrderRequestModel!).then((value) {
                widget.model.setState(ViewState.Idle);
                if (widget.model.state != ViewState.ErrorLocal) {
                  if (widget.model.orderId != null) {
                    showCMCConfirmDialog(
                      context,
                      widget.model.orderId.toString(),
                      onClick: () {
                        Navigator.popUntil(context, (route) => Utils.route(route, equalsTo: HomeHealthCarePage));
                        Navigator.push(context, FadePage(page: HomeHealthCarePage()));
                      },
                    );
                  }
                } else {
                  AppToast.showErrorToast(message: widget.model.error);
                }
              });
            },
            color: CustomColors.green,
          ),
        ),

        // SecondaryButton(
        //     label: TranslationBase.of(context).confirm,
        //     disabled: widget.patientERInsertPresOrderRequestModel.patientERHHCInsertServicesList.length == 0,
        //     color: Colors.grey[800],
        //     onTap: () async {
        //       widget.model.setState(ViewState.Busy);
        //       widget.model.insertPresPresOrder(order: widget.patientERInsertPresOrderRequestModel).then((value) => {
        //             widget.model.setState(ViewState.Idle),
        //             Navigator.push(
        //               context,
        //               FadePage(
        //                 page: HomeHealthCarePage(),
        //               ),
        //             )
        //           });
        //     },
        //     textColor: Theme.of(context).backgroundColor),
      ),
    );
  }
}
