import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
import { useLocation } from 'react-router-dom';

export interface Achievement {
  id: string;
  name: string;
  description: string;
  icon: string;
  unlocked: boolean;
  unlockedAt?: number;
  secret?: boolean;
}

// Track AI message count in localStorage
const AI_MESSAGE_COUNT_KEY = 'ai-message-count';

export const incrementAiMessageCount = (): number => {
  const current = parseInt(localStorage.getItem(AI_MESSAGE_COUNT_KEY) || '0', 10);
  const newCount = current + 1;
  localStorage.setItem(AI_MESSAGE_COUNT_KEY, newCount.toString());
  return newCount;
};

export const getAiMessageCount = (): number => {
  return parseInt(localStorage.getItem(AI_MESSAGE_COUNT_KEY) || '0', 10);
};

// Game score achievement thresholds
export const GAME_SCORE_ACHIEVEMENTS = {
  tetris: [
    { score: 1000, id: 'tetris_1000' },
    { score: 10000, id: 'tetris_10000' },
    { score: 50000, id: 'tetris_50000' },
  ],
  pacman: [
    { score: 1000, id: 'pacman_1000' },
    { score: 5000, id: 'pacman_5000' },
    { score: 10000, id: 'pacman_10000' },
  ],
  snake: [
    { score: 500, id: 'snake_500' },
    { score: 2000, id: 'snake_2000' },
    { score: 5000, id: 'snake_5000' },
  ],
  breakout: [
    { score: 1000, id: 'breakout_1000' },
    { score: 5000, id: 'breakout_5000' },
    { score: 10000, id: 'breakout_10000' },
  ],
} as const;

export type GameType = keyof typeof GAME_SCORE_ACHIEVEMENTS;

interface AchievementsContextType {
  achievements: Achievement[];
  unlockAchievement: (id: string) => void;
  getUnlockedCount: () => number;
  getTotalCount: () => number;
  timeOnSite: number;
  checkAiAchievements: () => void;
  checkGameScoreAchievements: (game: GameType, score: number) => void;
  unlockMusicListener: () => void;
  unlockMaxScore: () => void;
}

// Track time spent on each page
const PAGE_TIME_KEY = 'page-time-tracking';

const getPageTimeTracking = (): Record<string, number> => {
  const saved = localStorage.getItem(PAGE_TIME_KEY);
  return saved ? JSON.parse(saved) : {};
};

const updatePageTime = (path: string, seconds: number): void => {
  const tracking = getPageTimeTracking();
  tracking[path] = (tracking[path] || 0) + seconds;
  localStorage.setItem(PAGE_TIME_KEY, JSON.stringify(tracking));
};

export const getPageTime = (path: string): number => {
  const tracking = getPageTimeTracking();
  return tracking[path] || 0;
};

