import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/routes.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/view_models/chat_view_model.dart';
import 'package:provider/provider.dart';

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

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

class SplashPageState extends State<SplashPage> with WidgetsBindingObserver {
  bool showFlashingLights = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    startSplashTimer();
  }

  void startSplashTimer() {
    // After 3 seconds, show the flashing lights gif
    Future.delayed(const Duration(seconds: 3), () {
      if (mounted) {
        setState(() {
          showFlashingLights = true;
        });
      }
    });

    // After 5 seconds total, navigate to next page
    Future.delayed(const Duration(seconds: 5), () {
      if (mounted) {
        navigateToNextDeciderPage();
      }
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      final chatVM = context.read<ChatVM>();
      chatVM.buildHubConnection(context: context);
    }
  }

  // Timer function to navigate after a delay
  // performTimer(BuildContext context) {
  //   Utils.delay(3).whenComplete(() {
  //     navigateReplaceWithName(context, AppRoutes.registerSelection);
  //   });
  // }

  navigateToNextDeciderPage() {
    navigateReplaceWithName(context, AppRoutes.registerSelection);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.black,
        child: Column(
          children: [
            // Spacer widget for flexible spacing
            Utils.mExp(1),
            // Logo Image
            Expanded(
              child: Image.asset(
                MyAssets.icLogoWhitePng,
              ),
            ),
            // Car Image (flex value adjusted to make it more responsive)
            Expanded(
              flex: 10,
              child: showFlashingLights
                  ? Image.asset(
                      MyAssets.bnCarFlashingLights,
                      fit: BoxFit.contain,
                      width: double.infinity,
                      height: double.infinity,
                    )
                  : Transform.scale(
                      scale: 1.72, // Scale down to match gif size (1144/1920 ≈ 0.6)
                      child: Image.asset(
                        MyAssets.bnCar,
                        fit: BoxFit.contain,
                        width: double.infinity,
                        height: double.infinity,
                      ),
                    ),
            ),
            // Engine Image
            Expanded(
              flex: 3,
              child: InkWell(
                onTap: () {
                  navigateToNextDeciderPage();
                },
                child: Image.asset(
                  MyAssets.icEnginePng,
                ),
              ),
            ),
            // Spacer widget again for spacing
            Utils.mExp(1),
          ],
        ),
      ),
    );
  }
}
