import 'dart:async';

import 'package:diplomaticquarterapp/core/enum/viewstate.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/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_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,
      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,
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Container(
          width: double.maxFinite,
          margin: EdgeInsets.only(left: 12, right: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Texts(TranslationBase.of(context).orderDetails, fontWeight: FontWeight.bold,),
              SizedBox(
                height: 12,
              ),
              Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12)),
                padding: EdgeInsets.all(8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Texts(TranslationBase.of(context).orderLocation, fontWeight: FontWeight.bold),
                    SizedBox(
                      height: 12,
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        height: 200,
                        child: GoogleMap(
                          mapType: MapType.normal,
                          markers: markers,
                          initialCameraPosition: _kGooglePlex,
                          onMapCreated: (GoogleMapController controller) {
                            _controller.complete(controller);
                          },
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 12,
                    ),
                    Texts(TranslationBase.of(context).selectedService + " : ", fontWeight: FontWeight.bold),
                    ...List.generate(
                      widget.patientERInsertPresOrderRequestModel
                          .patientERHHCInsertServicesList.length,
                      (index) => Container(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Texts(
                              TranslationBase.of(context).serviceName,
                              fontSize: 12,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Texts(
                              widget
                                  .patientERInsertPresOrderRequestModel
                                  .patientERHHCInsertServicesList[index]
                                  .serviceName,
                              fontSize: 15,
                              bold: true,
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 57,
              ),
            ],
          ),
        ),
      ),
      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).confirm,
                  disabled: widget.patientERInsertPresOrderRequestModel
                          .patientERHHCInsertServicesList.length ==
                      0,
                  color: Colors.grey[800],
                  onTap: () async {
                    await widget.model.insertPresPresOrder(
                        order: widget.patientERInsertPresOrderRequestModel);
                    if (widget.model.state != ViewState.ErrorLocal) {
                      widget.changePageViewIndex(0);
                    }
                  },
                  textColor: Theme.of(context).backgroundColor),
            ),
          ],
        ),
      ),
    );
  }
}
