import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/enums/map_edit_tool.dart';
import 'package:terepi_seged/pages/map_survey/presentations/controllers/map_survey_controller.dart';

import 'map_edit_small_status_chip.dart';
import 'map_edit_square_toolbar_button.dart';

class LineOrPolygonDrawingContent extends StatelessWidget {
  final MapSurveyController controller;

  const LineOrPolygonDrawingContent({
    required this.controller,
  });

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

    return Obx(() {
      final pointCount = controller.editorPointCount.value;
      final canFinish = controller.canFinishGeometry;
      final measureText = controller.draftMeasureText;

      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              Icon(
                controller.activeEditToolIcon,
                size: 20,
                color: colorScheme.primary,
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  '${controller.activeEditToolTitle} · $pointCount pont',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: Theme.of(context).textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.w800,
                      ),
                ),
              ),
              if (measureText.isNotEmpty) ...[
                const SizedBox(
                  width: 6,
                ),
                SmallStatusChip(
                  text: measureText,
                  color: colorScheme.primary,
                ),
                const SizedBox(
                  width: 6,
                ),
              ],
              SmallStatusChip(
                text: _requiredPointText(controller.activeEditTool.value),
                color: canFinish
                    ? colorScheme.primary
                    : colorScheme.onSurfaceVariant,
              ),
            ],
          ),
          const SizedBox(height: 5),
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              controller.activeEditToolHint,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: Theme.of(context).textTheme.bodySmall?.copyWith(
                    color: colorScheme.onSurfaceVariant,
                    height: 1.15,
                  ),
            ),
          ),
          const SizedBox(height: 8),
          Row(
            children: [
              // SquareToolbarButton(
              //   tooltip: 'Utolsó pont törlése',
              //   icon: Icons.undo,
              //   // onPressed: controller.canUndo ? controller.undoLastPoint : null,
              //   onPressed: () {},
              // ),
              // const SizedBox(width: 8),
              Expanded(
                child: OutlinedButton.icon(
                  onPressed: controller.cancelEditing,
                  icon: const Icon(Icons.close, size: 18),
                  label: const Text('Mégse'),
                ),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: FilledButton.icon(
                  onPressed: canFinish ? controller.finishGeometry : null,
                  icon: const Icon(Icons.check, size: 18),
                  label: Text(controller.finishButtonText),
                ),
              ),
            ],
          ),
        ],
      );
    });
  }

  String _requiredPointText(MapEditTool tool) {
    switch (tool) {
      case MapEditTool.line:
        return 'min. 2';
      case MapEditTool.polygon:
        return 'min. 3';
      case MapEditTool.point:
        return '1 pont';
      case MapEditTool.none:
        return '';
    }
  }
}
