import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/pages/tracking/presentation/controllers/tracking_controller.dart';

import 'stat_cell.dart';

class LiveStatsPanel extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Obx(() {
      final ctrl = TrackingController.to;
      final isRec = ctrl.isRecording.value;

      return Padding(
        padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
        child: isRec
            ? Column(children: [
                // Statisztika sor
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    StatCell(
                        icon: Icons.timer_outlined,
                        label: 'Idő',
                        value: ctrl.elapsedFormatted.value,
                        large: true),
                    StatCell(
                        icon: Icons.route,
                        label: 'Távolság',
                        value: _fmtDist(ctrl.sessionDistance.value)),
                    StatCell(
                        icon: Icons.location_on_outlined,
                        label: 'Pontok',
                        value: '${ctrl.livePointCount}'),
                  ],
                ),
                const SizedBox(height: 10),
                // Vezérlők
                Row(children: [
                  Expanded(
                    child: OutlinedButton.icon(
                      icon: Icon(
                          ctrl.isPaused.value ? Icons.play_arrow : Icons.pause),
                      label: Text(ctrl.isPaused.value ? 'Folytatás' : 'Szünet'),
                      onPressed: ctrl.isPaused.value
                          ? ctrl.resumeRecording
                          : ctrl.pauseRecording,
                    ),
                  ),
                  const SizedBox(width: 10),
                  Expanded(
                    child: FilledButton.icon(
                      icon: const Icon(Icons.stop),
                      label: const Text('Befejezés'),
                      style: FilledButton.styleFrom(
                          backgroundColor: Colors.red.shade700),
                      onPressed: ctrl.stopRecording,
                    ),
                  ),
                ]),
              ])
            : FilledButton.icon(
                icon: const Icon(Icons.fiber_manual_record),
                label: const Text('Rögzítés indítása'),
                style: FilledButton.styleFrom(
                    backgroundColor: Colors.red.shade700),
                onPressed: () => _showStartDialog(context),
              ),
      );
    });
  }

  void _showStartDialog(BuildContext context) {
    final nameCtrl = TextEditingController(
      text: 'Track ${DateTime.now().toString().substring(5, 16)}',
    );
    Get.dialog(AlertDialog(
      title: const Text('Rögzítés indítása'),
      content: TextField(
        controller: nameCtrl,
        autofocus: true,
        decoration: const InputDecoration(
          labelText: 'Track neve',
          border: OutlineInputBorder(),
        ),
      ),
      actions: [
        TextButton(onPressed: Get.back, child: const Text('Mégse')),
        FilledButton(
          onPressed: () {
            Get.back();
            TrackingController.to.startRecording();
          },
          child: const Text('Indítás'),
        ),
      ],
    ));
  }

  String _fmtDist(double m) => m < 1000
      ? '${m.toStringAsFixed(0)} m'
      : '${(m / 1000).toStringAsFixed(2)} km';
}
