import 'dart:ui';

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

import 'track_recording_action_visual.dart';

class TrackRecordingAction extends StatelessWidget {
  final TrackingController controller;
  final VoidCallback? onTap;
  final VoidCallback? onLongPress;

  const TrackRecordingAction({
    super.key,
    required this.controller,
    this.onTap,
    this.onLongPress,
  });

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return TrackRecordingActionVisual(
        isRecording: controller.isRecording.value,
        isPaused: controller.isPaused.value,
        elapsedText: _shortElapsedText(controller.elapsedFormatted.value),
        onTap: onTap,
        onLongPress: onLongPress,
      );
    });
  }

  String _shortElapsedText(String value) {
    // A controller most HH:MM:SS formátumot ad, pl. 00:38:37.
    final parts = value.split(':');

    if (parts.length != 3) {
      return value;
    }

    final h = int.tryParse(parts[0]) ?? 0;
    final m = parts[1];
    final s = parts[2];

    if (h <= 0) {
      return '$m:$s'; // 38:37
    }

    return '$h:$m'; // 1:04
  }
}
