import 'package:design/design.dart';
import 'package:example/data/constants.dart';
import 'package:guru_app/financial/data/db/order_database.dart';
import 'package:guru_app/guru_app.dart';
import 'package:guru_app/property/settings/guru_settings.dart';
import 'package:guru_utils/packages/guru_package.dart';
import 'package:injectable/injectable.dart';
import 'package:guru_utils/router/router.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import '../route/router.dart';
import 'settings/debug_settings.dart';

part 'initializer.g.dart';

@guruSpecCreator
AppSpec createAppSpec(String flavor) {
  return _GuruSpecFactory.create(flavor);
}

class _RootPackage extends RootPackage {
  bool migrate = false;

  @override
  Future initialize() async {
    // GuruAnalytics.disableAnalytics();
    Log.isDebug = kDebugMode;

    await DebugSettings.instance.refresh();

    Initializer.initialPath = AppPages.initialPath;

    RouteCenter.initialize(routeMatchers: [
      RouteMatcher(
          checker: (route) =>
              (route.host == GuruApp.instance.appSpec.details.authority) &&
              (route.path == Initializer.initialPath),
          dispatcher: (uri) async {
            // 忽略分享回流链接为主页的情况
            return;
          }),
    ]);
  }

  @override
  // TODO: implement localizationsDelegates
  Iterable<LocalizationsDelegate> get localizationsDelegates => const [
        // Built-in localization of basic text for Material widgets
        GlobalMaterialLocalizations.delegate,
        // Built-in localization for text direction LTR/RTL
        GlobalWidgetsLocalizations.delegate,
        // Built-in localization of basic text for Cupertino widgets
        GlobalCupertinoLocalizations.delegate,
      ];

  @override
  // TODO: implement supportedLocales
  Iterable<Locale> get supportedLocales => throw UnimplementedError();
}

class ComplianceProtocol implements IGuruSdkComplianceProtocol {
  @override
  int getCurrentLevel() {
    return 1;
  }

  @override
  String getLevelName() {
    return "";
  }
}

@singleton
class Initializer {
  // final TransactionService transactionService;
  // final AccountService accountService;

  // final GuruAppCompat guruAppCompat;

  static String initialPath = "/";

  bool initialized = false;

  Initializer() {
    _initialize();
  }

  static _initialize() {}

  static AppEnv _buildAppEnv({String flavor = ""}) {
    final rootPackage = _RootPackage();

    return AppEnv(
        spec: createAppSpec(flavor),
        package: rootPackage,
        complianceProtocol: ComplianceProtocol());
  }

  static Future ensureInitialized() async {
    await GuruApp.initialize(appEnv: _buildAppEnv(flavor: Flavors.classic));
  }
}
