import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:gif_view/gif_view.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:vasthram/Home.dart';
import 'package:vasthram/SignUp.dart';

class UserPreferences {
  static const String isLoggedInKey = 'isLoggedIn';

  static Future<SharedPreferences> getSharedPreferences() async {
    return await SharedPreferences.getInstance();
  }

  static Future<bool> isLoggedIn() async {
    final prefs = await getSharedPreferences();
    return prefs.getBool(isLoggedInKey) ?? false;
  }
}

class SplashScreen extends StatefulWidget {
  const SplashScreen({Key? key}) : super(key: key);

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

class SplashScreenState extends State<SplashScreen> {
  bool? isLoggedIn = false;

  void timeToShowSplashScreenOnScreen() async {
    Timer(const Duration(seconds: 5), () async {
      if (!mounted) return;
      SharedPreferences prefs = await SharedPreferences.getInstance();
      var isLoggedIn = prefs.getBool("islogged") ?? false;
      if (kDebugMode) {
        print(isLoggedIn);
      }
      if (isLoggedIn) {
        if (kDebugMode) {
          print('if condition   :$isLoggedIn');
        }
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => const Home()));
      } else {
        Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(builder: (context) => const SignUp()),
            (Route<dynamic> route) => false);
      }
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration:
            const BoxDecoration(color: Color.fromARGB(255, 229, 227, 249)),
        child: GifView.asset(
          'assets/images/splash.gif',
          height: MediaQuery.of(context).size.height * 2,
          width: MediaQuery.of(context).size.width,
          frameRate: 30, //default is 15 FPS
        ),
      ),
    );
  }
}
