import 'dart:async';
import 'package:fis_ui/values.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/lab/env/env.dart';
import 'package:flyinsono/managers/interfaces/vboxcontrol.dart';
import 'package:get/get.dart';

import 'prompt_box.dart';

Timer? _reloginTimer;

class GlobalLogout {
  static void relogin() {
    if (_reloginTimer != null && _reloginTimer!.isActive) return;

    if (router.currentName == RouteNames.Auth.AuthBoard) return;

    _reloginTimer = Timer(const Duration(milliseconds: 500), () async {
      Store.app.busy = true;
      await Store.user.handelLogOut();
      Store.app.busy = false;
      if (router.currentName != RouteNames.Auth.AuthBoard) {
        if (kIsLab) {
          router.offAll(RouteNames.Lab.Login);
          return;
        }
        if (kIsMobile && Get.context!.isLandscape) {
          SystemChrome.setPreferredOrientations(
              [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
          // 延迟1000ms，等待屏幕旋转完成
          await Future.delayed(Duration(milliseconds: 300));
          router.offAll(RouteNames.Auth.AuthBoard);
        } else {
          router.offAll(RouteNames.Auth.AuthBoard);
        }
      }
      if (CartSystemPlatform.isCartSystemVersion) {
        var vBoxManager = Get.find<IVBoxControlManager>();
        vBoxManager.canShowHardwareDetector = false;
      }
      PromptBox.snackbar(i18nBook.auth.authExpired.t);
      PromptBox.$SetEnable(false);

      Future.delayed(const Duration(milliseconds: 500), () {
        PromptBox.$SetEnable(true);
      });

      _reloginTimer = null;
    });
  }
}
