import 'package:rasadyar_app/presentation/routes/app_pages.dart';
import 'package:rasadyar_auth/data/models/local/module/module_model.dart';
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_inspection/inspection.dart';
import 'package:rasadyar_livestock/presentation/routes/app_pages.dart';

class AuthService extends GetxService {
  var tokenService = Get.find<TokenStorageService>();
  RxBool accessRes = false.obs;
  RxBool refAccessRes = false.obs;

  @override
  void onInit() {
    super.onInit();

    ever(tokenService.accessToken, (callback) {
      accessRes.value = (callback != null);
    });

    ever(tokenService.refreshToken, (callback) {
      refAccessRes.value = (callback != null);
    });

    everAll([accessRes, refAccessRes], (_) {
      if (accessRes.value && refAccessRes.value) {
        var targetPage = getTargetPage(tokenService.appModule.value);
        Get.offAllNamed(targetPage);
      }
    });
  }

}
