import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_polygon_editor/polygon_editor/polygon_editor_controller.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:geolocator/geolocator.dart';

class FieldTripController extends GetxController {
  RxBool mapIsInitialized = false.obs;
  RxBool mapInEditorMode = false.obs;
  RxBool mapInPointEditorMode = false.obs;

  final pointNotes = <Marker>[].obs;
  final polylineNotes = <Polyline<Object>>[].obs;
  final polygonNotes = <Polygon<Object>>[].obs;

  late final MapController mapController;
  late final MapOptions mapOptions;
  late final PolygonEditorController polygonEditorController;

  final PolygonLabelPlacementCalculator _labelPlacementCalculator =
      const PolygonLabelPlacementCalculator.centroid();

  @override
  void onInit() async {
    // TODO: implement onInit
    super.onInit();

    LatLng initialCenter = const LatLng(47.5, 19.0);
    try {
      final last = await Geolocator.getLastKnownPosition();
      if (last != null) {
        initialCenter = LatLng(last.latitude, last.longitude);
      }
    } catch (_) {}

    mapOptions = MapOptions(
      initialCenter: initialCenter,
      initialZoom: 13,
      onLongPress: (tapPosition, point) {
        if (mapInEditorMode.value) {
          polygonEditorController.addPoint(point);
        }
        if (mapInPointEditorMode.value) {
          savePointNote(point: point);
        }
      },
      keepAlive: true,
    );

    polylineNotes.add(
      Polyline(
        points: [
          const LatLng(47.65, 19.00),
          const LatLng(47.64, 19.02),
          const LatLng(47.69, 19.22),
        ],
        strokeWidth: 8,
        color: const Color.fromARGB(255, 227, 238, 71),
        hitValue: (
          title: 'Purple Line',
          subtitle: 'Nothing really special here...',
        ),
      ),
    );

    polygonEditorController =
        PolygonEditorController(mode: PolygonEditorMode.polygon);

    mapController = MapController();

    mapIsInitialized.value = true;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    polygonEditorController.dispose();
    super.dispose();
  }

  void cancelEditorController() {
    polygonEditorController.clear();
    mapInEditorMode.value = false;
  }

  void startPolygonEdition() {
    polygonEditorController.clear();
    polygonEditorController.setMode(PolygonEditorMode.polygon);
    mapInEditorMode.value = true;
  }

  void startPolylineEdition() {
    polygonEditorController.clear();
    polygonEditorController.setMode(PolygonEditorMode.line);
    mapInEditorMode.value = true;
  }

  void startPointEdition() {
    mapInPointEditorMode.value = true;
  }

  void saveNote() {
    if (polygonEditorController.mode == PolygonEditorMode.line) {
      print("Points number in line: ${polygonEditorController.points.length}");
      print(
          "1. point coords: ${polygonEditorController.points[0].latitude} - ${polygonEditorController.points[0].longitude}");
      if (polygonEditorController.points.length < 2) return;

      Polyline polyline = Polyline(
        points: List.from(polygonEditorController.points),
        color: Colors.red,
        strokeWidth: 8,
        // hitValue: (
        //   title: 'Purple Line',
        //   subtitle: 'Nothing really special here...',
        // ),
      );
      polylineNotes.add(polyline);
      // polylineNotes.refresh();

      print("Points number in polylineNotes: ${polylineNotes.length}");
      print(
          "1. point coords of polyline: ${polyline.points[0].latitude} - ${polyline.points[0].longitude}");

      polygonEditorController.clear();
      mapInEditorMode.value = false;
    }
    if (polygonEditorController.mode == PolygonEditorMode.polygon) {
      print(
          "Points number in polygon: ${polygonEditorController.points.length}");

      Polygon polygon = Polygon(
        points: List.from(polygonEditorController.points),
        color: Colors.purple,
        borderColor: Colors.yellow,
        borderStrokeWidth: 4,
        label: 'Label!',
        labelPlacementCalculator: _labelPlacementCalculator,
        // hitValue: (
        //   title: 'Basic Filled Polygon',
        //   subtitle: 'Nothing really special here...',
      );

      polygonNotes.add(polygon);
      //polygonNotes.refresh();
      //update();

      print("Points number in polygonNotes: ${polygonNotes.length}");

      polygonEditorController.clear();
      mapInEditorMode.value = false;
    }
  }

  void savePointNote({required LatLng point}) {
    Marker marker = Marker(
        point: point,
        width: 15.0,
        height: 15.0,
        child: Container(
          width: 15.0,
          height: 15.0,
          decoration: BoxDecoration(
              color: Colors.amber[700],
              shape: BoxShape.circle,
              border: Border.all(width: 1.0, color: Colors.black)),
        ));
    pointNotes.add(marker);
    pointNotes.refresh();
    update();
    mapInPointEditorMode.value = false;
  }
}
