import 'package:diplomaticquarterapp/config/config.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/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/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/close_back.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_place_picker/google_maps_place_picker.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';

class NewCMCStepTowPage extends StatefulWidget {
  final Function(PickResult) onPick;
  final double latitude;
  final double longitude;
  final CMCInsertPresOrderRequestModel cmcInsertPresOrderRequestModel;
  final Function changePageViewIndex;
  final CMCViewModel model;

  const NewCMCStepTowPage(
      {Key key,
      this.onPick,
      this.latitude,
      this.longitude,
      this.cmcInsertPresOrderRequestModel,
      this.changePageViewIndex,
      this.model})
      : super(key: key);

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

class _NewCMCStepTowPageState
    extends State<NewCMCStepTowPage> {
  double latitude = 0;
  double longitude = 0;

  @override
  void initState() {
    if (widget.cmcInsertPresOrderRequestModel.latitude == null) {
      latitude = widget.latitude;
      longitude = widget.longitude;
    } else {
      latitude = widget.cmcInsertPresOrderRequestModel.latitude;
      longitude = widget.cmcInsertPresOrderRequestModel.longitude;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppScaffold(
      isShowDecPage: false,
      body: PlacePicker(
        apiKey: GOOGLE_API_KEY,
        enableMyLocationButton: true,
        automaticallyImplyAppBarLeading: false,
        autocompleteOnTrailingWhitespace: true,
        selectInitialPosition: true,
        autocompleteLanguage: projectViewModel.currentLanguage,
        enableMapTypeButton: true,
        onPlacePicked: (PickResult result) {
          print(result.adrAddress);
          widget.changePageViewIndex(3);
        },
        selectedPlaceWidgetBuilder:
            (_, selectedPlace, state, isSearchBarFocused) {
          print("state: $state, isSearchBarFocused: $isSearchBarFocused");
          return isSearchBarFocused
              ? Container()
              : FloatingCard(
                  bottomPosition: 0.0,
                  leftPosition: 0.0,
                  rightPosition: 0.0,
                  width: 500,
                  borderRadius: BorderRadius.circular(12.0),
                  child: state == SearchingState.Searching
                      ? Center(child: CircularProgressIndicator())
                      : Container(
                          margin: EdgeInsets.all(12),
                          child: SecondaryButton(
                            color: Colors.grey[800],
                            textColor: Colors.white,
                            onTap: () {
                              setState(() {
                                widget.cmcInsertPresOrderRequestModel
                                        .latitude =
                                    selectedPlace.geometry.location.lat;
                                widget.cmcInsertPresOrderRequestModel
                                        .longitude =
                                    selectedPlace.geometry.location.lng;
                              });
                              widget.changePageViewIndex(3);
                            },
                            label: TranslationBase.of(context).next,
                          ),
                        ),
                );
        },
        initialPosition: LatLng(latitude, longitude),
        useCurrentLocation: true,
      ),
    );
  }
}
