import 'package:tacban/src/app/auth/screens/cadastro.dart';
import 'package:tacban/src/app/auth/screens/codigoverifica.dart';
import 'package:tacban/src/app/auth/screens/login.dart';
import 'package:tacban/src/app/base/screens/navigator.dart';
import 'package:tacban/src/app/base/screens/splash.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:tacban/src/app/home/screens/categoria_screen.dart';
import 'package:tacban/src/app/home/screens/formulario_screen.dart';
import 'package:tacban/src/app/home/screens/home_screen.dart';
import 'package:tacban/src/app/home/screens/relatorio_screen.dart';

final GlobalKey<NavigatorState> _rootNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'root');
final GlobalKey<NavigatorState> _shellNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'shell');

final routerProvider = Provider<GoRouter>((ref) {
  return GoRouter(
    initialLocation: SlapshScreen.path,
    navigatorKey: _rootNavigatorKey,
    debugLogDiagnostics: false,
    routes: [
      GoRoute(
        path: SlapshScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) => const SlapshScreen()
      ),
      GoRoute(
        path: LoginScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) => const LoginScreen()
      ),
      GoRoute(
        path: CadastroScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) => const CadastroScreen()
      ),
      GoRoute(
        path: CodigoVerificacaoScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) => const CodigoVerificacaoScreen()
      ),
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
        builder: (context, state, child) => NavigatorScreen(child: child),
        routes: [
          GoRoute(
            path: HomeScreen.path,
            name: 'inicio_vistoria',
            pageBuilder: (_, state) => transitionScreen(
              state.pageKey,
              child: const HomeScreen(),
            )
          )
        ]
      ),
      GoRoute(
        path: CategoriaScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) {
          String title = state.extra as String;
          return CategoriaScreen(title: title);
        }
      ),
      GoRoute(
        path: FormularioScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) => const FormularioScreen()
      ),
      GoRoute(
        path: RelatorioScreen.path,
        parentNavigatorKey: _rootNavigatorKey,
        builder: (_, state) => const RelatorioScreen()
      ),
    ]
  );
});


CustomTransitionPage transitionScreen(LocalKey key, {required Widget child}){
  return CustomTransitionPage(
    key: key,
    child: child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      return FadeTransition(
        opacity: CurveTween(
          curve: Curves.ease
          // curve: Curves.easeInOutCirc
        ).animate(animation),
        child: child,
      );
    }
  );
}