import { useState, useEffect, useRef } from "react";
import { usePlayer } from "../providers/PlayerProvider";
import styles from "../styles/Army.module.css";

// Quote categories based on total minted GELD
const PROGRESSION_TIERS = {
  BEGINNER: BigInt(1000000),
  NOVICE: BigInt(10000000),
  INTERMEDIATE: BigInt(100000000),
  EXPERIENCED: BigInt(1000000000),
  EXPERT: BigInt(10000000000),
  MASTER: BigInt(100000000000),
  LEGENDARY: BigInt(1000000000000),
};

const quotes = {
  BEGINNER: [
    "Well, well... another fresh face looking to be a hero. At least you're holding your weapon the right way up.",
    "Ha! Your armor's so shiny I can see my reflection. Give it a week, rookie.",
    "Another raider? The rats in the cellar are trembling... with laughter.",
    "Don't worry, we keep the bandages cheap for newcomers like yourself.",
    "The practice dummy out back is taking bets against you. Just thought you should know.",
  ],
  NOVICE: [
    "Back again? You're lasting longer than most of the fresh blood.",
    "You're developing some calluses finally. Good, you'll need them.",
    "Starting to walk with a bit of confidence, eh? Don't let it go to your head.",
    "The local wolves don't laugh quite as hard when they see you coming now.",
    "The raiders have stopped taking bets on when you'll quit. Progress!",
  ],
  INTERMEDIATE: [
    "Now there's a familiar face that's earned their drink!",
    "The usual? You've been around long enough to have a usual!",
    "Looking more battle-worn these days. It suits you.",
    "Your reputation's growing. Heard some kids playing 'mighty raider' in the street.",
    "Those monsters in the forest? They're starting to learn your name.",
  ],
  EXPERIENCED: [
    "Honor to serve you, friend. Your exploits bring customers to my humble establishment.",
    "The bards are starting to write songs about you. Decent ones, too!",
    "Ah, the hero of the hour! Your usual table awaits.",
    "Your presence honors us. What tales will you bring today?",
    "They say you've met Moloch face to face. Now that's a tale worth hearing!",
  ],
  EXPERT: [
    "My most esteemed patron! The usual bottle of our finest?",
    "Blessed are we to host a hero of your caliber!",
    "The stuff of legends walks among us, friends!",
    "They say you've slain dragons now. Dragons! In my tavern!",
    "Even the ancient raiders speak of your deeds with reverence.",
  ],
  MASTER: [
    "You humble us with your magnificent presence!",
    "Champions and heroes seek you out for guidance now. How far you've come!",
    "They say your name in whispers in the dark kingdoms of Moloch. In fear.",
    "To think, I've watched you grow from newcomer to living legend!",
    "The ancient prophecies speak of one such as you...",
  ],
  LEGENDARY: [
    "The greatest raider of our age graces us! All hail!",
    "Let it be known - a living legend walks among us!",
    "Champion of champions! Slayer of the Moloch!",
    "The Moloch speaks your name in terror!",
    "Even the gods watch your journey with interest!",
  ],
};

const EARLY_GAME_QUOTES = [
  "There is always Moloch to be slain here...",
  "We prioritize Shipping at All Costs!",
  "Get out there RAIDER, Moloch won't Slay Himself!",
];

function PixelatedQuote() {
  const { player } = usePlayer();
  const [isShown, setIsShown] = useState(true);
  const [currentQuote, setCurrentQuote] = useState("Welcome to the Dark Forest!");
  const intervalIdRef = useRef<NodeJS.Timeout | null>(null);
  const hasShownWelcome = useRef(false);

  // Determine which tier of quotes to use based on total minted
  const getQuoteTier = (totalMinted: bigint) => {
    if (totalMinted >= PROGRESSION_TIERS.LEGENDARY) return "LEGENDARY";
    if (totalMinted >= PROGRESSION_TIERS.MASTER) return "MASTER";
    if (totalMinted >= PROGRESSION_TIERS.EXPERT) return "EXPERT";
    if (totalMinted >= PROGRESSION_TIERS.EXPERIENCED) return "EXPERIENCED";
    if (totalMinted >= PROGRESSION_TIERS.INTERMEDIATE) return "INTERMEDIATE";
    if (totalMinted >= PROGRESSION_TIERS.NOVICE) return "NOVICE";
    return "BEGINNER";
  };

  useEffect(() => {
    if (intervalIdRef.current !== null) {
      clearInterval(intervalIdRef.current);
    }

    intervalIdRef.current = setInterval(() => {
      const totalMinted = player?.total_minted ?? BigInt(0);
      
      // Show welcome message only once at the start
      if (!hasShownWelcome.current) {
        setCurrentQuote("Welcome to the Dark Forest!");
        hasShownWelcome.current = true;
      } else if (totalMinted < PROGRESSION_TIERS.BEGINNER) {
        // Show early game quotes until player reaches beginner level
        setCurrentQuote(
          EARLY_GAME_QUOTES[Math.floor(Math.random() * EARLY_GAME_QUOTES.length)]
        );
      } else {
        const tier = getQuoteTier(totalMinted);
        const tierQuotes = quotes[tier];
        setCurrentQuote(
          tierQuotes[Math.floor(Math.random() * tierQuotes.length)]
        );
      }
      setIsShown(true);

      setTimeout(() => {
        setIsShown(false);
      }, 4000);
    }, 6000);

    return () => {
      if (intervalIdRef.current !== null) {
        clearInterval(intervalIdRef.current);
        intervalIdRef.current = null;
      }
    };
  }, [player?.total_minted]);

  return (
    <div>
      <div
        className={`pixel-borders pixel-borders--2 pixelFont ${styles.pixelQuote}`}
        style={{ opacity: isShown ? 1 : 0 }}
      >
        {currentQuote}
      </div>
    </div>
  );
}

export default PixelatedQuote;