const defaultAchievements: Achievement[] = [
  // Discovery achievements
  { id: 'first_visit', name: 'Hello World', description: 'Visit the site for the first time', icon: '👋', unlocked: false },
  { id: 'found_achievements', name: 'Achievement Hunter', description: 'Discover the achievements section', icon: '🏆', unlocked: false },
  { id: 'konami_master', name: 'Konami Master', description: 'Enter the legendary cheat code', icon: '🎮', unlocked: false, secret: true },
  { id: 'terminal_user', name: 'Terminal Jockey', description: 'Use the terminal command interface', icon: '💻', unlocked: false },
  { id: 'hint_seeker', name: 'Hint Seeker', description: 'Ask for a hint in the terminal', icon: '🔍', unlocked: false, secret: true },
  
  // Navigation achievements - now require 1 minute on page
  { id: 'home_visitor', name: 'Home Base', description: 'Spend 1 minute on the home page', icon: '🏠', unlocked: false },
  { id: 'about_visitor', name: 'Getting Personal', description: 'Spend 1 minute learning about the owner', icon: '👤', unlocked: false },
  { id: 'projects_visitor', name: 'Project Explorer', description: 'Spend 1 minute exploring projects', icon: '📁', unlocked: false },
  { id: 'resources_visitor', name: 'Resource Collector', description: 'Spend 1 minute browsing resources', icon: '📚', unlocked: false },
  { id: 'links_visitor', name: 'Link Crawler', description: 'Spend 1 minute on the links page', icon: '🔗', unlocked: false },
  { id: 'faq_visitor', name: 'Question Everything', description: 'Spend 1 minute reading the FAQ', icon: '❓', unlocked: false },
  { id: 'music_visitor', name: 'DJ Mode', description: 'Spend 1 minute in the music player', icon: '🎵', unlocked: false },
  { id: 'ai_visitor', name: 'AI Whisperer', description: 'Spend 1 minute chatting with AI', icon: '🤖', unlocked: false },
  { id: 'arcade_visitor', name: 'Arcade Enthusiast', description: 'Spend 1 minute in the arcade', icon: '🕹️', unlocked: false },
  { id: 'all_pages', name: 'Completionist', description: 'Spend 1 minute on every page', icon: '🗺️', unlocked: false, secret: true },
  
  // Time achievements
  { id: 'time_15min', name: 'Quick Visit', description: 'Spend 15 minutes on the site', icon: '⏱️', unlocked: false },
  { id: 'time_30min', name: 'Taking Your Time', description: 'Spend 30 minutes on the site', icon: '⏰', unlocked: false },
  { id: 'time_1hour', name: 'Deep Dive', description: 'Spend 1 hour on the site', icon: '🕐', unlocked: false },
  { id: 'time_3hour', name: 'Marathon Session', description: 'Spend 3 hour on the site', icon: '🕑', unlocked: false },
  { id: 'time_24hour', name: 'No Life', description: 'Spend 24 hours on the site', icon: '💀', unlocked: false, secret: true },
  
  // Game achievements
  { id: 'tetris_played', name: 'Block Stacker', description: 'Play Tetris', icon: '🧱', unlocked: false },
  { id: 'pacman_played', name: 'Pac-Fan', description: 'Play Pac-Man', icon: '🟡', unlocked: false },
  { id: 'snake_played', name: 'Sssssnake', description: 'Play Snake', icon: '🐍', unlocked: false },
  { id: 'breakout_played', name: 'Brick Breaker', description: 'Play Breakout', icon: '🧨', unlocked: false },
  { id: 'all_games', name: 'Arcade Master', description: 'Play all four arcade games', icon: '👾', unlocked: false },
  
  // Score achievements
  { id: 'tetris_1000', name: 'Tetris Novice', description: 'Score 1,000 points in Tetris', icon: '🥉', unlocked: false },
  { id: 'tetris_10000', name: 'Tetris Pro', description: 'Score 10,000 points in Tetris', icon: '🥈', unlocked: false },
  { id: 'tetris_50000', name: 'Tetris Legend', description: 'Score 50,000 points in Tetris', icon: '🥇', unlocked: false, secret: true },
  { id: 'pacman_1000', name: 'Pac-Rookie', description: 'Score 1,000 points in Pac-Man', icon: '🥉', unlocked: false },
  { id: 'pacman_5000', name: 'Pac-Veteran', description: 'Score 5,000 points in Pac-Man', icon: '🥈', unlocked: false },
  { id: 'pacman_10000', name: 'Pac-Champion', description: 'Score 10,000 points in Pac-Man', icon: '🥇', unlocked: false, secret: true },
  { id: 'snake_500', name: 'Baby Snake', description: 'Score 500 points in Snake', icon: '🥉', unlocked: false },
  { id: 'snake_2000', name: 'Growing Snake', description: 'Score 2,000 points in Snake', icon: '🥈', unlocked: false },
  { id: 'snake_5000', name: 'Mega Snake', description: 'Score 5,000 points in Snake', icon: '🥇', unlocked: false, secret: true },
  { id: 'breakout_1000', name: 'Brick Novice', description: 'Score 1,000 points in Breakout', icon: '🥉', unlocked: false },
  { id: 'breakout_5000', name: 'Brick Crusher', description: 'Score 5,000 points in Breakout', icon: '🥈', unlocked: false },
  { id: 'breakout_10000', name: 'Brick Destroyer', description: 'Score 10,000 points in Breakout', icon: '🥇', unlocked: false, secret: true },
  
  // Special achievements
  { id: 'night_owl', name: 'Night Owl', description: 'Visit the site between midnight and 4 AM', icon: '🦉', unlocked: false, secret: true },
  { id: 'early_bird', name: 'Early Bird', description: 'Visit the site between 5 AM and 7 AM', icon: '🐦', unlocked: false, secret: true },
  { id: 'theme_switcher', name: 'Indecisive', description: 'Switch between red and green themes', icon: '🎨', unlocked: false },
  { id: 'crt_toggler', name: 'Retro Purist', description: 'Toggle the CRT effect', icon: '📺', unlocked: false },
  { id: 'sound_toggler', name: 'Audio Engineer', description: 'Toggle sound effects', icon: '🔊', unlocked: false },
  { id: 'music_listener', name: 'Radio Listener', description: 'Listen to a radio station', icon: '📻', unlocked: false },
  { id: 'ai_conversation', name: 'Deep Thinker', description: 'Send 5 messages to the AI', icon: '💭', unlocked: false },
  { id: 'ai_long_chat', name: 'Chatterbox', description: 'Send 20 messages to the AI', icon: '🗣️', unlocked: false, secret: true },
  { id: 'project_detail', name: 'Project Inspector', description: 'View a project in detail', icon: '🔬', unlocked: false },
  { id: 'leaderboard_check', name: 'Competitive Spirit', description: 'Check the arcade leaderboard', icon: '📊', unlocked: false },
  { id: 'max_score', name: 'Integer Overflow', description: 'Reach the maximum score in any game', icon: '💥', unlocked: false, secret: true },
  { id: 'verified_human', name: 'Certified Human', description: 'Pass the human verification', icon: '✅', unlocked: false },
];

