import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:supplier_new/common/dashboard.dart';
import 'package:supplier_new/common/settings.dart';
import 'package:supplier_new/login/login.dart';
import 'package:supplier_new/login/starting_screen.dart';

import '../login/login_signup_screen.dart';

void main() async{
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  await Firebase.initializeApp();
  //FirebaseMessaging.onBackgroundMessage(_messageHandler);
  runApp(new Splash());
}


class Splash extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Aquick Supplier',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new SplashScreen(),
    );
  }
}

class SplashScreen extends StatefulWidget {
  const SplashScreen({ super.key });

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {

  final storage = FlutterSecureStorage(
    aOptions: AndroidOptions(
      resetOnError: true,
      encryptedSharedPreferences: true,
    ),
  );

  Future<void> _checkLoginStatus() async {
    await Future.delayed(Duration(seconds: 2)); // Simulate splash delay
    String? authToken = await storage.read(key: 'authToken');
    if (authToken != null) {
      await AppSettings.loadDataFromMemory();
      //AppSettings.fcmId=await AppSettings.getData('FCM_TOKEN', 'STRING');
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => DashboardScreen()),
      );
    } else {
      // Navigate to Login Screen otherwise
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => LoginSignUpScreen()),
      );
    }
  }



  @override
  void initState() {

    //loginCheck();
    super.initState();
    _checkLoginStatus();
    /* Future.delayed(
        const Duration(seconds: 3),
            () => Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => _defaultHome),
        ));*/
  }
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        body:  Stack(
          children: <Widget>[
            Container(
              decoration:  const BoxDecoration(
                image:  DecorationImage(image:  AssetImage("images/final_splash_screen.jpg"), fit: BoxFit.fill,),
              ),
            ),

          ],
        )
    );
  }

}