import {
  Stack,
  usePathname,
  useRootNavigationState,
  useRouter,
} from "expo-router";
import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client";
import { queryPersister } from "@/lib/queryPersister";
import { useState, useEffect, useRef } from "react";
import "../styles/global.css";
import { SheetProvider } from "react-native-actions-sheet";
import "@/components/ActionSheets/Sheets";
import { useColorScheme } from "nativewind";
import { ThemeProvider } from "expo-router/react-navigation";
import {
  lightTheme,
  darkTheme,
  lightNavigationTheme,
  darkNavigationTheme,
} from "../lib/theme";
import { View } from "react-native";
import { rawTheme, ThemeName } from "@/lib/colors";
import { useShareIntent } from "expo-share-intent";
import useDataStore from "@/store/data";
import useAuthStore from "@/store/auth";
import useReaderStore from "@/store/reader";
import { KeyboardProvider } from "react-native-keyboard-controller";
import { queryClient } from "@/lib/queryClient";
import { StatusBar } from "expo-status-bar";
import OfflineSyncProvider from "@/components/OfflineSyncProvider";
import Analytics from "@/components/Analytics";
import { installCustomHeaders } from "@/lib/customHeaders";

installCustomHeaders();

export default function RootLayout() {
  const [isLoading, setIsLoading] = useState(true);
  const { hasShareIntent, shareIntent, resetShareIntent } = useShareIntent();
  const { updateData, setData, data } = useDataStore();
  const { setReader } = useReaderStore();

  const router = useRouter();
  const pathname = usePathname();

  const { setAuth } = useAuthStore();
  const rootNavState = useRootNavigationState();
  const redirectingToIncoming = useRef(false);

  useEffect(() => {
    setAuth();
    setData();
    setReader();
  }, []);

  useEffect(() => {
    if (!rootNavState?.key || isLoading) return;

    if (hasShareIntent) {
      if (shareIntent.webUrl) {
        updateData({
          shareIntent: {
            hasShareIntent: true,
            url: shareIntent.webUrl,
          },
        });
      }

      resetShareIntent();
    }

    const needsRewrite =
      ((typeof pathname === "string" && pathname.startsWith("/dataUrl=")) ||
        hasShareIntent) &&
      pathname !== "/incoming";

    if (needsRewrite) {
      if (!redirectingToIncoming.current) {
        redirectingToIncoming.current = true;
        router.replace("/incoming");
      }
    } else if (pathname === "/incoming") {
      redirectingToIncoming.current = false;
    }
  }, [
    rootNavState?.key,
    hasShareIntent,
    pathname,
    shareIntent?.webUrl,
    data.shareIntent,
    isLoading,
  ]);

  return (
    <PersistQueryClientProvider
      client={queryClient}
      persistOptions={{
        persister: queryPersister,
        maxAge: Infinity,
        dehydrateOptions: {
          shouldDehydrateMutation: () => true,
          shouldDehydrateQuery: (query) => query.state.data !== undefined,
        },
      }}
      onSuccess={() => {
        setIsLoading(false);
        queryClient.invalidateQueries();
      }}
    >
      <RootComponent isLoading={isLoading} />
    </PersistQueryClientProvider>
  );
}

const RootComponent = ({ isLoading }: { isLoading: boolean }) => {
  const { colorScheme } = useColorScheme();

  return (
    <KeyboardProvider>
      <View
        style={[{ flex: 1 }, colorScheme === "dark" ? darkTheme : lightTheme]}
      >
        <SheetProvider>
          <OfflineSyncProvider />
          <Analytics />
          <StatusBar style={colorScheme === "dark" ? "light" : "dark"} />
          {!isLoading && (
            <ThemeProvider
              value={
                colorScheme === "dark"
                  ? darkNavigationTheme
                  : lightNavigationTheme
              }
            >
              <Stack
                screenOptions={{
                  headerShown: false,
                  contentStyle: {
                    backgroundColor:
                      rawTheme[colorScheme as ThemeName]["base-100"],
                  },
                }}
              >
                {/* <Stack.Screen name="(tabs)" /> */}
                <Stack.Screen
                  name="links/[id]"
                  options={{
                    headerShown: true,
                    headerBackTitle: "Back",
                    headerTitle: "",
                    headerTintColor: colorScheme === "dark" ? "white" : "black",
                    headerStyle: {
                      backgroundColor:
                        colorScheme === "dark"
                          ? rawTheme["dark"]["base-100"]
                          : "white",
                    },
                  }}
                />
                <Stack.Screen name="index" />
                <Stack.Screen name="subscribe" />
                <Stack.Screen name="incoming" />
                <Stack.Screen name="verify-email" />
                <Stack.Screen name="+not-found" />
              </Stack>
            </ThemeProvider>
          )}
        </SheetProvider>
      </View>
    </KeyboardProvider>
  );
};