const AchievementsContext = createContext<AchievementsContextType | undefined>(undefined);

export const AchievementsProvider = ({ children }: { children: ReactNode }) => {
  const [achievements, setAchievements] = useState<Achievement[]>(() => {
    const saved = localStorage.getItem('achievements');
    if (saved) {
      const parsed = JSON.parse(saved);
      // Merge with defaults to handle new achievements
      return defaultAchievements.map(def => {
        const saved = parsed.find((a: Achievement) => a.id === def.id);
        return saved ? { ...def, unlocked: saved.unlocked, unlockedAt: saved.unlockedAt } : def;
      });
    }
    return defaultAchievements;
  });

  const [timeOnSite, setTimeOnSite] = useState(() => {
    const saved = localStorage.getItem('timeOnSite');
    return saved ? parseInt(saved, 10) : 0;
  });

  const [visitedPages, setVisitedPages] = useState<Set<string>>(() => {
    const saved = localStorage.getItem('visitedPages');
    return saved ? new Set(JSON.parse(saved)) : new Set();
  });

  const location = useLocation();

  // Track time on site
  useEffect(() => {
    const interval = setInterval(() => {
      setTimeOnSite(prev => {
        const newTime = prev + 1;
        localStorage.setItem('timeOnSite', newTime.toString());
        return newTime;
      });
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  // Check time-based achievements
  useEffect(() => {
    if (timeOnSite >= 900) unlockAchievement('time_15min');
    if (timeOnSite >= 1800) unlockAchievement('time_30min');
    if (timeOnSite >= 3600) unlockAchievement('time_1hour');
    if (timeOnSite >= 10800) unlockAchievement('time_3hour');
    if (timeOnSite >= 86400) unlockAchievement('time_24hour');
  }, [timeOnSite]);

  // Check time of day achievements
  useEffect(() => {
    const hour = new Date().getHours();
    if (hour >= 0 && hour < 4) unlockAchievement('night_owl');
    if (hour >= 5 && hour < 7) unlockAchievement('early_bird');
  }, []);

  // Track page visits and time spent
  const [currentPath, setCurrentPath] = useState(location.pathname);
  
  useEffect(() => {
    const path = location.pathname;
    setCurrentPath(path);
    
    setVisitedPages(prev => {
      const newSet = new Set(prev);
      newSet.add(path);
      localStorage.setItem('visitedPages', JSON.stringify([...newSet]));
      return newSet;
    });

    // Track time on page - increment every second
    const interval = setInterval(() => {
      updatePageTime(path, 1);
      const pageTime = getPageTime(path);
      
      // Unlock achievements after 60 seconds (1 minute) on page
      if (pageTime >= 60) {
        if (path === '/') unlockAchievement('home_visitor');
        if (path === '/about') unlockAchievement('about_visitor');
        if (path === '/projects') unlockAchievement('projects_visitor');
        if (path === '/resources') unlockAchievement('resources_visitor');
        if (path === '/links') unlockAchievement('links_visitor');
        if (path === '/faq') unlockAchievement('faq_visitor');
        if (path === '/music') unlockAchievement('music_visitor');
        if (path === '/ai') unlockAchievement('ai_visitor');
        if (path === '/games') unlockAchievement('arcade_visitor');
      }
    }, 1000);

    // Immediate unlocks for games and detail pages
    if (path.startsWith('/projects/')) unlockAchievement('project_detail');
    if (path === '/games/leaderboard') unlockAchievement('leaderboard_check');
    if (path === '/games/tetris') unlockAchievement('tetris_played');
    if (path === '/games/pacman') unlockAchievement('pacman_played');
    if (path === '/games/snake') unlockAchievement('snake_played');
    if (path === '/games/breakout') unlockAchievement('breakout_played');

    return () => clearInterval(interval);
  }, [location.pathname]);

  // Check all pages visited (1 min on each)
  useEffect(() => {
    const requiredPages = ['/', '/about', '/projects', '/resources', '/links', '/faq', '/music', '/ai', '/games'];
    const allVisitedLongEnough = requiredPages.every(p => getPageTime(p) >= 60);
    if (allVisitedLongEnough) unlockAchievement('all_pages');
  }, [visitedPages, timeOnSite]); // Check periodically with timeOnSite

  // Check all games played
  useEffect(() => {
    const gamePages = ['/games/tetris', '/games/pacman', '/games/snake', '/games/breakout'];
    const allPlayed = gamePages.every(p => visitedPages.has(p));
    if (allPlayed) unlockAchievement('all_games');
  }, [visitedPages]);

  // First visit achievement
  useEffect(() => {
    unlockAchievement('first_visit');
  }, []);

  // Check for music listener achievement (set by MusicContext)
  useEffect(() => {
    const checkMusicListener = () => {
      if (localStorage.getItem('has-listened-to-music') === 'true') {
        unlockAchievement('music_listener');
      }
    };
    checkMusicListener();
    // Also check periodically in case user starts playing music
    const interval = setInterval(checkMusicListener, 2000);
    return () => clearInterval(interval);
  }, []);

  const [notification, setNotification] = useState<Achievement | null>(null);

  const unlockAchievement = useCallback((id: string) => {
    setAchievements(prev => {
      const achievement = prev.find(a => a.id === id);
      if (!achievement || achievement.unlocked) return prev;
      
      // Show notification
      setNotification(achievement);
      setTimeout(() => setNotification(null), 4000);
      
      const updated = prev.map(a => 
        a.id === id ? { ...a, unlocked: true, unlockedAt: Date.now() } : a
      );
      localStorage.setItem('achievements', JSON.stringify(updated));
      return updated;
    });
  }, []);

  const getUnlockedCount = useCallback(() => {
    return achievements.filter(a => a.unlocked).length;
  }, [achievements]);

  const getTotalCount = useCallback(() => {
    return achievements.length;
  }, [achievements]);

  // Check AI message achievements
  const checkAiAchievements = useCallback(() => {
    const count = getAiMessageCount();
    if (count >= 5) unlockAchievement('ai_conversation');
    if (count >= 20) unlockAchievement('ai_long_chat');
  }, [unlockAchievement]);

  // Check game score achievements
  const checkGameScoreAchievements = useCallback((game: GameType, score: number) => {
    const thresholds = GAME_SCORE_ACHIEVEMENTS[game];
    for (const threshold of thresholds) {
      if (score >= threshold.score) {
        unlockAchievement(threshold.id);
      }
    }
  }, [unlockAchievement]);

  // Unlock music listener achievement
  const unlockMusicListener = useCallback(() => {
    unlockAchievement('music_listener');
  }, [unlockAchievement]);

  // Unlock max score achievement
  const unlockMaxScore = useCallback(() => {
    unlockAchievement('max_score');
  }, [unlockAchievement]);

  // Save achievements
  useEffect(() => {
    localStorage.setItem('achievements', JSON.stringify(achievements));
  }, [achievements]);

  return (
    <AchievementsContext.Provider
      value={{
        achievements,
        unlockAchievement,
        getUnlockedCount,
        getTotalCount,
        timeOnSite,
        checkAiAchievements,
        checkGameScoreAchievements,
        unlockMusicListener,
        unlockMaxScore,
      }}
    >
      {children}
      {/* Achievement Notification */}
      {notification && (
        <div className="fixed top-4 right-4 z-[300] animate-in slide-in-from-right-5 fade-in duration-300">
          <div className="bg-background border-2 border-primary p-4 box-glow-strong max-w-xs">
            <div className="flex items-center gap-3">
              <span className="text-2xl">{notification.icon}</span>
              <div>
                <div className="text-primary font-minecraft text-sm text-glow">ACHIEVEMENT UNLOCKED!</div>
                <div className="text-primary font-bold">{notification.name}</div>
                <div className="text-primary/70 text-xs">{notification.description}</div>
              </div>
            </div>
          </div>
        </div>
      )}
    </AchievementsContext.Provider>
  );
};

export const useAchievements = () => {
  const context = useContext(AchievementsContext);
  if (context === undefined) {
    throw new Error('useAchievements must be used within an AchievementsProvider');
  }
  return context;
};
