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 {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    // performTimer(context);
  }

  @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);
    }
  }

  // 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: Image.asset(
                MyAssets.bnCar,
                fit: BoxFit.cover,
                width: double.infinity,
                height: double.infinity, // Use full height in this expanded section
              ),
            ),
            // Engine Image
            Expanded(
              flex: 3,
              child: InkWell(
                onTap: () {
                  navigateToNextDeciderPage();
                },
                child: Image.asset(
                  MyAssets.icEnginePng,
                ),
              ),
            ),
            // Spacer widget again for spacing
            Utils.mExp(1),
          ],
        ),
      ),
    );
  }
}
