import { ReactNode, useCallback, useEffect, useState } from 'react';
import { router } from '@inertiajs/react';
import { I18nextProvider } from 'react-i18next';
import { DirectionProvider as RadixDirectionProvider } from '@radix-ui/react-direction';
import { I18N_LANGUAGES } from '@rutatiina/ui/i18n/config';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

// Import translation files
import enTranslations from '@rutatiina/ui/i18n/messages/en.json';
import arTranslations from '@rutatiina/ui/i18n/messages/ar.json';
import esTranslations from '@rutatiina/ui/i18n/messages/es.json';
import deTranslations from '@rutatiina/ui/i18n/messages/de.json';
import zhTranslations from '@rutatiina/ui/i18n/messages/zh_CN.json';
import { setDisplayLocalization } from '@rutatiina/ui/lib/helpers';
import {
  LARAVEL_NAMESPACE,
  translationResources,
} from '@rutatiina/ui/lib/translations';

type ServerLocalization = { locale?: string; timezone?: string };

/** The language of the page the server sent first (Inertia's initial page). */
function initialServerLocalization(): ServerLocalization | undefined {
  try {
    const page = JSON.parse(
      document.querySelector('script[data-page]')?.textContent ?? '{}',
    ) as { props?: { localization?: ServerLocalization } };

    return page.props?.localization;
  } catch {
    return undefined;
  }
}

interface I18nProviderProps {
  children: ReactNode;
}

function I18nProvider({ children }: I18nProviderProps) {
  const [isI18nInitialized, setIsI18nInitialized] = useState(false);

  useEffect(() => {
    // Initialize i18n only on client side
    const initial = initialServerLocalization();
    setDisplayLocalization(initial?.locale, initial?.timezone);

    if (!i18n.isInitialized) {
      const messages: Record<string, object> = {
        en: enTranslations,
        ar: arTranslations,
        es: esTranslations,
        de: deTranslations,
        zh_CN: zhTranslations,
      };
      // The application's strings (registerTranslations) beside these UI messages.
      const laravel = translationResources();
      const resources = Object.fromEntries(
        I18N_LANGUAGES.map(({ code }) => [
          code,
          {
            translation: messages[code] ?? {},
            [LARAVEL_NAMESPACE]: laravel[code]?.[LARAVEL_NAMESPACE] ?? {},
          },
        ]),
      );

      void i18n
        .use(LanguageDetector)
        .use(initReactI18next)
        .init({
          resources,
          // The server's language wins over what this browser last used.
          lng: initialServerLocalization()?.locale,
          fallbackLng: 'en',
          ns: ['translation', LARAVEL_NAMESPACE],
          debug: process.env.NODE_ENV === 'development',

          interpolation: {
            escapeValue: false, // React already does escaping
          },

          detection: {
            order: ['localStorage', 'navigator', 'htmlTag'],
            caches: ['localStorage'],
            lookupLocalStorage: 'language',
          },

          react: {
            useSuspense: false, // Important for Next.js SSR
          },
        })
        .then(() => {
          setIsI18nInitialized(true);
        });
    } else {
      setIsI18nInitialized(true);
    }

    // Update document direction when language changes
    const handleLanguageChange = (lng: string) => {
      const language = I18N_LANGUAGES.find((lang) => lang.code === lng);
      if (language?.direction) {
        document.documentElement.setAttribute('dir', language.direction);
      }
    };

    // Set initial direction
    if (i18n.language) {
      handleLanguageChange(i18n.language);
    }

    // Listen for language changes
    i18n.on('languageChanged', handleLanguageChange);

    // The server decides the language (the user's choice, else the organization's):
    // every page it renders carries it.
    const stopFollowingServer = router.on('navigate', (event) => {
      const localization = (
        event.detail.page.props as { localization?: ServerLocalization }
      ).localization;
      const locale = localization?.locale;
      setDisplayLocalization(locale, localization?.timezone);

      if (locale && locale !== i18n.language) {
        void i18n.changeLanguage(locale);
      }
    });

    return () => {
      i18n.off('languageChanged', handleLanguageChange);
      stopFollowingServer();
    };
  }, []);

  // Get current language for direction
  const currentLanguage =
    I18N_LANGUAGES.find((lang) => lang.code === (i18n.language || 'en')) ||
    I18N_LANGUAGES[0];

  // Don't render until i18n is initialized
  if (!isI18nInitialized) {
    return (
      <RadixDirectionProvider dir="ltr">{children}</RadixDirectionProvider>
    );
  }

  return (
    <I18nextProvider i18n={i18n}>
      <RadixDirectionProvider dir={currentLanguage.direction}>
        {children}
      </RadixDirectionProvider>
    </I18nextProvider>
  );
}

const useLanguage = () => {
  const [languageCode, setLanguageCode] = useState(i18n.language || 'en');
  const [currentLanguage, setCurrentLanguage] = useState(
    () =>
      I18N_LANGUAGES.find((lang) => lang.code === (i18n.language || 'en')) ||
      I18N_LANGUAGES[0],
  );

  useEffect(() => {
    const handleChange = (lng: string) => {
      setLanguageCode(lng);
      setCurrentLanguage(
        I18N_LANGUAGES.find((lang) => lang.code === lng) || I18N_LANGUAGES[0],
      );
    };

    // sync initial
    handleChange(i18n.language || 'en');

    i18n.on('languageChanged', handleChange);
    return () => {
      i18n.off('languageChanged', handleChange);
    };
  }, []);

  const changeLanguage = useCallback((code: string) => {
    return i18n.changeLanguage(code);
  }, []);

  return {
    languageCode,
    language: currentLanguage,
    changeLanguage,
  };
};

export { I18nProvider, useLanguage };
