import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:twonly/app.dart';
import 'package:twonly/src/constants/routes.keys.dart';
import 'package:twonly/src/database/twonly.db.dart';
import 'package:twonly/src/visual/views/camera/camera_qr_scanner.view.dart';
import 'package:twonly/src/visual/views/camera/camera_send_to.view.dart';
import 'package:twonly/src/visual/views/chats/archived_chats.view.dart';
import 'package:twonly/src/visual/views/chats/chat_messages.view.dart';
import 'package:twonly/src/visual/views/chats/media_viewer.view.dart';
import 'package:twonly/src/visual/views/chats/start_new_chat.view.dart';
import 'package:twonly/src/visual/views/contact/add_new_contact.view.dart';
import 'package:twonly/src/visual/views/contact/contact.view.dart';
import 'package:twonly/src/visual/views/groups/group.view.dart';
import 'package:twonly/src/visual/views/groups/group_create_select_members.view.dart';
import 'package:twonly/src/visual/views/onboarding/recover_password.view.dart';
import 'package:twonly/src/visual/views/onboarding/recover_passwordless.view.dart';
import 'package:twonly/src/visual/views/public_profile.view.dart';
import 'package:twonly/src/visual/views/settings/account.view.dart';
import 'package:twonly/src/visual/views/settings/appearance.view.dart';
import 'package:twonly/src/visual/views/settings/backup/backup_settings.view.dart';
import 'package:twonly/src/visual/views/settings/backup/backup_setup.view.dart';
import 'package:twonly/src/visual/views/settings/chat/chat_reactions.view.dart';
import 'package:twonly/src/visual/views/settings/chat/chat_settings.view.dart';
import 'package:twonly/src/visual/views/settings/data_and_storage.view.dart';
import 'package:twonly/src/visual/views/settings/data_and_storage/import_from_gallery.view.dart';
import 'package:twonly/src/visual/views/settings/data_and_storage/manage_storage.view.dart';
import 'package:twonly/src/visual/views/settings/developer/automated_testing.view.dart';
import 'package:twonly/src/visual/views/settings/developer/developer.view.dart';
import 'package:twonly/src/visual/views/settings/developer/informations.view.dart';
import 'package:twonly/src/visual/views/settings/developer/reduce_flames.view.dart';
import 'package:twonly/src/visual/views/settings/developer/retransmission_data.view.dart';
import 'package:twonly/src/visual/views/settings/help/changelog.view.dart';
import 'package:twonly/src/visual/views/settings/help/contact_us.view.dart';
import 'package:twonly/src/visual/views/settings/help/credits.view.dart';
import 'package:twonly/src/visual/views/settings/help/diagnostics.view.dart';
import 'package:twonly/src/visual/views/settings/help/faq.view.dart';
import 'package:twonly/src/visual/views/settings/help/faq/verification_badge_faq.view.dart';
import 'package:twonly/src/visual/views/settings/help/help.view.dart';
import 'package:twonly/src/visual/views/settings/help/news.view.dart';
import 'package:twonly/src/visual/views/settings/notification.view.dart';
import 'package:twonly/src/visual/views/settings/privacy.view.dart';
import 'package:twonly/src/visual/views/settings/privacy/block_users.view.dart';
import 'package:twonly/src/visual/views/settings/privacy/user_discovery.view.dart';
import 'package:twonly/src/visual/views/settings/profile/modify_avatar.view.dart';
import 'package:twonly/src/visual/views/settings/profile/profile.view.dart';
import 'package:twonly/src/visual/views/settings/settings_main.view.dart';
import 'package:twonly/src/visual/views/settings/share_with_friends.view.dart';
import 'package:twonly/src/visual/views/settings/subscription/subscription.view.dart';
import 'package:twonly/src/visual/views/user_study/user_study_questionnaire.view.dart';
import 'package:twonly/src/visual/views/user_study/user_study_welcome.view.dart';

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

