"use client"

import { useEffect, useState } from "react"
import { ChatbotUISVG } from "@/components/icons/chatbotui-svg"
import { IconArrowRight } from "@tabler/icons-react"
import { useTheme } from "next-themes"
import Link from "next/link"

import HomeRedirector from "@/components/utility/home-redirector"
import { LanguageSwitcher } from '@/components/ui/language-switcher'

import { useTranslation } from "react-i18next"

export default function HomePage() {
  const { theme } = useTheme()
  const { t, i18n } = useTranslation()

  const [preferredLanguage, setPreferredLanguage] = useState<string>('en') // 默认语言为 'en'

  // 根据 localStorage 或 cookie 设置 preferredLanguage
  useEffect(() => {
    const languageFromStorage = localStorage.getItem('preferred-language') || document.cookie.split('; ').find(row => row.startsWith('preferred-language='))?.split('=')[1];
    if (languageFromStorage) {
      setPreferredLanguage(languageFromStorage);
      // 更新 i18n 的语言设置
      //i18n.changeLanguage(languageFromStorage); // 通过 i18n 更新默认语言      
    }
  }, []);

  return (
    <div className="flex size-full flex-col items-center justify-center relative">

      <HomeRedirector />
      <LanguageSwitcher />

      <div>
        <ChatbotUISVG theme={theme === "dark" ? "dark" : "light"} scale={0.3} />
      </div>

      <div className="mt-2 text-4xl font-bold">{t("Company Name")}</div>

      <Link
        className="mt-4 flex w-[200px] items-center justify-center rounded-md bg-blue-500 p-2 font-semibold"
        href={`/${preferredLanguage}/login`}
      >
        {t("Clock In")}
        <IconArrowRight className="ml-1" size={20} />
      </Link>
    </div>
  )
}
