import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:terepi_seged/enums/map_survey_mode.dart';
import 'package:terepi_seged/pages/map_survey/presentations/controllers/map_survey_controller.dart';
import 'package:terepi_seged/services/project_service.dart';

class MapModeMenuAnchor extends StatelessWidget {
  final MapSurveyController controller;

  const MapModeMenuAnchor({super.key, required this.controller});

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    return Obx(() {
      final p = ProjectService.to.activeProject.value;
      return MenuAnchor(
          menuChildren: [
            MenuItemButton(
                leadingIcon: const Icon(Icons.map),
                trailingIcon: controller.mode.value == MapSurveyMode.browse
                    ? const Icon(Icons.check)
                    : null,
                onPressed: () => controller.setMode(MapSurveyMode.browse),
                child: const Text('Térkép')),
            MenuItemButton(
                leadingIcon: const Icon(Icons.add_location_alt),
                trailingIcon: controller.mode.value == MapSurveyMode.measure
                    ? const Icon(Icons.check)
                    : null,
                onPressed: () => controller.setMode(MapSurveyMode.measure),
                child: const Text('Bemérés')),
            MenuItemButton(
                leadingIcon: const Icon(Icons.gps_fixed),
                trailingIcon: controller.mode.value == MapSurveyMode.stakeout
                    ? const Icon(Icons.check)
                    : null,
                onPressed: () => controller.setMode(MapSurveyMode.stakeout),
                child: const Text('Kitűzés')),
            MenuItemButton(
                leadingIcon: const Icon(Icons.hiking),
                trailingIcon: controller.mode.value == MapSurveyMode.fieldWalk
                    ? const Icon(Icons.check)
                    : null,
                onPressed: () => controller.setMode(MapSurveyMode.fieldWalk),
                child: const Text('Bejárás')),
            MenuItemButton(
                leadingIcon: const Icon(Icons.route),
                trailingIcon: controller.mode.value == MapSurveyMode.track
                    ? const Icon(Icons.check)
                    : null,
                onPressed: () => controller.setMode(MapSurveyMode.track),
                child: const Text('Útvonal')),
          ],
          builder: (context, menuController, child) {
            return InkWell(
                borderRadius: BorderRadius.circular(18),
                onTap: () {
                  if (menuController.isOpen) {
                    menuController.close();
                  } else {
                    menuController.open();
                  }
                },
                child: Padding(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 6,
                      vertical: 4,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(mainAxisSize: MainAxisSize.min, children: [
                          Icon(controller.currentModeIcon, size: 18),
                          const SizedBox(width: 6),
                          Text(controller.currentModeLabel,
                              style: const TextStyle(
                                  fontSize: 18, fontWeight: FontWeight.w600)),
                          const Icon(Icons.arrow_drop_down, size: 22),
                        ]),
                        Text(
                            p == null
                                ? 'Projekt'
                                : (p.name.length > 18
                                    ? '${p.name.substring(0, 18)}...'
                                    : p.name),
                            style: TextStyle(
                                fontSize: 12,
                                fontStyle: FontStyle.italic,
                                fontWeight: FontWeight.w400,
                                color: colorScheme.onSurfaceVariant))
                      ],
                    )));
          });
    });
  }
}