final routerProvider = GoRouter(
  navigatorKey: rootNavigatorKey,
  routes: [
    GoRoute(
      path: Routes.home,
      builder: (context, state) => const AppMainWidget(initialPage: 1),
    ),
    GoRoute(
      path: Routes.recoverPasswordless,
      builder: (context, state) {
        final token = state.extra as String?;
        return RecoverPasswordless(initialEmailToken: token);
      },
    ),

    // Chats
    GoRoute(
      path: Routes.chats,
      builder: (context, state) => const AppMainWidget(initialPage: 0),
      routes: [
        GoRoute(
          path: 'add_new_user',
          builder: (context, state) => const AddNewUserView(),
        ),
        GoRoute(
          path: 'archived',
          builder: (context, state) => const ArchivedChatsView(),
        ),
        GoRoute(
          path: 'start_new_chat',
          builder: (context, state) => const StartNewChatView(),
        ),
        GoRoute(
          path: 'camera_send_to',
          builder: (context, state) {
            final group = state.extra! as Group;
            return CameraSendToView(group);
          },
        ),
        GoRoute(
          path: 'media_viewer',
          builder: (context, state) {
            final group = state.extra! as Group;
            return MediaViewerView(group);
          },
        ),
        GoRoute(
          path: 'messages/:groupId',
          builder: (context, state) {
            final groupId = state.pathParameters['groupId']!;
            return ChatMessagesView(groupId);
          },
        ),
      ],
    ),

    GoRoute(
      path: '/profile/contact/:contactId',
      builder: (context, state) {
        final contactId = state.pathParameters['contactId']!;
        return ContactView(int.parse(contactId));
      },
    ),

    GoRoute(
      path: '/profile/group/:groupId',
      builder: (context, state) {
        final groupId = state.pathParameters['groupId']!;
        return GroupView(groupId);
      },
    ),

    GoRoute(
      path: '/group/create/select_member',
      builder: (context, state) {
        return const GroupCreateSelectMembersView();
      },
      routes: [
        GoRoute(
          path: ':groupId',
          builder: (context, state) {
            final groupId = state.pathParameters['groupId'];
            return GroupCreateSelectMembersView(groupId: groupId);
          },
        ),
      ],
    ),

    GoRoute(
      path: Routes.cameraQRScanner,
      builder: (context, state) {
        final extra = state.extra as Map<String, dynamic>?;
        final contact = extra?['contact'] as Contact?;
        final openToVerify = extra?['openToVerify'] as bool? ?? false;
        return QrCodeScannerView(
          contact: contact,
          openToVerify: openToVerify,
        );
      },
    ),

    // settings
    GoRoute(
      path: Routes.settings,
      builder: (context, state) => const SettingsMainView(),
      routes: [
        GoRoute(
          path: 'profile',
          builder: (context, state) => const ProfileView(),
          routes: [
            GoRoute(
              path: 'modify_avatar',
              builder: (context, state) => const ModifyAvatarView(),
            ),
          ],
        ),
        GoRoute(
          path: 'public_profile',
          builder: (context, state) => const PublicProfileView(),
        ),
        GoRoute(
          path: 'account',
          builder: (context, state) => const AccountView(),
        ),
        GoRoute(
          path: 'subscription',
          builder: (context, state) => const SubscriptionView(),
        ),
        GoRoute(
          path: 'backup',
          builder: (context, state) => const BackupView(),
          routes: [
            GoRoute(
              path: 'recovery',
              builder: (context, state) => const BackupRecoveryView(),
            ),
            GoRoute(
              path: 'setup',
              builder: (context, state) => const SetupBackupView(),
            ),
          ],
        ),
        GoRoute(
          path: 'appearance',
          builder: (context, state) => const AppearanceView(),
        ),
        GoRoute(
          path: 'chats',
          builder: (context, state) => const ChatSettingsView(),
          routes: [
            GoRoute(
              path: 'reactions',
              builder: (context, state) => const ChatReactionSelectionView(),
            ),
          ],
        ),
        GoRoute(
          path: 'privacy',
          builder: (context, state) => const PrivacyView(),
          routes: [
            GoRoute(
              path: 'block_users',
              builder: (context, state) => const BlockUsersView(),
            ),
            GoRoute(
              path: 'user_discovery',
              builder: (context, state) => const UserDiscoverySettingsView(),
            ),
          ],
        ),
        GoRoute(
          path: 'notification',
          builder: (context, state) => const NotificationView(),
        ),
        GoRoute(
          path: 'storage_data',
          builder: (context, state) => const DataAndStorageView(),
          routes: [
            GoRoute(
              path: 'manage',
              builder: (context, state) => const ManageStorageView(),
            ),
            GoRoute(
              path: 'import_gallery',
              builder: (context, state) => const ImportFromGalleryView(),
            ),
          ],
        ),
        GoRoute(
          path: 'help',
          builder: (context, state) => const HelpView(),
          routes: [
            GoRoute(
              path: 'faq',
              builder: (context, state) => const FaqView(),
              routes: [
                GoRoute(
                  path: 'verifybadge',
                  builder: (context, state) => VerificationBadeFaqView(
                    contact: state.extra as Contact?,
                  ),
                ),
              ],
            ),
            GoRoute(
              path: 'contact_us',
              builder: (context, state) => const ContactUsView(),
            ),
            GoRoute(
              path: 'news',
              builder: (context, state) => const NewsView(),
            ),
            GoRoute(
              path: 'diagnostics',
              builder: (context, state) => const DiagnosticsView(),
            ),
            GoRoute(
              path: 'user_study',
              builder: (context, state) => UserStudyWelcomeView(
                wasOpenedAutomatic: state.extra as bool? ?? false,
              ),
              routes: [
                GoRoute(
                  path: 'questionnaire',
                  builder: (context, state) =>
                      const UserStudyQuestionnaireView(),
                ),
              ],
            ),
            GoRoute(
              path: 'credits',
              builder: (context, state) => const CreditsView(),
            ),
            GoRoute(
              path: 'changelog',
              builder: (context, state) => ChangeLogView(
                changeLog: state.extra as String?,
              ),
            ),
          ],
        ),
        GoRoute(
          path: 'developer',
          builder: (context, state) => const DeveloperSettingsView(),
          routes: [
            GoRoute(
              path: 'retransmission_database',
              builder: (context, state) => const RetransmissionDataView(),
            ),
            GoRoute(
              path: 'automated_testing',
              builder: (context, state) => const AutomatedTestingView(),
            ),
            GoRoute(
              path: 'informations',
              builder: (context, state) => const DeveloperInformationsView(),
            ),
            GoRoute(
              path: 'reduce_flames',
              builder: (context, state) => const ReduceFlamesView(),
            ),
          ],
        ),
        GoRoute(
          path: 'invite',
          builder: (context, state) => const ShareWithFriendsView(),
        ),
      ],
    ),
    // Fallback instead of showing a Page Not Found error redirect to home
    GoRoute(
      path: '/:path(.*)',
      redirect: (context, state) => '/',
    ),
  ],
);
