import 'dart:async';

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

import '../../consts/consts.dart';
import '../../packages/ambito_theme/ambito_theme.dart';

class MapWidget extends StatefulWidget {
  const MapWidget(
      {super.key,
      required this.markers,
      required this.polygons,
      required this.polylines});

  final Set<Marker> markers;
  final Set<Polygon> polygons;
  final Set<Polyline> polylines;

  @override
  State<StatefulWidget> createState() => MapWidgetState();
}

class MapWidgetState extends State<MapWidget> {
  final Completer<GoogleMapController> _controller =
      Completer<GoogleMapController>();

  bool _drawPolygon = false;
  int _counter = 0;

  static CameraPosition _initialPos = const CameraPosition(
    target: LatLng(0, 0),
    zoom: 14.4746,
  );

  @override
  void initState() {
    setState(() {
      _initialPos =
          CameraPosition(target: widget.markers.first.position, zoom: 14);
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final AmbitoTheme theme = getTheme(context);
    return ClipRRect(
      borderRadius: BorderRadius.circular(10.0),
      child: SizedBox(
          width: double.infinity,
          child: Column(
            children: [
              Expanded(
                child: GoogleMap(
                  onTap: (position) {
                    if (_drawPolygon == true) {
                      setState(() {
                        widget.markers.add(
                          Marker(
                            onTap: onTapMarker('polygon_$_counter'),
                            markerId: MarkerId('polygon_$_counter'),
                            position: position,
                          ),
                        );
                        if (_counter > 0) {
                          logger.i('add polyline');
                          widget.polylines.add(
                            Polyline(
                                polylineId: PolylineId('polyline_$_counter'),
                                color: theme.currentColorScheme.primary,
                                width: 2,
                                points: [
                                  widget.markers
                                      .where((el) =>
                                          el.markerId ==
                                          MarkerId('polygon_${_counter - 1}'))
                                      .first
                                      .position,
                                  position
                                ]),
                          );
                        }
                        _counter++;
                      });
                    }
                  },
                  mapToolbarEnabled: true,
                  mapType: MapType.hybrid,
                  markers: widget.markers,
                  polygons: widget.polygons,
                  polylines: widget.polylines,
                  initialCameraPosition: _initialPos,
                  onMapCreated: (GoogleMapController controller) {
                    _controller.complete(controller);
                  },
                ),
              ),
              Row(
                children: [
                  const Spacer(),
                  IconButton(
                      onPressed: () {
                        setState(() {
                          _drawPolygon = !_drawPolygon;
                        });
                      },
                      icon: Icon(
                        Icons.polyline_outlined,
                        color: (_drawPolygon
                            ? theme.currentColorScheme.primary
                            : theme.currentColorScheme.outline),
                      ))
                ],
              ),
            ],
          )),
    );
  }

  onTapMarker(String counter) {
    logger.d(counter);
    if (counter == 'polygon_0') {
      logger.d('first pressed');
    }
  }
}
