/*
*@Author: XianKaiQun
*@Date: 2020-04-20 16:05:51
 * @LastEditors  : WuWei
 * @LastEditTime : 2022-10-31 14:51:47
*@Descripttion:
*/
part of 'index.dart';

class _Store with ChangeNotifier {
  final BuildContext context;
  _Store(this.context) {
    print(WisStorageUtil.get(key: 'isInstallApp'));
    // print(WisStorageUtil.get(key: 'isReadPrivacy'));
    route();
  }
  final authGlobalStore = GetIt.I.get<AuthGlobalStore>();

  bool get isInstallApp => WisStorageUtil.get(key: 'isInstallApp') ?? false;

  bool get _isReadPrivacy => WisStorageUtil.get(key: 'isReadPrivacy') ?? false;

  // 不同意
  onNotAgree() async {
    await WisStorageUtil.set(key: 'isInstallApp', data: false);
    await WisStorageUtil.set(key: 'isReadPrivacy', data: false);
    exitApp();
  }

  onAgree() async {
    await WisStorageUtil.set(key: 'isReadPrivacy', data: true);
    // route();

    print(
        '---------------------------------------------------------------------mainmainmain-------------------------------------------');

    // ///推送依赖注入
    // if (!GetIt.I.isRegistered<PushGlobalStore>()) {
    //   GetIt.I.registerLazySingleton(() => PushGlobalStore());
    // }

    ///APP配置依赖注入
    GetIt.I.registerLazySingleton(() => AppGlobalStore());

    ///用户配置依赖注入
    GetIt.I.registerLazySingleton(() => AuthGlobalStore());

    ///小区配置依赖注入
    GetIt.I.registerLazySingleton(() => CommunityGlobalStore());

 
  }

  ///去请求获取登录的用户信息   如果不能获取  说明用户未登录
  route() async {
    final res = await authGlobalStore.getInfo();
    if (isInstallApp == true && _isReadPrivacy == true) {
      await Future.delayed(Duration(milliseconds: 1000));

      ///  slapsh页面增加过渡动画
      await toLogin(res);
    }
  }

  toLogin(res) async {
    WNavUtil.instance!.push(
      PageRouteBuilder(
        transitionDuration: const Duration(milliseconds: 150),
        pageBuilder: (context, animation, secondaryAnimation) {
          const begin = Offset(1.0, 0.0);
          const end = Offset.zero;
          const curve = Curves.ease;
          final tween =
              Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
          final offsetAnimation = animation.drive(tween);

          return SlideTransition(
            position: offsetAnimation,
            child: res ? MainPage() : LoginPage(),
          );
        },
      ),
    );
  }

  void exitApp() {
    exit(0);
  }
}
