import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart' as googlemap;

// import 'package:flutter_hms_gms_availability/flutter_hms_gms_availability.dart';
import 'package:huawei_hmsavailability/huawei_hmsavailability.dart';
import 'package:huawei_map/huawei_map.dart' as hmsMap;

class AppMap extends StatefulWidget {
  late dynamic onCameraMove;
  late Map initialCamera;

  AppMapState? _state;
  Function? onMapCreated;
  Function? onCameraIdle;

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

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

  moveTo({required googlemap.CameraPosition cameraPostion}) {
    if (_state!.isHuawei) {
      final cameraJson = cameraPostion.toMap();
      final camera = hmsMap.CameraPosition.fromMap(cameraJson);
      _state?._huaweiMapController
          .animateCamera(hmsMap.CameraUpdate.newCameraPosition(camera));
    } else {
      _state?.googleMapController.animateCamera(
          googlemap.CameraUpdate.newCameraPosition(
              cameraPostion as googlemap.CameraPosition));
    }
  }
}

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

  Completer<googlemap.GoogleMapController> _googleMapControllerComp =
      Completer();
  late googlemap.GoogleMapController googleMapController;

  Completer<hmsMap.HuaweiMapController> _huaweiMapControllerComp = Completer();
  late hmsMap.HuaweiMapController _huaweiMapController;

  late HmsApiAvailability hmsApiAvailability;

  checkIsHuawei() async {
    await hmsApiAvailability.isHMSAvailable().then((value) {
      isHuawei = value == 0 ? true : false;
      hmsMap.HuaweiMapInitializer.initializeMap();
    });
    print(isHuawei);
    setState(() {});
  }

  @override
  void initState() {
    hmsApiAvailability = HmsApiAvailability();
    if (Platform.isAndroid) checkIsHuawei();
    super.initState();
  }

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

  Widget map() => isHuawei ? huaweiMap() : googleMap();

  Widget googleMap() {
    return googlemap.GoogleMap(
      mapType: googlemap.MapType.normal,
      zoomControlsEnabled: true,
      myLocationButtonEnabled: true,
      myLocationEnabled: true,
      initialCameraPosition:
          googlemap.CameraPosition.fromMap!(widget.initialCamera)!,
      onCameraMove: (camera) {
        widget.onCameraMove;
      },
      onMapCreated: (googlemap.GoogleMapController controller) {
        googleMapController = controller;
        _googleMapControllerComp.complete(controller);
        widget.onMapCreated!();
      },
      onCameraIdle: () {
        print("onCameraIdle");
        widget.onCameraIdle;
      },
    );
  }

  Widget huaweiMap() {
    return hmsMap.HuaweiMap(
      mapType: hmsMap.MapType.normal,
      zoomControlsEnabled: false,
      myLocationButtonEnabled: true,
      myLocationEnabled: true,
      initialCameraPosition:
          hmsMap.CameraPosition.fromMap(widget.initialCamera),
      onCameraMove: (camera) => widget.onCameraMove!,
      onMapCreated: (controller) {
        _huaweiMapController = controller;
        _huaweiMapControllerComp.complete(controller);
        widget.onMapCreated!();
      },
      onCameraIdle: () {
        print("onCameraIdle");
        widget.onCameraIdle!();
      },
    );
  }
}
