// Szöveges feliratok a rögzített pontokhoz és vonalakhoz.
// Poligonoknál a Polygon.label-t a view kezeli (flutter_map beépített).

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:terepi_seged/enums/map_survey_mode.dart';

import '../../enums/note_type.dart';
import '../../pages/map_survey/presentations/controllers/map_survey_controller.dart';

class NoteItemLabelLayer extends StatelessWidget {
  final MapSurveyController controller;
  const NoteItemLabelLayer({super.key, required this.controller});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (!controller.showGeometryLabels.value) return const SizedBox.shrink();
      if (controller.mode.value != MapSurveyMode.fieldWalk)
        return const SizedBox.shrink();

      final markers = <Marker>[];

      for (final item in controller.allNoteItems) {
        if (item.label.isEmpty) continue;

        switch (item.type) {
          // ── Pont — felirat a marker felett ──────────────────────
          case NoteType.point:
            markers.add(Marker(
              point: item.points.first,
              width: 110,
              height: 48, // 22 szöveg + 22 pont marker helye
              alignment: Alignment.bottomCenter,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  _LabelBubble(label: item.label, color: item.color),
                  //const SizedBox(height: 4), // pont marker fölé kerül
                ],
              ),
            ));

          // ── Vonal — felirat a felezőponton ──────────────────────
          case NoteType.line:
            if (item.points.isEmpty) continue;
            final mid = _midpoint(item.points);
            markers.add(Marker(
              point: mid,
              width: 120,
              height: 24,
              alignment: Alignment.center,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  _LabelBubble(label: item.label, color: item.color),
                ],
              ),
            ));

          case NoteType.polygon:
            break; // poligonnál a Polygon.label kezeli a view-ban
        }
      }

      if (markers.isEmpty) return const SizedBox.shrink();
      return MarkerLayer(markers: markers);
    });
  }

  /// Vonal felezőpontja
  LatLng _midpoint(List<LatLng> points) {
    final mid = points[points.length ~/ 2];
    return mid;
  }
}

// ─── Felirat buborék ─────────────────────────────────────────────────────────

class _LabelBubble extends StatelessWidget {
  final String label;
  final Color color;
  const _LabelBubble({required this.label, required this.color});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.92),
        borderRadius: BorderRadius.circular(5),
        border: Border(left: BorderSide(color: color, width: 3)),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.18),
            blurRadius: 4,
            offset: const Offset(0, 1),
          ),
        ],
      ),
      child: Text(
        label,
        style: const TextStyle(
          fontSize: 11,
          fontWeight: FontWeight.w600,
          color: Colors.black87,
          height: 1.2,
        ),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }
}
