import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.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: 3), () 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.pushReplacement(
            context, MaterialPageRoute(builder: (context) => const Home()));
      } else {
        if(kDebugMode) {
        }
        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(
          //loadingText: Text("Loading"),
          image: DecorationImage(
            image: AssetImage("assets/images/splash.png"),
            fit: BoxFit.cover,
          ),
        ),
      ),
    );
  }
}
