import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:logger/logger.dart';
import 'package:lottie/lottie.dart';
import 'package:sigap/src/features/auth/data/repositories/authentication_repository.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/image_strings.dart';
import 'package:sigap/src/utils/helpers/helper_functions.dart';

class AnimatedSplashScreenWidget extends StatefulWidget {
  const AnimatedSplashScreenWidget({super.key});

  @override
  State<AnimatedSplashScreenWidget> createState() =>
      _AnimatedSplashScreenWidgetState();
}

class _AnimatedSplashScreenWidgetState extends State<AnimatedSplashScreenWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  final storage = GetStorage();

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

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

    _animation = CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    );

    _animationController.forward();

    // Delay for splash screen duration
    Future.delayed(const Duration(milliseconds: 3500), () {
      _handleNavigation();
    });
  }

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

  Future<void> _handleNavigation() async {
    AuthenticationRepository.instance.screenRedirect();
  }

  @override
  Widget build(BuildContext context) {
    final isDark = THelperFunctions.isDarkMode(context);
    final isFirstTime = storage.read('isFirstTime') ?? false;

    Logger().i('isFirstTime: $isFirstTime');

    return Scaffold(
      backgroundColor: isDark ? TColors.dark : TColors.white,
      body: Center(
        child: FadeTransition(
          opacity: _animation,
          child: Lottie.asset(
            isDark ? TImages.darkSplashApp : TImages.lightSplashApp,
            frameRate: FrameRate.max,
            repeat: true,
            width: 300,
            height: 300,
          ),
        ),
      ),
    );
  }
}
