import 'package:fis_i18n/i18n.dart';
import 'package:flutter/widgets.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/managers/interfaces/vboxcontrol.dart';
import 'package:get/get.dart';

/// 检查是否有权限
class RouteAuthMiddleware extends GetMiddleware {
  // priority 数字小优先级高
  @override
  int? priority = 0;

  /// 单Key值验证
  final String? feature;

  /// 组合Key值验证
  final List<String>? featureGroup;

  RouteAuthMiddleware({
    this.priority,
    this.feature,
    this.featureGroup,
  }) : assert(() {
          if (feature == null && featureGroup == null) {
            throw ArgumentError(
                "RouteAuthMiddleware props `feature` and `featureGroup` cannot both be null.");
          }
          if (feature != null && feature.isEmpty)
            throw ArgumentError(
                "RouteAuthMiddleware prop `feature` cannot be empty.");
          if (featureGroup != null) {
            if (featureGroup.isEmpty) {
              throw ArgumentError(
                  "RouteAuthMiddleware prop `featureGroup` cannot be empty.");
            }
            if (featureGroup.any((x) => x.isEmpty)) {
              throw ArgumentError(
                  "RouteAuthMiddleware prop `featureGroup` cannot contains empty value.");
            }
          }
          return true;
        }());

  @override
  RouteSettings? redirect(String? route) {
    final limitedFeatures = _getLimitFeatures();
    final userFeatures = Store.user.features;
    for (var key in limitedFeatures) {
      if (!userFeatures.contains(key)) {
        return _returnUnpassed();
      }
    }
    return null;
  }

  RouteSettings _returnUnpassed() {
    Future.delayed(
      const Duration(milliseconds: 500),
      () => PromptBox.snackbar(i18nBook.auth.unauthorized.t),
    );
    return RouteSettings(name: RouteNames.Error.Unauthorized);
  }

  List<String> _getLimitFeatures() {
    final arr = featureGroup != null ? featureGroup! : <String>[];
    if (feature != null && feature!.isNotEmpty) {
      arr.add(feature!);
    }
    return arr;
  }
}

class RouteLoginMiddleware extends GetMiddleware {
  // priority 数字小优先级高
  @override
  int? priority = 1;

  RouteLoginMiddleware({this.priority});

  static final _whiteList = [
    RouteNames.Auth.AuthBoard,
    RouteNames.App.Splash,
    RouteNames.App.SecondSplash,
    RouteNames.Auth.SignUp,
    RouteNames.Auth.SignIn,
    RouteNames.Auth.RetrievePwd,
    RouteNames.Lab.Login,
    RouteNames.Lab.Main,
    MobileRouteNames.Wechat.MiniProgramHome
  ];

  @override
  RouteSettings? redirect(String? route) {
    if (Store.user.isLogin) {
      return null;
    }

    if (_whiteList.contains(route)) {
      return null;
    }
    if (CartSystemPlatform.isCartSystemVersion) {
      var vBoxManager = Get.find<IVBoxControlManager>();
      vBoxManager.canShowHardwareDetector = false;
    }
    Future.delayed(
      const Duration(milliseconds: 500),
      () => PromptBox.snackbar(i18nBook.auth.authExpired.t),
    );

    return RouteSettings(name: RouteNames.Auth.AuthBoard);
  }
}
