import 'package:flutter/material.dart';
import 'onboarding_pages.dart';
import 'home_page.dart';

class SplashScreen extends StatefulWidget {
  final bool isFirstTime;
  const SplashScreen({super.key, required this.isFirstTime});

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  double _opacity = 0.0;
  double _scale = 0.5;
  double _rotation = 0.0;
  bool _showText = false;
  late AnimationController _controller;
  late Animation<double> _pulseAnimation;

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

    // Inisialisasi AnimationController untuk efek pulsasi background
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat(reverse: true);

    _pulseAnimation = Tween<double>(begin: 0.2, end: 0.5).animate(
      CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
    );

    // Animasi fade-in dan scale untuk logo
    Future.delayed(const Duration(milliseconds: 500), () {
      setState(() {
        _opacity = 1.0;
        _scale = 1.0;
        _rotation = 0.1;
        _showText = true;
      });
    });

    // Navigasi ke halaman berikutnya setelah 5 detik
    Future.delayed(const Duration(seconds: 5), () {
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(
          builder: (context) =>
              widget.isFirstTime ? const OnboardingScreen() : const HomePage(),
        ),
      );
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // Latar belakang putih solid
          Container(
            color: Colors.white,
          ),
          // Lapisan animasi pulsasi
          AnimatedBuilder(
            animation: _pulseAnimation,
            builder: (context, child) {
              return Container(
                color: Colors.white.withOpacity(_pulseAnimation.value),
              );
            },
          ),
          // Konten utama
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                AnimatedOpacity(
                  opacity: _opacity,
                  duration: const Duration(seconds: 2),
                  child: AnimatedScale(
                    scale: _scale,
                    duration: const Duration(seconds: 2),
                    curve: Curves.easeOutBack,
                    child: Transform.rotate(
                      angle: _rotation,
                      child: Image.asset(
                        'assets/logo.png', // Ganti dengan logo Anda
                        width: 150,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
