import 'dart:async';

import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/patient_er_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/cmc_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/home_health_care_view_model.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';

class NewCMCStepThreePage extends StatefulWidget {
  final CMCInsertPresOrderRequestModel cmcInsertPresOrderRequestModel;
  final Function changePageViewIndex;
  final CMCViewModel model;

  NewCMCStepThreePage(
      {Key key,
      this.changePageViewIndex,
      this.model,
        this.cmcInsertPresOrderRequestModel});

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

class _NewCMCStepThreePageState
    extends State<NewCMCStepThreePage> {
  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.cmcInsertPresOrderRequestModel.latitude != null) {
      markers.clear();
      markers.add(
        Marker(
            markerId: MarkerId(
              widget.cmcInsertPresOrderRequestModel.latitude.hashCode
                  .toString(),
            ),
            position: LatLng(
                widget.cmcInsertPresOrderRequestModel.latitude,
                widget.cmcInsertPresOrderRequestModel.longitude)),
      );
      _kGooglePlex = CameraPosition(
        target: LatLng(widget.cmcInsertPresOrderRequestModel.latitude,
            widget.cmcInsertPresOrderRequestModel.longitude),
        zoom: 14.4746,
      );
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowDecPage: false,
      baseViewModel: widget.model,
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Container(
          height: 400,
          width: double.maxFinite,
          margin: EdgeInsets.only(left: 12, right: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Texts('Order Details'),
              SizedBox(
                height: 12,
              ),
              Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12)),
                padding: EdgeInsets.all(8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Texts('Location :'),
                    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('Selected Service :'),
                    ...List.generate(
                      widget.cmcInsertPresOrderRequestModel.patientERCMCInsertServicesList.length,
                      (index) => Container(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Texts(
                              'Service Name :',
                              fontSize: 12,
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Texts(
                              widget
                                  .cmcInsertPresOrderRequestModel.patientERCMCInsertServicesList[index]
                                  .selectedServiceName,
                              fontSize: 15,
                              bold: true,
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
      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: "Confirm",
                  onTap: () async {
                    await widget.model.insertPresPresOrder(order: widget.cmcInsertPresOrderRequestModel);
                    if (widget.model.state != ViewState.ErrorLocal) {
                      widget.changePageViewIndex(0);
                    }
                  },
                  textColor: Theme.of(context).backgroundColor),
            ),
          ],
        ),
      ),
    );
  }
}
