import 'dart:async';
import 'dart:convert';

import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
import 'package:diplomaticquarterapp/theme/theme_notifier.dart';
import 'package:diplomaticquarterapp/theme/theme_value.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';

import 'core/service/AuthenticatedUserObject.dart';
import 'core/service/privilege_service.dart';
import 'core/viewModels/project_view_model.dart';
import 'locator.dart';

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

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

class _SplashScreenState extends State<SplashScreen> {
  PrivilegeService _privilegeService = locator<PrivilegeService>();

  @override
  void initState() {
    super.initState();
    Timer(
      Duration(seconds: 1, milliseconds: 500),
      () {
        loadPrivilege().then((value) {
          Navigator.of(context).pushReplacement(
            MaterialPageRoute(
              builder: (BuildContext context) => LandingPage(),
            ),
          );
        });
      },
    );


    AppSharedPreferences().getAll().then((value){
      debugPrint(jsonEncode(value));
    });
  }

  /// load the Privilege from service
  Future loadPrivilege() async {
    await _privilegeService.getPrivilege();
    ProjectViewModel projectProvider = Provider.of<ProjectViewModel>(context, listen: false);
    projectProvider.setPrivilegeModelList(privilege: _privilegeService.privilegeModelList);
    var themeNotifier = Provider.of<ThemeNotifier>(context, listen: false);
    themeNotifier.setTheme(defaultTheme(fontName: projectProvider.isArabic ? 'Cairo' : 'Poppins'));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xffF8F8F8),
      body: Stack(
        alignment: Alignment.center,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 53),
            child: Image.asset(
              'assets/images/new/hmg_logo.png',
              fit: BoxFit.fitWidth,
              width: MediaQuery.of(context).size.width,
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  TranslationBase.of(context).poweredBy,
                  style: TextStyle(fontSize: 14, fontWeight: FontWeight.w400, color: Color(0xff333C45), letterSpacing: -0.56, height: 16 / 14),
                ),
                SizedBox(
                  height: 5,
                ),
                SvgPicture.asset(
                  'assets/images/new/cloud_logo.svg',
                  width: 40,
                  height: 40,
                ),
                SizedBox(
                  height: 7,
                ),
                Text(
                  "Version 1.1.0",
                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w400, color: Color(0xff3989898), letterSpacing: 0, height: 12 / 10),
                ),
                SizedBox(
                  height: 18,
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
