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';

class SaveSheetActions extends StatelessWidget {
  final MapSurveyController ctrl;
  const SaveSheetActions({required this.ctrl});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      final isEditing = ctrl.isGeometryEditing;
      final tool = ctrl.activeEditTool.value;
      final isPoint = tool == MapEditTool.point;

      return Column(mainAxisSize: MainAxisSize.min, children: [
        Row(children: [
          Expanded(
            child: OutlinedButton.icon(
                style: OutlinedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10))),
                icon: Icon(
                  isEditing ? Icons.close : Icons.arrow_back,
                  size: 18,
                ),
                label: Text(isEditing ? 'Mégse' : 'Vissza'),
                onPressed: () {
                  Get.back();
                  ctrl.cancelEditing();
                }),
          ),
          const SizedBox(width: 12),
          Expanded(
            flex: 2,
            child: FilledButton.icon(
              style: FilledButton.styleFrom(
                  backgroundColor: ctrl.activeEditColor.value,
                  padding: const EdgeInsets.symmetric(vertical: 14),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10))),
              icon: const Icon(Icons.check, size: 18),
              label: const Text(
                'Mentés',
                style: TextStyle(fontWeight: FontWeight.w600, fontSize: 15),
              ),
              onPressed: () async {
                Get.back();
                await ctrl.finishDraft();
              },
            ),
          )
        ]),
        if (isEditing) ...[
          const SizedBox(height: 8),
          SizedBox(
            width: double.infinity,
            child: OutlinedButton.icon(
              style: OutlinedButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 12),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
              ),
              icon: const Icon(Icons.edit_outlined, size: 18),
              label: const Text('Geometria szerkesztése'),
              onPressed: () {
                Get.back();
                ctrl.startGeometryEdit();
              },
            ),
          ),
        ],
        if (isEditing) ...[
          const SizedBox(height: 8),
          SizedBox(
            width: double.infinity,
            child: OutlinedButton.icon(
              style: OutlinedButton.styleFrom(
                  foregroundColor: Colors.red,
                  side: const BorderSide(color: Colors.red),
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10))),
              icon: const Icon(Icons.delete_outline, size: 18),
              label: const Text('Törlés'),
              onPressed: () => _confirmDelete(context, ctrl),
            ),
          )
        ]
      ]);
    });
  }
  // return Row(children: [
  //   // ← Vissza — bezárja a sheet-et, folytatja a rajzolást
  //   Expanded(
  //     child: OutlinedButton.icon(
  //       style: OutlinedButton.styleFrom(
  //         padding: const EdgeInsets.symmetric(vertical: 14),
  //         shape: RoundedRectangleBorder(
  //             borderRadius: BorderRadius.circular(10)),
  //       ),
  //       icon: const Icon(Icons.arrow_back, size: 18),
  //       label: const Text('Vissza'),
  //       onPressed: () => Navigator.pop(context),
  //     ),
  //   ),
  //   const SizedBox(width: 12),

  //   // Mentés — végleges mentés, mindkét sheet bezárása
  //   Expanded(
  //     flex: 2,
  //     child: Obx(() => FilledButton.icon(
  //           style: FilledButton.styleFrom(
  //             backgroundColor: ctrl.activeEditColor.value,
  //             padding: const EdgeInsets.symmetric(vertical: 14),
  //             shape: RoundedRectangleBorder(
  //                 borderRadius: BorderRadius.circular(10)),
  //           ),
  //           icon: const Icon(Icons.check, size: 18),
  //           label: const Text(
  //             'Mentés',
  //             style: TextStyle(fontWeight: FontWeight.w600, fontSize: 15),
  //           ),
  //           onPressed: () async {
  //             //Navigator.pop(context); // style sheet bezárás
  //             Get.back();
  //             await ctrl.finishDraft();
  //           },
  //         )),
  //   ),
  // ]);
  void _confirmDelete(BuildContext context, MapSurveyController ctrl) {
    Get.dialog(AlertDialog(
      title: const Text('Elem törlése'),
      content: const Text('Ez az elem véglegesen törlődik.'),
      actions: [
        TextButton(onPressed: Get.back, child: const Text('Mégse')),
        FilledButton(
          style: FilledButton.styleFrom(backgroundColor: Colors.red),
          onPressed: () async {
            Get.back(); // dialóg
            Get.back(); // sheet
            await ctrl.deleteEditingItem();
          },
          child: const Text('Törlés'),
        ),
      ],
    ));
  }
}
