import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:storemonitor/Screens/Home.dart';
import 'LoginPage.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) {
        Navigator.pushReplacement(
            context,
            MaterialPageRoute(
                builder: (context) => const Home(
                      message: 'success',
                    )));
      } else {
        if (kDebugMode) {
          print('else condition : $isLoggedIn');
        }
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => const LoginPage()),
          (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(
      body: Container(
        decoration: const BoxDecoration(
          //loadingText: Text("Loading"),
          image: DecorationImage(
            image: AssetImage("assets/images/splash_screen.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"
    }
  }*/
}
