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/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/cmc_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/ComprehensiveMedicalCheckup/Dialog/confirm_dialog.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.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 NewCMCStepThreePage extends StatefulWidget {
  final CMCInsertPresOrderRequestModel cmcInsertPresOrderRequestModel;
  final CMCViewModel model;

  NewCMCStepThreePage({Key key, 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) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppScaffold(
      isShowAppBar: true,
      description: TranslationBase.of(context).infoCMC,
      imagesInfo: [
        ImagesInfo(imageAr: 'https://hmgwebservices.com/Images/MobileApp/CMC/ar/0.png', imageEn: 'https://hmgwebservices.com/Images/MobileApp/CMC/en/0.png'),
      ],
      appBarTitle: TranslationBase.of(context).comprehensiveMedicalCheckup,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      backgroundColor: CustomColors.appBackgroudGreyColor,
      baseViewModel: widget.model,
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Container(
          height: 500,
          width: double.maxFinite,
          margin: EdgeInsets.only(left: 12, right: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 12,
              ),
              Text(
                TranslationBase.of(context).orderSummary,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  letterSpacing: -0.46,
                ),
              ),
              SizedBox(
                height: 12,
              ),
              Container(
                decoration: cardRadius(12),
                child: Container(
                  padding: EdgeInsets.all(8),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        TranslationBase.of(context).orderDetails + " : ",
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          letterSpacing: -0.46,
                          color: CustomColors.grey,
                        ),
                      ),
                      SizedBox(
                        height: 6,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          height: 200,
                          decoration: containerColorRadiusBorder(Colors.white, 12, Colors.grey),
                          clipBehavior: Clip.antiAlias,
                          child: Container(
                            decoration: cardRadius(12),
                            clipBehavior: Clip.antiAlias,
                            margin: const EdgeInsets.all(0),
                            // child: GoogleMap(
                            //   mapType: MapType.normal,
                            //   markers: markers,
                            //   initialCameraPosition: _kGooglePlex,
                            //   onMapCreated: (GoogleMapController controller) {
                            //     _controller.complete(controller);
                            //   },
                            // ),
                            child: Image.network(
                              "https://maps.googleapis.com/maps/api/staticmap?center=" +
                                  widget.cmcInsertPresOrderRequestModel.latitude.toString() +
                                  "," +
                                  widget.cmcInsertPresOrderRequestModel.longitude.toString() +
                                  "&zoom=16&size=600x300&maptype=roadmap&markers=color:red%7C" +
                                  widget.cmcInsertPresOrderRequestModel.latitude.toString() +
                                  "," +
                                  widget.cmcInsertPresOrderRequestModel.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.bold,
                          letterSpacing: -0.46,
                          color: CustomColors.grey,
                        ),
                      ),
                      ...List.generate(
                        widget.cmcInsertPresOrderRequestModel.patientERCMCInsertServicesList.length,
                        (index) => Container(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                projectViewModel.isArabic
                                    ? widget.cmcInsertPresOrderRequestModel.patientERCMCInsertServicesList[index].selectedServiceNameAR
                                    : widget.cmcInsertPresOrderRequestModel.patientERCMCInsertServicesList[index].selectedServiceName,
                                style: TextStyle(
                                  fontSize: 12,
                                  fontWeight: FontWeight.bold,
                                  letterSpacing: -0.46,
                                ),
                              ),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
      bottomSheet: Container(
        height: MediaQuery.of(context).size.height * 0.10,
        width: double.infinity,
        color: Colors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width * 0.9,
              child: SecondaryButton(
                  label: TranslationBase.of(context).confirm,
                  color: CustomColors.green,
                  onTap: () async {
                    GifLoaderDialogUtils.showMyDialog(context);
                    String requestId = await widget.model.insertCMCOrderRC(order: widget.cmcInsertPresOrderRequestModel);
                    GifLoaderDialogUtils.hideDialog(context);
                    if (widget.model.state != ViewState.ErrorLocal) {
                      //show scuccess dialog
                      showCMCConfirmDialog(
                        context,
                        requestId,
                        onClick: () {
                          Navigator.pop(context);
                          Navigator.pop(context);
                        },
                      );
                    } else {
                      AppToast.showErrorToast(message: widget.model.error);
                    }
                  },
                  textColor: Theme.of(context).backgroundColor),
            ),
          ],
        ),
      ),
    );
  }
}
