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

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_zoom_videosdk/native/zoom_videosdk.dart';
import 'package:hmg_patient_app_new/presentation/lab/lab_orders_page.dart';
import 'package:hmg_patient_app_new/presentation/onboarding/onboarding_screen.dart';
import 'package:hmg_patient_app_new/presentation/onboarding/splash_animation_screen.dart';
import 'package:hmg_patient_app_new/core/api_consts.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/authentication/authentication_view_model.dart';

// import 'package:hmg_patient_app_new/presentation/authantication/login.dart';
import 'package:hmg_patient_app_new/presentation/home/landing_page.dart';
import 'package:hmg_patient_app_new/presentation/home/navigation_screen.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/transitions/fade_page.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';

import 'core/cache_consts.dart';
import 'core/utils/local_notifications.dart';
import 'core/utils/push_notification_handler.dart';
import 'widgets/routes/custom_page_route.dart';

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

class _SplashScreenState extends State<SplashPage> {
  late AuthenticationViewModel authVm;

  Future<void> initializeStuff() async {
    Timer(
      Duration(milliseconds: 500),
      () async {
        ApiConsts.setBackendURLs();
        PushNotificationHandler().init(context); // Asyncronously
      },
    );
    await authVm.getServicePrivilege();
    Timer(Duration(seconds: 2, milliseconds: 500), () async {
      LocalNotification.init(onNotificationClick: (payload) {});

      if (await Utils.getBoolFromPrefs(CacheConst.firstLaunch)) {
        Navigator.of(context).pushReplacement(FadePage(page: SplashAnimationScreen(routeWidget: OnboardingScreen())));
      } else {
        Navigator.of(context).pushReplacement(FadePage(page: SplashAnimationScreen(routeWidget: LandingNavigation())));
      }
    });
    var zoom = ZoomVideoSdk();
    InitConfig initConfig = InitConfig(
      domain: "zoom.us",
      enableLog: true,
    );
    zoom.initSdk(initConfig);
  }

  /// load the Privilege from service
  Future loadPrivilege() async {
    // ProjectViewModel projectProvider = Provider.of<ProjectViewModel>(context, listen: false);
    // projectProvider.setPrivilegeModelList(privilege: _privilegeService.privilegeModelList);
    // projectProvider.setVidaPlusProjectList(_privilegeService.vidaPlusProjectListModel);
    // projectProvider.setHMCProjectList(_privilegeService.hMCProjectListModel);
    // projectProvider.setProjectsDetailList(_privilegeService.projectDetailListModel);
    // double lat = await AppSharedPreferences().getDouble(USER_LAT) ?? 0.0;
    // double long = await AppSharedPreferences().getDouble(USER_LONG) ?? 0.0;
    // AppSharedPreferences().clear(); // Clearing Shared Preferences On App Launch
    // await AppSharedPreferences().setDouble(USER_LAT, lat);
    // await AppSharedPreferences().setDouble(USER_LONG, long);
    // AppSharedPreferences().setString(APP_LANGUAGE, projectProvider.isArabic ? "ar" : "en");
    // var themeNotifier = Provider.of<ThemeNotifier>(context, listen: false);
    // themeNotifier.setTheme(defaultTheme(fontName: projectProvider.isArabic ? 'Cairo' : 'Poppins'));
    PushNotificationHandler().init(context); // Asyncronously
  }

  @override
  void initState() {
    authVm = context.read<AuthenticationViewModel>();
    super.initState();
    initializeStuff();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: AppColors.whiteColor,
        body: Lottie.asset(AppAnimations.loadingAnimation, repeat: true, reverse: false, frameRate: FrameRate(60), width: 80.h, height: 80.h, fit: BoxFit.fill).center);
  }
}
