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;

  MyMaterialRouterConfig(String? token) {
    router = GoRouter(
      routes: <RouteBase>[
        ShellRoute(
          builder: (BuildContext context, GoRouterState state, Widget child) {
            return HomePage(
              content: child,
            );
          },
          routes: [
            GoRoute(
                path: "/",
                pageBuilder: (BuildContext context, GoRouterState state) =>
                    const NoTransitionPage(child: Gallery()),
                redirect: (BuildContext context, GoRouterState state) {
                  if (token == null || token.isEmpty) {
                    return '/login';
                  }
                  return null;
                }),
            GoRoute(
                path: "/profile",
                pageBuilder: (BuildContext context, GoRouterState state) =>
                    const NoTransitionPage(child: Profile())),
            GoRoute(
                path: "/detail",
                pageBuilder: (BuildContext context, GoRouterState state) =>
                    const NoTransitionPage(child: ImageDetail())),
          ],
        ),
        GoRoute(
            path: "/login",
            builder: (BuildContext context, GoRouterState state) {
              return const LoginPage();
            },
            redirect: (BuildContext context, GoRouterState state) {
              if (token != null && token.isNotEmpty) {
                return '/';
              }
              return null;
            }),
      ],
    );
  }
}
