import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:google_maps_flutter/google_maps_flutter.dart';

class AppMap extends StatefulWidget {
  final CameraPositionCallback onCameraMove;
  final VoidCallback onCameraIdle;
  Map initialCamera;

  late AppMapState _state;
  final Function onMapCreated;

  AppMap(this.initialCamera, {required this.onCameraMove, required this.onCameraIdle, required this.onMapCreated});

  @override
  State<StatefulWidget> createState() => _state = AppMapState();

  moveTo({required CameraPosition cameraPostion}) {
    _state.googleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPostion));
  }
}

class AppMapState extends State<AppMap> {
  bool isHuawei = false;

  final Completer<GoogleMapController> _googleMapControllerComp = Completer();
  late GoogleMapController googleMapController;

  checkIsHuawei() async {
    setState(() {});
  }

  @override
  void initState() {}

  @override
  void dispose() {
    super.dispose();
    googleMapController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (isHuawei == null) return const CircularProgressIndicator();
    return googleMap();
  }

  Widget googleMap() {
    return GoogleMap(
      mapType: MapType.normal,
      zoomControlsEnabled: true,
      myLocationButtonEnabled: true,
      myLocationEnabled: true,
      initialCameraPosition: CameraPosition.fromMap(widget.initialCamera) ?? const CameraPosition(target: LatLng(-26.1711459, 27.9002758), zoom: 2.0),
      onCameraMove: widget.onCameraMove,
      onCameraIdle: widget.onCameraIdle,
      onMapCreated: (GoogleMapController controller) {
        googleMapController = controller;
        _googleMapControllerComp.complete(controller);
        widget.onMapCreated();
      },
    );
  }
}
