import 'dart:async';
import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'config/shared_pref_kay.dart';
import 'core/service/AuthenticatedUserObject.dart';
import 'core/viewModels/project_view_model.dart';
import 'locator.dart';
import 'models/Authentication/authenticated_user.dart';

AppSharedPreferences sharedPref = new AppSharedPreferences();
AuthenticatedUserObject authenticatedUserObject =
    locator<AuthenticatedUserObject>();

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  //Provider.of<ProjectViewModel>(context, listen: false).isLogin
  @override
  void initState() {
    super.initState();
    Timer(
      Duration(seconds: 1, milliseconds: 500),
      () {
        getUserData().then((value) {
          Navigator.of(context).pushReplacement(
            MaterialPageRoute(
              builder: (BuildContext context) => LandingPage(),
            ),
          );
        });
      },
    );
  }

  Future getUserData() async {
    var data = await sharedPref.getObject(USER_PROFILE);
    if (data != null) {
      AuthenticatedUser userData = AuthenticatedUser.fromJson(data);
      Provider.of<ProjectViewModel>(context, listen: false).isLogin = true;
      authenticatedUserObject.isLogin = true;
      authenticatedUserObject.user = userData;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(12.0),
          child: Image.asset('assets/images/DQ/DQ_logo.png'),
        ),
      ),
    );
  }
}
