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) {
          print('else condition :$isLoggedIn');
        }
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => const SignUp()),
          (Route<dynamic> route) => false,
        );
        // }
      }
    });
  }

  @override
  void initState() {
    super.initState();
    //Constants('header', 'intit').showAlertDialog();
    timeToShowSplashScreenOnScreen();
    //getDeviceId();
    //Constants.showAlertDialog1(context);
  }

  @override
  Widget build(BuildContext context) {
    /* return Scaffold(
      appBar: AppBar(
        title: Text('Splash Screen'),
      ),
    ); */

    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          //loadingText: Text("Loading"),
          image: DecorationImage(
            image: AssetImage("assets/images/splash.png"),
            fit: BoxFit.cover,
          ),
        ),
      ),
    );
  }

  /*Future<void> getDeviceId() async {
    String uniqueDeviceId = (await PlatformDeviceId.getDeviceId)!;
    print(uniqueDeviceId);
    if (Platform.isAndroid) {
      final info = await DeviceInfoPlugin().iosInfo;
      print(info.model);
      //print(info.systemVersion);
    }
    if (Platform.isIOS) {
      final info = await DeviceInfoPlugin().iosInfo;
      print(info.utsname.machine); // "iPhone15,2"
      print(info.utsname.version); // "iPhone 14 Pro"
    }
  } */
}
