import 'package:flutter/material.dart';
import 'package:plugin_basic/constants/app_constant.dart';
import 'package:plugin_basic/provider/user_config/user_config_service.dart';
import 'package:plugin_platform/engine/sp/sp_util.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:router/componentRouter/component_service_manager.dart';
import 'package:shared/utils/log_utils.dart';

part 'splash_view_model.g.dart';

@riverpod
class SplashViewModel extends _$SplashViewModel {
  @override
  void build() {
    // 初始构建逻辑
    Log.d("SplashViewModel - 初始构建逻辑");
    _gotoNextPage();
  }

  BuildContext? _context;

  void setContext(BuildContext context) {
    Log.d("赋值Context");
    _context = context;
  }

  void _gotoNextPage() async {
    await Future.delayed(const Duration(seconds: 1));

    // await AppInitializer.delayInitialize(futureTasks: [
    //   () async {
    //     await FlutterBugly.init(
    //       androidAppId: "12345678",
    //       iOSAppId: "12345678",
    //     );
    //   },
    // ], normalTasks: [
    //   () {
    //     if (DeviceUtils.isIOS || DeviceUtils.isAndroid) {
    //       jpush.init();
    //     }
    //   },
    // ]);

    // 先查询原生平台有没有保存需要跳转的子路由
    // String routerName = await AppChannel.getNativeRouterName();
    // Log.d('SplashController - 查询原生平台有没有保存需要跳转的子路由:$routerName');

    UserConfigService.getInstance();

    if (UserConfigService.getState().hasLogin == true) {
      Log.d("已经登录，去首页页面");
      ComponentServiceManager().mainService.startMainPage();
    } else {
      //去登录页面
      Log.d("没有登录，去登录页面");
      ComponentServiceManager().authService.startAndPopAllLoginPage();
    }
  }
}
