import 'package:auto_route/auto_route.dart';

import '../../di/locator.dart';
import '../../main.dart' as main;
import 'app_router.gr.dart';

@AutoRouterConfig(
  replaceInRouteName: 'Route,Screen',
)
class AppRouter extends $AppRouter {
  final bool admin;

  AppRouter(this.admin);

  @override
  List<AutoRoute> get routes => <AutoRoute>[
        CustomRoute(
            // initial: !admin,
            initial: true,
            page: MainTabsPage.page,
            transitionsBuilder: TransitionsBuilders.noTransition,
            children: [
              CustomRoute(
                page: HomePage.page,
                transitionsBuilder: TransitionsBuilders.noTransition,
                durationInMilliseconds: 200,
              ),
              // CustomRoute(
              //   page: ExplorePage.page,
              //   transitionsBuilder: TransitionsBuilders.noTransition,
              //   durationInMilliseconds: 200,
              // ),
              // CustomRoute(
              //   page: ProfilePage.page,
              //   transitionsBuilder: TransitionsBuilders.noTransition,
              //   durationInMilliseconds: 200,
              // ),
            ]),
        CustomRoute(
          page: ProfilePage.page,
          transitionsBuilder: TransitionsBuilders.slideLeftWithFade,
          durationInMilliseconds: 200,
        ),
        CustomRoute(
          // initial: !locator.userManager.hasUser,
          page: AuthPage.page,
          transitionsBuilder: TransitionsBuilders.slideLeftWithFade,
          durationInMilliseconds: 200,
        ),
        CustomRoute(
          page: TestPage.page,
          transitionsBuilder: TransitionsBuilders.slideLeftWithFade,
          durationInMilliseconds: 200,
        ),
        CustomRoute(
          page: CardGamePage.page,
          transitionsBuilder: TransitionsBuilders.fadeIn,
          durationInMilliseconds: 400,
        ),
        CustomRoute(
          page: CardPackPage.page,
          transitionsBuilder: TransitionsBuilders.fadeIn,
          durationInMilliseconds: 200,
        ),
      ];

  static void openAuth() {
    final router = main.appRouter;
    if (!router.isRouteActive(AuthPage.name)) {
      router.replace(PageRouteInfo(AuthPage.name));
    }
  }

  static void closeAuthPage() {
    final router = main.appRouter;
    if (router.isRouteActive(AuthPage.name)) {
      router.replace(PageRouteInfo(HomePage.name));
    }
  }

  static void openAuthOrProfile() {
    if (locator.userManager.hasUser) {
      openProfile();
    } else {
      openAuth();
    }
  }

  static void openProfile() {
    final router = main.appRouter;
    if (!router.isRouteActive(ProfilePage.name)) {
      if (router.current.name == AuthPage.name) {
        router.maybePop();
      }
      router.push(PageRouteInfo(ProfilePage.name));
    }
  }
}
