import { useState, useEffect } from 'react'
import {
  BrowserRouter as Router,
  Routes,
  Route,
  useNavigate,
  useLocation,
  Navigate,
} from 'react-router-dom'
import LeaderboardPage from './components/LeaderboardPage'
import Examples from './components/Examples'
import Docs from './components/Docs'
import Descriptions from './Descriptions'

const TABS = [
  { label: 'Audio', type: 'audio-leaderboard', path: '/audio-leaderboard' },
  { label: 'Image', type: 'image-leaderboard', path: '/image-leaderboard' },
  { label: 'Video', type: 'video-leaderboard', path: '/video-leaderboard' },
  { label: 'Image Examples', type: 'image-examples', path: '/image-examples' },
  { label: 'Audio Examples', type: 'audio-examples', path: '/audio-examples' },
  { label: 'Video Examples', type: 'video-examples', path: '/video-examples' },
  { label: 'Docs', type: 'docs', path: '/docs' },
]

function TabbedNav({ activeTab }: { activeTab: string }) {
  const navigate = useNavigate()
  return (
    <div className="tabs tabs-border mb-2 ">
      {TABS.map((tab) => (
        <input
          key={tab.type}
          type="radio"
          name="leaderboard_tabs"
          className="tab"
          aria-label={tab.label}
          checked={activeTab === tab.type}
          onChange={() => navigate(tab.path)}
        />
      ))}
    </div>
  )
}

function AppContent() {
  const location = useLocation()
  const [theme, setTheme] = useState<'dark' | 'light'>('dark')
  const [descriptionsLoaded, setDescriptionsLoaded] = useState(false)

  // Load descriptions on app load
  useEffect(() => {
    Descriptions.getInstance().load().then(() => setDescriptionsLoaded(true))
  }, [])

  useEffect(() => {
    document.documentElement.setAttribute('data-theme', theme)
  }, [theme])

  // Find the active tab based on the current path
  const activeTab = TABS.find((tab) => tab.path === location.pathname)?.type || TABS[0].type

  let content = null
  if (activeTab === 'audio-leaderboard') {
    content = <LeaderboardPage datasetType="audio" />
  } else if (activeTab === 'image-leaderboard') {
    content = <LeaderboardPage datasetType="image" />
  } else if (activeTab === 'video-leaderboard') {
    content = <LeaderboardPage datasetType="video" />
  } else if (activeTab === 'image-examples') {
    content = <Examples fileType="image" />
  } else if (activeTab === 'audio-examples') {
    content = <Examples fileType="audio" />
  } else if (activeTab === 'video-examples') {
    content = <Examples fileType="video" />
  } else if (activeTab === 'docs') {
    content = <Docs />
  }

  return (
    <div className="min-h-screen w-11/12 mx-auto">
      <div className="bg-base-100 my-4">
        <div className="flex flex-row justify-between items-center">
          <h2 className="card-title">🥇 Omni Seal Bench Watermarking Leaderboard</h2>
          <div className="flex justify-end items-center gap-2">
            <span className="text-sm">{theme === 'dark' ? '🌙 Dark Mode' : '☀️ Light Mode'}</span>
            <input
              type="checkbox"
              className="toggle"
              checked={theme === 'dark'}
              onChange={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
              aria-label="Toggle dark mode"
            />
          </div>
        </div>
        {descriptionsLoaded && Descriptions.getInstance().getDescription('leaderboard_header') && (
          <div className="mt-2 text-sm text-gray-400">
            {Descriptions.getInstance().getDescription('leaderboard_header')}
            {Descriptions.getInstance().getDescriptionUrl('leaderboard_header') && (
              <a
                href={Descriptions.getInstance().getDescriptionUrl('leaderboard_header')}
                target="_blank"
                rel="noopener noreferrer"
                className="ml-2 text-blue-400 hover:text-blue-300 underline"
              >
                Learn more
              </a>
            )}
          </div>
        )}
      </div>


      <TabbedNav activeTab={activeTab} />

      <div className="w-full ">
        <Routes>
          <Route path="/audio-leaderboard" element={<LeaderboardPage datasetType="audio" />} />
          <Route path="/image-leaderboard" element={<LeaderboardPage datasetType="image" />} />
          <Route path="/video-leaderboard" element={<LeaderboardPage datasetType="video" />} />
          <Route path="/image-examples" element={<Examples fileType="image" />} />
          <Route path="/audio-examples" element={<Examples fileType="audio" />} />
          <Route path="/video-examples" element={<Examples fileType="video" />} />
          <Route path="/docs" element={<Docs />} />
          {/* Redirect root to audio-leaderboard */}
          <Route path="/" element={<Navigate to="/audio-leaderboard" replace />} />
          <Route path="*" element={<Navigate to="/audio-leaderboard" replace />} />
        </Routes>
      </div>
    </div>
  )
}

export default function App() {
  return (
    <Router>
      <AppContent />
    </Router>
  )
}
