import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:momo/material/detail.dart';
import 'package:momo/material/gallery.dart';
import 'package:momo/material/home.dart';
import 'package:momo/material/login.dart';
import 'package:momo/material/profile.dart';

class MyMaterialRouterConfig {
  late GoRouter router;

  final GlobalKey<NavigatorState> _rootNavigatorKey =
      GlobalKey<NavigatorState>();

  MyMaterialRouterConfig(String? token) {
    router = GoRouter(
      navigatorKey: _rootNavigatorKey,
      initialLocation: "/",
      routes: <RouteBase>[
        ShellRoute(
          builder: (BuildContext context, GoRouterState state, Widget child) {
            return HomePage(
              content: child,
            );
          },
          routes: <RouteBase>[
            GoRoute(
                path: "/",
                name: "相册",
                pageBuilder: (BuildContext context, GoRouterState state) =>
                    const NoTransitionPage(child: Gallery()),
                redirect: (BuildContext context, GoRouterState state) {
                  if (token == null || token.isEmpty) {
                    return '/login';
                  }
                  return null;
                },
                routes: <RouteBase>[
                  GoRoute(
                      path: "detail",
                      name: "图片详情",
                      builder: (BuildContext context, GoRouterState state) =>
                          const ImageDetail())
                ]),
            GoRoute(
                path: "/profile",
                name: "个人资料",
                pageBuilder: (BuildContext context, GoRouterState state) =>
                    const NoTransitionPage(child: Profile())),
          ],
        ),
        GoRoute(
            path: "/login",
            name: "登录",
            builder: (BuildContext context, GoRouterState state) {
              return const LoginPage();
            },
            redirect: (BuildContext context, GoRouterState state) {
              if (token != null && token.isNotEmpty) {
                return '/';
              }
              return null;
            }),
      ],
    );
  }
}
