import 'package:flutter/material.dart';

import 'elapsed_badge.dart';

class TrackRecordingActionVisual extends StatefulWidget {
  final bool isRecording;
  final bool isPaused;
  final String elapsedText;
  final VoidCallback? onTap;
  final VoidCallback? onLongPress;

  const TrackRecordingActionVisual({
    required this.isRecording,
    required this.isPaused,
    required this.elapsedText,
    this.onTap,
    this.onLongPress,
  });

  @override
  State<TrackRecordingActionVisual> createState() =>
      TrackRecordingActionVisualState();
}

class TrackRecordingActionVisualState extends State<TrackRecordingActionVisual>
    with SingleTickerProviderStateMixin {
  late final AnimationController _animationController;
  late final Animation<double> _scale;
  late final Animation<double> _opacity;

  bool get _shouldPulse {
    return widget.isRecording && !widget.isPaused;
  }

  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 3000),
    );

    _scale = Tween<double>(
      begin: 0.80,
      end: 1.20,
    ).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeInOut,
      ),
    );

    _opacity = Tween<double>(
      begin: 0.72,
      end: 1.0,
    ).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeInOut,
      ),
    );

    _updateAnimation();
  }

  @override
  void didUpdateWidget(covariant TrackRecordingActionVisual oldWidget) {
    super.didUpdateWidget(oldWidget);
    _updateAnimation();
  }

  void _updateAnimation() {
    if (_shouldPulse && !_animationController.isAnimating) {
      _animationController.repeat(reverse: true);
      return;
    }

    if (!_shouldPulse && _animationController.isAnimating) {
      _animationController.stop();
      _animationController.reset();
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

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

    final Color iconColor;
    final Color badgeColor;

    if (!widget.isRecording) {
      iconColor = colorScheme.onSurfaceVariant;
      badgeColor = colorScheme.onSurfaceVariant;
    } else if (widget.isPaused) {
      iconColor = Colors.orange.shade800;
      badgeColor = Colors.orange.shade800;
    } else {
      iconColor = Colors.red.shade700;
      badgeColor = Colors.red.shade700;
    }

    return Tooltip(
      message: _tooltipText,
      child: InkResponse(
        radius: 22,
        onTap: widget.onTap,
        onLongPress: widget.onLongPress,
        child: SizedBox(
          width: widget.isRecording ? 58 : 40,
          height: 44,
          child: Center(
            child: Stack(
              clipBehavior: Clip.none,
              alignment: Alignment.center,
              children: [
                _buildIcon(iconColor),
                if (widget.isRecording)
                  Positioned(
                    right: -20,
                    top: -4,
                    child: ElapsedBadge(
                      text: widget.elapsedText,
                      color: badgeColor,
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildIcon(Color iconColor) {
    final icon = Icon(
      Icons.route_outlined,
      size: 25,
      color: iconColor,
    );

    if (!_shouldPulse) {
      return icon;
    }

    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return Opacity(
          opacity: _opacity.value,
          child: Transform.scale(
            scale: _scale.value,
            child: child,
          ),
        );
      },
      child: icon,
    );
  }

  String get _tooltipText {
    if (!widget.isRecording) {
      return 'Útvonal rögzítés';
    }

    if (widget.isPaused) {
      return 'Útvonal rögzítés szünetel\n${widget.elapsedText}';
    }

    return 'Útvonal rögzítés folyamatban\n${widget.elapsedText}';
  }
}
