import 'package:get/get.dart';

import 'package:app/jpush/jpush_receive.dart';
import 'package:initializer/app_initializer.dart';
import 'package:plugin_basic/constants/app_constant.dart';
import 'package:plugin_basic/service/app_config_service.dart';
import 'package:plugin_basic/service/user_service.dart';
import 'package:plugin_platform/engine/sp/sp_util.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:router/componentRouter/component_router_service.dart';
import 'package:shared/utils/device_utils.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

class SplashController extends GetxController {
  @override
  void onReady() {
    super.onReady();
    UserService.to;
    _gotoNextPage();
  }
}

void _gotoNextPage() async {
  // 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');

  String? token = SPUtil.getString(AppConstant.storageToken);
  UserService.to.setToken(token);

  if (UserService.to.isLogin) {
    //去Attendance页面签到
    Log.d("去首页页面");
    if (ConfigService.to.selectCountry.value == 1) {
      //去新加坡首页
      ComponentRouterServices.sgService.startSGMainPage();
    } else if (ConfigService.to.selectCountry.value == 2) {
      //去英国首页
      ComponentRouterServices.ukService.startUKMainPage();
    } else if (ConfigService.to.selectCountry.value == 3) {
      //去马来首页
      ComponentRouterServices.msService.startMSMainPage();
    } else if (ConfigService.to.selectCountry.value == 4) {
      //去荷兰首页
      ComponentRouterServices.nlService.startNLMainPage();
    } else if (ConfigService.to.selectCountry.value == 5) {
      //去泰国首页
      ComponentRouterServices.thService.startTHMainPage();
    } else {
      //去越南首页
      ComponentRouterServices.vnService.startVNMainPage();
    }
  } else {
    //去登录页面
    Log.d("去登录页面");
    ComponentRouterServices.authService.startPopAllLoginPage();
  }

  // if (!Utils.isEmpty(routerName)) {
  //   // 只跳转一次
  //   Get.offAllNamed(routerName);
  //   AppChannel.clearNativeRouterName();
  // } else {
  //   // 如果没有经历过GUIDE页面，进入GUIDE，否则进入首页
  //     MainPage.startWithPopAll();
  // }

  //跳转页面之后清除 Android 的 SplashScreen 库
  // if (Device.isAndroid) {
  //   AppChannel.skipAndroidSplashScreen();
  // }
}
