import 'package:flutter/material.dart';
// Assuming 'package:on_field_work/images.dart' correctly provides 'Images.logoDark'
import 'package:on_field_work/images.dart';

class SplashScreen extends StatefulWidget {
  final String? message;
  final double? logoSize;
  final Color? backgroundColor;

  const SplashScreen({
    super.key,
    this.message =
        'GET WORK DONE, ANYWHERE.', // Default message for a modern look
    this.logoSize = 150, // Slightly larger logo
    this.backgroundColor = Colors.white,
  });

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  // Animation for the logo's vertical float effect
  late Animation<double> _floatAnimation;
  // Animation for logo's initial scale-in
  late Animation<double> _scaleAnimation;
  // Animation for logo and text fade-in
  late Animation<double> _opacityAnimation;

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

    _controller = AnimationController(
      duration:
          const Duration(seconds: 3), // Longer duration for complex sequence
      vsync: this,
    );

    // Initial scale-in: from 0.0 to 1.0 (happens in the first 40% of the duration)
    _scaleAnimation = Tween<double>(begin: 0.5, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve:
            const Interval(0.0, 0.4, curve: Curves.easeOutBack), // Bouncy start
      ),
    );

    // Overall fade-in: from 0.0 to 1.0 (happens in the first 50% of the duration)
    _opacityAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.5, curve: Curves.easeIn),
      ),
    );

    // Floating effect: from 0.0 to 1.0 (loops repeatedly after initial animations)
    _floatAnimation = Tween<double>(begin: -8.0, end: 8.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.easeInOut,
      ),
    );

    // Start the complex animation sequence
    _controller.forward().then((_) {
      // After the initial scale/fade, switch to repeating the float animation
      if (mounted) {
        _controller.repeat(
          min: 0.4, // Start repeat from the float interval
          max: 1.0,
          reverse: true,
        );
      }
    });
  }

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

  // A simple, modern custom progress indicator
  Widget _buildProgressIndicator() {
    return SizedBox(
      width: 60,
      child: LinearProgressIndicator(
        backgroundColor: Colors.blueAccent.withOpacity(0.2),
        valueColor: const AlwaysStoppedAnimation<Color>(Colors.blueAccent),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: widget.backgroundColor,
      // Full screen display, no SafeArea needed for a full bleed splash
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Animated Logo (Scale, Opacity, and Float)
            FadeTransition(
              opacity: _opacityAnimation,
              child: AnimatedBuilder(
                animation: _floatAnimation,
                builder: (context, child) {
                  return Transform.translate(
                    offset: Offset(0, _floatAnimation.value),
                    child: ScaleTransition(
                      scale: _scaleAnimation,
                      child: SizedBox(
                        width: widget.logoSize,
                        height: widget.logoSize,
                        // Replace with your actual logo image widget
                        child: Image.asset(Images.logoDark),
                      ),
                    ),
                  );
                },
              ),
            ),

            const SizedBox(height: 30),

            // Text Message (Fades in slightly after logo)
            if (widget.message != null)
              FadeTransition(
                opacity: _opacityAnimation,
                child: Text(
                  widget.message!,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: Colors.grey.shade700,
                    letterSpacing: 1.2,
                  ),
                ),
              ),

            const SizedBox(height: 40),

            // Modern Loading Indicator
            _buildProgressIndicator(),
          ],
        ),
      ),
    );
  }
}
