// ignore_for_file: non_constant_identifier_names, camel_case_types, missing_return, unused_import, unused_local_variable, dead_code, unnecessary_cast
//////////////////////////////////////////////////////////
// GENERATED BY FLUTTIFY. DO NOT EDIT IT.
//////////////////////////////////////////////////////////

import 'dart:convert';
import 'dart:typed_data';

import 'package:amap_map_fluttify/src/android/android.export.g.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'package:foundation_fluttify/foundation_fluttify.dart';
import 'package:core_location_fluttify/core_location_fluttify.dart';
import 'package:amap_core_fluttify/amap_core_fluttify.dart';
import 'package:amap_search_fluttify/amap_search_fluttify.dart';
import 'package:amap_location_fluttify/amap_location_fluttify.dart';

typedef WearMapViewCreatedCallback = void Function(com_amap_api_maps_WearMapView controller);
typedef _OnAndroidViewDispose = Future<void> Function();

class com_amap_api_maps_WearMapView_AndroidView extends StatefulWidget {
  const com_amap_api_maps_WearMapView_AndroidView({
    Key? key,
    this.onViewCreated,
    this.onDispose,
    this.params = const <String, dynamic>{},
    this.gestureRecognizers,
    this.hybridComposition = false,
  }) : super(key: key);

  final WearMapViewCreatedCallback? onViewCreated;
  final _OnAndroidViewDispose? onDispose;
  final Map<String, dynamic> params;
  final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers;
  final bool hybridComposition;

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

class _com_amap_api_maps_WearMapView_AndroidViewState extends State<com_amap_api_maps_WearMapView_AndroidView> {
  com_amap_api_maps_WearMapView? _controller;

  @override
  Widget build(BuildContext context) {
    final gestureRecognizers = widget.gestureRecognizers ?? <Factory<OneSequenceGestureRecognizer>>{
      Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()),
    };

    // hybrid composition version.
    // if (widget.hybridComposition) {
    //   return PlatformViewLink(
    //     viewType: 'me.yohom/com.amap.api.maps.WearMapView',
    //     surfaceFactory: (
    //       BuildContext context,
    //       PlatformViewController controller,
    //     ) {
    //       return AndroidViewSurface(
    //         controller: controller,
    //         gestureRecognizers: gestureRecognizers,
    //         hitTestBehavior: PlatformViewHitTestBehavior.opaque,
    //       );
    //     },
    //     onCreatePlatformView: (PlatformViewCreationParams params) {
    //       return PlatformViewsService.initSurfaceAndroidView(
    //         id: params.id,
    //         viewType: 'me.yohom/com.amap.api.maps.WearMapView',
    //         layoutDirection: TextDirection.ltr,
    //         creationParams: widget.params,
    //         creationParamsCodec: kAmapMapFluttifyMessageCodec,
    //       )
    //         ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
    //         ..addOnPlatformViewCreatedListener((id) => _onViewCreated(id))
    //         ..create();
    //     },
    //   );
    // } else {
    return AndroidView(
      viewType: 'me.yohom/com.amap.api.maps.WearMapView',
      gestureRecognizers: gestureRecognizers,
      onPlatformViewCreated: _onViewCreated,
      creationParamsCodec: kAmapMapFluttifyMessageCodec,
      creationParams: widget.params,
    );
    // }
  }

  void _onViewCreated(int id) async {
    // 碰到一个对象返回的hashCode为0的情况, 造成和这个id冲突了, 这里用一个magic number避免一下
    // 把viewId转换为refId再使用, 使其与其他对象统一
    final refId = await viewId2RefId((2147483647 - id).toString());
    _controller = com_amap_api_maps_WearMapView()..refId = 'com.amap.api.maps.WearMapView:$refId';
    if (widget.onViewCreated != null) {
      widget.onViewCreated!(_controller!);
    }
  }

  @override
  void dispose() {
    if (widget.onDispose != null) {
      widget.onDispose!().whenComplete(() => _controller?.release__());
    } else {
      _controller?.release__();
    }
    super.dispose();
  }
}
