import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/src/consts/map_constants.dart';
import 'package:flutter_map/src/consts/marker_type.dart';
import 'package:flutter_map/src/core/map_controller.dart';
import 'package:flutter_map/src/entity/marker.dart';

class MapWidget extends StatefulWidget {
  final MapController? controller;
  final MarkerTapCallback? onMarkerTap;

  const MapWidget({
    super.key,
    this.controller,
    this.onMarkerTap,
  });

  @override
  State<MapWidget> createState() => _MapWidgetState();
}

class _MapWidgetState extends State<MapWidget> {
  final String mapViewType = MapConstants.mapWidgetViewType;

  @override
  Widget build(BuildContext context) {
    debugPrint(
        'MapWidget...build...mapViewType==>$mapViewType,defaultTargetPlatform==>$defaultTargetPlatform');
    if (defaultTargetPlatform == TargetPlatform.android) {
      return PlatformViewLink(
          surfaceFactory: (context, controller) {
            return AndroidViewSurface(
              controller: controller as AndroidViewController,
              gestureRecognizers: const <Factory<
                  OneSequenceGestureRecognizer>>{},
              hitTestBehavior: PlatformViewHitTestBehavior.opaque,
            );
          },
          onCreatePlatformView: onCreatePlatformView,
          viewType: mapViewType);
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: mapViewType,
        onPlatformViewCreated: onPlatformViewCreated,
        creationParamsCodec: const StandardMessageCodec(),
      );
    }
    return Text('当前平台:$defaultTargetPlatform, 不支持使用地图插件');
  }

  PlatformViewController onCreatePlatformView(
      PlatformViewCreationParams params) {
    return PlatformViewsService.initSurfaceAndroidView(
      id: params.id,
      viewType: mapViewType,
      layoutDirection: TextDirection.ltr,
      creationParams: <String, dynamic>{},
      creationParamsCodec: const StandardMessageCodec(),
    )
      ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
      ..addOnPlatformViewCreatedListener(onPlatformViewCreated)
      ..create();
  }

  Future<void> onPlatformViewCreated(int viewId) async {
    MethodChannel mapChannel =
        MethodChannel('${MapConstants.mapViewChannelName}$viewId');
    widget.controller?.setChannel(mapChannel);
    mapChannel.setMethodCallHandler(_mapMethodCallHandler);
  }

  Future<dynamic> _mapMethodCallHandler(MethodCall call) async {
    debugPrint(
        '_mapMethodCallHandler...call.method==>${call.method},call.arguments==>${call.arguments}');
    switch (call.method) {
      case MapConstants.methodMarkerOnTap:
        Map<String, dynamic> jsonMap = jsonDecode(call.arguments);
        Marker marker = Marker.fromJson(jsonMap);
        widget.onMarkerTap?.call(marker);
        break;
    }
  }
}
