import { useState, useEffect, lazy, Suspense } from 'react';
import { useLocation } from 'react-router-dom';
import MatrixRain from '@/components/MatrixRain';
import LoadingScreen from '@/components/LoadingScreen';
import MainLayout from '@/components/MainLayout';
import MatrixCursor from '@/components/MatrixCursor';
import { VERIFIED_KEY, BYPASS_KEY } from '@/components/HumanVerification';
import { useSettings } from '@/contexts/SettingsContext';
import { useAchievements } from '@/contexts/AchievementsContext';
import { useKonamiCode } from '@/hooks/useKonamiCode';
import { toast } from '@/hooks/use-toast';
import { AudioBlockedOverlay } from '@/components/AudioBlockedOverlay';

// Lazy load conditionally rendered components to reduce initial bundle
const HumanVerification = lazy(() => import('@/components/HumanVerification'));
const MusicPlayer = lazy(() => import('@/components/MusicPlayer'));
const SettingsPanel = lazy(() => import('@/components/SettingsPanel'));
const CryptoConsentModal = lazy(() => import('@/components/CryptoConsentModal'));
const TerminalCommand = lazy(() => import('@/components/TerminalCommand'));

const Index = () => {
  const [isVerified, setIsVerified] = useState(() => {
    // Check localStorage or bypass URL
    if (localStorage.getItem(VERIFIED_KEY) === 'true') return true;
    const params = new URLSearchParams(window.location.search);
    if (params.has(BYPASS_KEY) || window.location.pathname.includes(BYPASS_KEY)) return true;
    return false;
  });
  const [isLoading, setIsLoading] = useState(true);
  const [isRedTheme, setIsRedTheme] = useState(() => {
    const saved = localStorage.getItem('themeColor');
    // Default to red theme unless explicitly set to green
    return saved !== 'green';
  });
  const [showConsentModal, setShowConsentModal] = useState(false);
  const [konamiActive, setKonamiActive] = useState(false);
  
  const { 
    crtEnabled, 
    playSound, 
    cryptoConsent, 
    showAudioOverlay, 
    enableAudio, 
    disableAudio,
    soundEnabled,
    setSoundEnabled 
  } = useSettings();
  const { unlockAchievement } = useAchievements();
  const { activated: konamiActivated, reset: resetKonami } = useKonamiCode();
  const location = useLocation();
  
  // Hide mini music player when on the full music page (to avoid UI duplication)
  // but audio continues playing via MusicContext
  const showMiniPlayer = location.pathname !== '/music';

  // Handle Konami code activation
  useEffect(() => {
    if (konamiActivated) {
      setKonamiActive(true);
      
      // Unlock achievement
      unlockAchievement('konami_master');
      
      // Play special sound sequence
      playSound('success');
      setTimeout(() => playSound('boot'), 200);
      setTimeout(() => playSound('success'), 400);
      
      // Show secret toast
      toast({
        title: "🎮 KONAMI CODE ACTIVATED",
        description: "You found the secret! You are a true hacker.",
      });
      
      // Reset after animation
      setTimeout(() => {
        setKonamiActive(false);
        resetKonami();
      }, 3000);
    }
  }, [konamiActivated, playSound, resetKonami, unlockAchievement]);

  // Persist theme to localStorage
  useEffect(() => {
    localStorage.setItem('themeColor', isRedTheme ? 'red' : 'green');
  }, [isRedTheme]);

  useEffect(() => {
    const timer = setTimeout(() => {
      setIsLoading(false);
      // Show consent modal after loading if user hasn't accepted yet (null = needs prompt)
      if (cryptoConsent === null) {
        setShowConsentModal(true);
      }
    }, 3000); // Extended to 3s for boot sequence

    return () => clearTimeout(timer);
  }, [cryptoConsent]);

  useEffect(() => {
    if (isRedTheme) {
      document.documentElement.classList.add('red-theme');
      document.body.classList.add('red-theme');
    } else {
      document.documentElement.classList.remove('red-theme');
      document.body.classList.remove('red-theme');
    }
  }, [isRedTheme]);

  const toggleTheme = () => {
    setIsRedTheme(!isRedTheme);
    playSound('click');
    unlockAchievement('theme_switcher');
    // Notify other components of theme change
    window.dispatchEvent(new CustomEvent('themeChange', { detail: { isRedTheme: !isRedTheme } }));
  };

  const handleConsentClose = () => {
    setShowConsentModal(false);
  };

  const handleVerified = () => {
    setIsVerified(true);
    unlockAchievement('verified_human');
  };

  // Show verification gate if not verified
  if (!isVerified) {
    return (
      <div className={`min-h-screen overflow-x-hidden ${crtEnabled ? 'crt' : ''}`}>
        <MatrixRain color={isRedTheme ? '#FF0000' : '#00FF00'} />
        <Suspense fallback={null}>
          <HumanVerification onVerified={handleVerified} />
        </Suspense>
      </div>
    );
  }

  return (
    <div className={`min-h-screen overflow-x-hidden ${crtEnabled ? 'crt' : ''} ${konamiActive ? 'konami-active' : ''}`}>
      <MatrixCursor />
      <MatrixRain color={isRedTheme ? '#FF0000' : '#00FF00'} />
      <LoadingScreen isLoading={isLoading} />
      
      {/* Audio blocked overlay */}
      {showAudioOverlay && soundEnabled && (
        <AudioBlockedOverlay 
          onEnableAudio={enableAudio} 
          onDisableAudio={disableAudio} 
        />
      )}
      
      {/* Moving scanline - only visible when CRT is enabled */}
      <div className="moving-scanline" />
      
      {!isLoading && (
        <Suspense fallback={null}>
          <CryptoConsentModal isOpen={showConsentModal} onClose={handleConsentClose} />
          <SettingsPanel onToggleTheme={toggleTheme} isRedTheme={isRedTheme} />
          <TerminalCommand />
          
          <div className="relative z-10 flex flex-col items-center min-h-screen pb-16">
            <MainLayout />
          </div>
          {showMiniPlayer && <MusicPlayer />}
        </Suspense>
      )}
    </div>
  );
};

export default Index;
