import 'package:flutter/material.dart';

import '../../core/constants/design_system.dart';
import '../../domain/entities/social_post.dart';
import 'animated_widgets.dart';

/// Widget de barre d'interactions pour les posts sociaux.
///
/// Affiche les boutons Like, Comment, Share et Bookmark avec animations.
class SocialInteractionRow extends StatefulWidget {
  const SocialInteractionRow({
    required this.post,
    required this.onLike,
    required this.onComment,
    required this.onShare,
    super.key,
  });

  final SocialPost post;
  final VoidCallback onLike;
  final VoidCallback onComment;
  final VoidCallback onShare;

  @override
  State<SocialInteractionRow> createState() => _SocialInteractionRowState();
}

class _SocialInteractionRowState extends State<SocialInteractionRow>
    with SingleTickerProviderStateMixin {
  late AnimationController _likeController;
  bool _isBookmarked = false;

  @override
  void initState() {
    super.initState();
    _likeController = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );

    if (widget.post.isLikedByCurrentUser) {
      _likeController.value = 1.0;
    }
  }

  @override
  void didUpdateWidget(SocialInteractionRow oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.post.isLikedByCurrentUser != oldWidget.post.isLikedByCurrentUser) {
      if (widget.post.isLikedByCurrentUser) {
        _likeController.forward();
      } else {
        _likeController.reverse();
      }
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Row(
      children: [
        _buildLikeButton(theme),
        const SizedBox(width: DesignSystem.spacingLg),
        _buildCommentButton(theme),
        const SizedBox(width: DesignSystem.spacingLg),
        _buildShareButton(theme),
        const Spacer(),
        _buildBookmarkButton(theme),
      ],
    );
  }

  /// Construit le bouton Like avec animation
  Widget _buildLikeButton(ThemeData theme) {
    final isLiked = widget.post.isLikedByCurrentUser;

    return AnimatedScaleButton(
      onTap: () {
        if (isLiked) {
          _likeController.reverse();
        } else {
          _likeController.forward();
        }
        widget.onLike();
      },
      scaleFactor: 0.8,
      child: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: DesignSystem.spacingSm,
          vertical: DesignSystem.spacingSm,
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            AnimatedBuilder(
              animation: _likeController,
              builder: (context, child) {
                return Stack(
                  alignment: Alignment.center,
                  children: [
                    // Cercle d'effet de like
                    if (_likeController.value > 0)
                      Container(
                        width: 26 + (8 * _likeController.value),
                        height: 26 + (8 * _likeController.value),
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: Colors.red.withOpacity(
                            0.2 * (1 - _likeController.value),
                          ),
                        ),
                      ),

                    // Icône de coeur avec animation
                    Transform.scale(
                      scale: 1 + (_likeController.value * 0.2),
                      child: Icon(
                        isLiked ? Icons.favorite_rounded : Icons.favorite_border_rounded,
                        size: 26,
                        color: isLiked
                            ? Colors.red
                            : theme.colorScheme.onSurface.withOpacity(0.7),
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  /// Construit le bouton Commentaire
  Widget _buildCommentButton(ThemeData theme) {
    return AnimatedScaleButton(
      onTap: widget.onComment,
      scaleFactor: 0.85,
      child: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: DesignSystem.spacingSm,
          vertical: DesignSystem.spacingSm,
        ),
        child: Icon(
          Icons.chat_bubble_outline_rounded,
          size: 26,
          color: theme.colorScheme.onSurface.withOpacity(0.7),
        ),
      ),
    );
  }

  /// Construit le bouton Partage
  Widget _buildShareButton(ThemeData theme) {
    return AnimatedScaleButton(
      onTap: widget.onShare,
      scaleFactor: 0.85,
      child: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: DesignSystem.spacingSm,
          vertical: DesignSystem.spacingSm,
        ),
        child: Icon(
          Icons.send_outlined,
          size: 26,
          color: theme.colorScheme.onSurface.withOpacity(0.7),
        ),
      ),
    );
  }

  /// Construit le bouton Bookmark
  Widget _buildBookmarkButton(ThemeData theme) {
    return AnimatedScaleButton(
      onTap: () {
        setState(() {
          _isBookmarked = !_isBookmarked;
        });
      },
      scaleFactor: 0.85,
      child: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: DesignSystem.spacingSm,
          vertical: DesignSystem.spacingSm,
        ),
        child: AnimatedSwitcher(
          duration: DesignSystem.durationFast,
          transitionBuilder: (child, animation) {
            return ScaleTransition(
              scale: animation,
              child: child,
            );
          },
          child: Icon(
            _isBookmarked
                ? Icons.bookmark_rounded
                : Icons.bookmark_border_rounded,
            key: ValueKey<bool>(_isBookmarked),
            size: 26,
            color: _isBookmarked
                ? theme.colorScheme.primary
                : theme.colorScheme.onSurface.withOpacity(0.7),
          ),
        ),
      ),
    );
  }
}
