import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:watermanagement/dashboard.dart';
import 'package:watermanagement/settings.dart';
import 'package:watermanagement/signup.dart';
import 'fcm_notifications.dart';
import 'package:firebase_core/firebase_core.dart';

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

class Splash extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Water Management',
      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> {


  Widget _defaultHome = new SignIn();



  void loginCheck()async{

    bool _result = await AppSettings.isSigIn();

    /*store device information in firebase*/
    try{
      if(Platform.isIOS){
        await FireBaseCore.init();
      }
      //await FireBaseCore.init();
      await FireBaseCore.checkFcmTokenInFireStoreDbWithdeviceId();
      // AppSettings.fcmId=await AppSettings.getData('FCM_TOKEN', 'STRING');
    }
    catch(e){
      print(e);
    }

    if (_result) {
      await AppSettings.loadDataFromMemory();
      AppSettings.fcmId=await AppSettings.getData('FCM_TOKEN', 'STRING');
      await AppSettings.getProfile();
      _defaultHome = new Dashboard();
    }
  }


  @override
  void initState() {

    loginCheck();
    super.initState();
    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/img.png"), fit: BoxFit.cover,),
              ),
            ),

          ],
        )
    );
  }

}