import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:krow/core/application/di/injectable.dart';
import 'package:krow/core/application/routing/guards.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/sevices/auth_state_service/auth_service.dart';

@AutoRouterConfig(
  replaceInRouteName: 'Screen|Page,Route',
)
class AppRouter extends RootStackRouter {
  @override
  RouteType get defaultRouteType => const RouteType.material();

  Future<DeepLink> deepLinkBuilder(PlatformDeepLink deepLink) async {
    final uri = deepLink.uri;
    if (uri.queryParameters.containsKey('oobCode')) {
      final code = uri.queryParameters['oobCode'];
      popUntil((route) => false);
      return DeepLink([
        const WelcomeRoute(),
        const SignInRoute(),
        EnterNewPassRoute(code: code),
      ]);
    }
    return deepLink;
  }

  @override
  List<AutoRoute> get routes => [
        AdaptiveRoute(
          path: '/',
          page: SplashRoute.page,
          initial: true,
          guards: [SplashRedirectGuard(getIt<AuthService>())],
        ),
        createEventFlow,
        authFlow,
        homeFlow,
      ];

  get authFlow => AdaptiveRoute(
        path: '/auth',
        page: SignInFlowRoute.page,
        children: [
          AutoRoute(path: 'welcome', page: WelcomeRoute.page, initial: true),
          AutoRoute(
            path: 'sign_in',
            page: SignInRoute.page,
          ),
          AutoRoute(
            path: 'reset_pass',
            page: ResetPassRoute.page,
          ),
          AutoRoute(
            path: 'pass',
            page: EnterNewPassRoute.page,
          ),
        ],
      );

  get createEventFlow =>
      AutoRoute(path: '/create', page: CreateEventFlowRoute.page, children: [
        AutoRoute(path: 'edit', page: CreateEventRoute.page, initial: true),
        AutoRoute(
          path: 'preview',
          page: EventDetailsRoute.page,
        ),
      ]);

  get homeFlow => AdaptiveRoute(
      path: '/home',
      page: HomeRoute.page,
      children: [eventsFlow, invoiceFlow, notificationsFlow, profileFlow]);

  get eventsFlow =>
      AdaptiveRoute(path: 'events', page: EventsFlowRoute.page, children: [
        AutoRoute(path: 'list', page: EventsListMainRoute.page, initial: true),
        AutoRoute(path: 'details', page: EventDetailsRoute.page),
        AutoRoute(path: 'assigned', page: AssignedStaffRoute.page),
        AutoRoute(path: 'assigned_manual', page: ClockManualRoute.page),
        AutoRoute(path: 'rate_staff', page: RateStaffRoute.page),
      ]);

  get invoiceFlow =>
      AdaptiveRoute(path: 'invoice', page: InvoiceFlowRoute.page, children: [
        AutoRoute(
            path: 'list', page: InvoicesListMainRoute.page, initial: true),
        AutoRoute(
            path: 'details', page: InvoiceDetailsRoute.page),
        AutoRoute(
          path: 'event',
          page: EventDetailsRoute.page,
        ),
      ]);

  get notificationsFlow => AdaptiveRoute(
          path: 'notifications',
          page: NotificationFlowRoute.page,
          children: [
            AutoRoute(
                path: 'list', page: NotificationsListRoute.page, initial: true),
            AutoRoute(
              path: 'details',
              page: NotificationDetailsRoute.page,
            ),
          ]);

  get profileFlow =>
      AdaptiveRoute(path: 'profile', page: ProfileFlowRoute.page, children: [
        AutoRoute(
            path: 'preview', page: ProfilePreviewRoute.page, initial: true),
        AutoRoute(
          path: 'edit',
          page: PersonalInfoRoute.page,
        ),
      ]);

  @override
  List<AutoRouteGuard> get guards => [];
}
