import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:in_app_update/in_app_update.dart';
import '../../constants/asset_constants.dart';
import '../../constants/color_constants.dart';
import '../../constants/font_constants.dart';
import '../../widgets/text_widget.dart';
import '../intro_view/intro_screen_view.dart';


class SplashScreenView extends StatefulWidget {
  const SplashScreenView({super.key});
  @override
  SplashScreenViewState createState() => SplashScreenViewState();
}

class SplashScreenViewState extends State<SplashScreenView>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<Offset> _slideAnimation;
  late Animation<Color?> _colorAnimation;
  bool showImage = true;
  bool showOverlay = true;

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

    // ✅ In-app update check
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      try {
        AppUpdateInfo updateInfo = await InAppUpdate.checkForUpdate();
        if (updateInfo.updateAvailability == UpdateAvailability.updateAvailable) {
          await InAppUpdate.performImmediateUpdate();
        } else {
          print("✅ App is already up-to-date");
        }
      } catch (e) {
        print("⚠️ Update check failed: $e");
      }
    });

    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );

    // Slide animation from left to right
    _slideAnimation = Tween<Offset>(
      begin: const Offset(-1.0, 0.0), // Start off-screen left
      end: const Offset(0.0, 0.0),    // End at center
    ).animate(
      CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
    );

    // Color transition
    _colorAnimation = ColorTween(
      begin: ColorConstants.secondaryColor,
      end: Colors.white,
    ).animate(
      CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
    );

    // Show image and loader for 3 seconds
    Future.delayed(const Duration(seconds: 1), () {
      setState(() {
        showOverlay = false; // Hide loader after 3 seconds
      });

      // Start the slide and color transition animations
      _controller.forward().whenComplete(() {
        Future.delayed(const Duration(milliseconds: 0), () {
          Get.off(() =>  IntroScreenView()); // Navigate to the next screen
        });
      });
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        extendBodyBehindAppBar: true,
        body: AnimatedBuilder(
          animation: _controller,
          builder: (context, child) {
            return Container(
              color: _colorAnimation.value, // Use animated background color
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    if (showImage || showOverlay) // Show image and overlay for 3 seconds
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          if (showImage)
                            SlideTransition(
                              position: _slideAnimation,
                              child: Image.asset(
                                AssetConstants.splashImage,
                                width: 300,
                                height: 300,
                              ),
                            ),
                        ],
                      ),
                  ],
                ),
              ),
            );
          },
        ),
        // bottomNavigationBar:  BottomAppBar(
        //   color: ColorConstants.secondaryColor,
        //   height: 50,
        //   child: ReusableTextWidget(
        //     text: 'All rights reserved - 2025',
        //     fontFamily: FontConstants.fontFamily,
        //     color: ColorConstants.primaryColor,
        //     fontWeight: FontWeight.w500,
        //     fontSize: 16,
        //     textAlign: TextAlign.center,
        //   ),
        // )

    );
  }
}