import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:go_router/go_router.dart';

extension AnimateWidgetExtensions on Widget {
  Widget fadeSlideUp({int delay = 0, int duration = 500}) {
    return animate(delay: Duration(milliseconds: delay))
        .fadeIn(duration: Duration(milliseconds: duration), curve: Curves.easeOutCubic)
        .slideY(begin: 0.08, end: 0, duration: Duration(milliseconds: duration), curve: Curves.easeOutCubic);
  }

  Widget fadeSlideRight({int delay = 0, int duration = 400}) {
    return animate(delay: Duration(milliseconds: delay))
        .fadeIn(duration: Duration(milliseconds: duration), curve: Curves.easeOutCubic)
        .slideX(begin: -0.05, end: 0, duration: Duration(milliseconds: duration), curve: Curves.easeOutCubic);
  }

  Widget scaleIn({int delay = 0, int duration = 400}) {
    return animate(delay: Duration(milliseconds: delay))
        .fadeIn(duration: Duration(milliseconds: duration))
        .scale(begin: const Offset(0.9, 0.9), end: const Offset(1, 1), duration: Duration(milliseconds: duration), curve: Curves.easeOutBack);
  }

  Widget shimmerEffect() {
    return animate(onPlay: (c) => c.repeat())
        .shimmer(duration: 1500.ms, color: Colors.white24);
  }
}

class StaggeredColumn extends StatelessWidget {
  final List<Widget> children;
  final int baseDelay;
  final int staggerDelay;

  const StaggeredColumn({
    super.key,
    required this.children,
    this.baseDelay = 100,
    this.staggerDelay = 80,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        for (int i = 0; i < children.length; i++)
          children[i]
              .animate(delay: Duration(milliseconds: baseDelay + (i * staggerDelay)))
              .fadeIn(duration: 450.ms, curve: Curves.easeOutCubic)
              .slideY(begin: 0.06, end: 0, duration: 450.ms, curve: Curves.easeOutCubic),
      ],
    );
  }
}

class AnimatedCounter extends StatelessWidget {
  final int value;
  final TextStyle? style;
  final String? suffix;

  const AnimatedCounter({super.key, required this.value, this.style, this.suffix});

  @override
  Widget build(BuildContext context) {
    return TweenAnimationBuilder<int>(
      tween: IntTween(begin: 0, end: value),
      duration: const Duration(milliseconds: 800),
      curve: Curves.easeOutCubic,
      builder: (context, val, _) => Text(
        '$val${suffix ?? ''}',
        style: style,
      ),
    );
  }
}

class PageTransitions {
  static CustomTransitionPage slide(Widget child, {required String key}) {
    return CustomTransitionPage(
      key: ValueKey(key),
      child: child,
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        return SlideTransition(
          position: Tween<Offset>(
            begin: const Offset(0.15, 0),
            end: Offset.zero,
          ).animate(CurvedAnimation(parent: animation, curve: Curves.easeOutCubic)),
          child: FadeTransition(opacity: animation, child: child),
        );
      },
      transitionDuration: const Duration(milliseconds: 350),
    );
  }

  static CustomTransitionPage fade(Widget child, {required String key}) {
    return CustomTransitionPage(
      key: ValueKey(key),
      child: child,
      transitionsBuilder: (context, animation, _, child) {
        return FadeTransition(opacity: animation, child: child);
      },
      transitionDuration: const Duration(milliseconds: 300),
    );
  }
}
