"use client";

import { useEffect, useState } from "react";

interface ScoreModalProps {
  score: number; // 0-100
  onClose?: () => void;
  onReplay?: () => void;
}

interface Grade {
  letter: string;
  color: string;
  message: string;
}

function getGrade(score: number): Grade {
  if (score >= 95) {
    return {
      letter: "A+",
      color: "#FFD700", // Gold
      message: "Perfect! You're a Dance Legend! 🌟",
    };
  } else if (score >= 90) {
    return {
      letter: "A",
      color: "#00FFD1", // Cyan
      message: "Amazing! Keep that energy! 💃",
    };
  } else if (score >= 85) {
    return {
      letter: "A-",
      color: "#00FFD1",
      message: "Great job! Almost perfect! ✨",
    };
  } else if (score >= 80) {
    return {
      letter: "B+",
      color: "#4ADE80", // Green
      message: "Good work! You've got moves! 🎵",
    };
  } else if (score >= 75) {
    return {
      letter: "B",
      color: "#4ADE80",
      message: "Nice! Keep practicing! 🔥",
    };
  } else if (score >= 70) {
    return {
      letter: "B-",
      color: "#FBBF24", // Yellow
      message: "Not bad! You're getting there! 💪",
    };
  } else if (score >= 65) {
    return {
      letter: "C+",
      color: "#FBBF24",
      message: "Good effort! Keep going! 🎶",
    };
  } else if (score >= 60) {
    return {
      letter: "C",
      color: "#F97316", // Orange
      message: "Keep practicing! You'll improve! 🌈",
    };
  } else if (score >= 50) {
    return {
      letter: "C-",
      color: "#F97316",
      message: "Try again! Practice makes perfect! 💫",
    };
  } else {
    return {
      letter: "D",
      color: "#EF4444", // Red
      message: "Don't give up! Everyone starts somewhere! 🎯",
    };
  }
}

export default function ScoreModal({ score, onClose, onReplay }: ScoreModalProps) {
  const [animateScore, setAnimateScore] = useState(false);
  const [animateGrade, setAnimateGrade] = useState(false);
  const [animateMessage, setAnimateMessage] = useState(false);
  const [displayScore, setDisplayScore] = useState(0);

  const grade = getGrade(score);

  useEffect(() => {
    // Animate score counting up
    const duration = 1500; // 1.5 seconds
    const steps = 60;
    const increment = score / steps;
    let current = 0;
    let step = 0;

    const timer = setInterval(() => {
      step++;
      current = Math.min(score, current + increment);
      setDisplayScore(Math.round(current));

      if (step >= steps) {
        clearInterval(timer);
        setDisplayScore(score);
        setAnimateScore(true);

        // Show grade after score animation
        setTimeout(() => setAnimateGrade(true), 300);
        // Show message after grade
        setTimeout(() => setAnimateMessage(true), 600);
      }
    }, duration / steps);

    return () => clearInterval(timer);
  }, [score]);

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center">
      {/* Simple backdrop */}
      <div
        className="absolute inset-0 bg-black/80 backdrop-blur-md"
        onClick={onClose}
      />

      {/* Modal Content with Suno Design */}
      <div className="relative z-10 w-full max-w-2xl mx-4">
        <div className="relative rounded-3xl p-12 shadow-2xl border border-white/10 overflow-hidden">
          {/* Suno Background Image */}
          <div
            className="absolute inset-0"
            style={{
              backgroundImage: `url(https://cdn-o.suno.com/Aura-1-Hero-Web.jpg)`,
              backgroundSize: 'cover',
              backgroundPosition: 'center',
              mixBlendMode: "screen",
            }}
          />
          {/* Gradient Overlay */}
          <div
            className="absolute inset-0"
            style={{
              background: `linear-gradient(180deg, rgba(16, 16, 18, 0.00) 0%, #101012 100%)`,
            }}
          />
          {/* Content overlay */}
          <div className="relative z-10">
          {/* Header with Suno Logo */}
          <div className="text-center mb-12">
            <div className="flex items-center justify-center gap-3 mb-4">
              <svg
                className="w-8 h-8"
                viewBox="0 0 24 24"
                fill="white"
                xmlns="http://www.w3.org/2000/svg"
                style={{
                  filter: 'drop-shadow(0 0 8px rgba(255, 255, 255, 0.3))',
                }}
              >
                <path d="m9.35 8.232-2.446.244a.865.865 0 0 0-.78.859v7.558a3 3 0 0 0-.521-.042C4.165 16.851 3 17.78 3 18.926 3 20.07 4.165 21 5.603 21c1.437 0 2.602-.929 2.602-2.074v-6.431l4.77-.475a3 3 0 0 1-.077-.173l-.572-1.395-1.4-.57a2.7 2.7 0 0 1-1.576-1.65M14.452 13.38v2.475a3 3 0 0 0-.521-.041c-1.437 0-2.603.929-2.603 2.074 0 1.146 1.166 2.075 2.603 2.075s2.603-.929 2.603-2.075V13.36a2.86 2.86 0 0 1-2.082.02M10.96 7.56a1.02 1.02 0 0 1 .647-1.004l1.67-.68a1.04 1.04 0 0 0 .57-.567l.682-1.664c.352-.86 1.575-.86 1.927 0l.682 1.664c.106.258.311.463.57.568l1.67.68c.344.14.551.417.621.722.017.407-.199.822-.646 1.004l-1.67.68c-.26.105-.465.31-.57.568l-.682 1.663c-.353.86-1.576.86-1.928 0l-.682-1.663a1.04 1.04 0 0 0-.57-.569l-1.67-.679a1.02 1.02 0 0 1-.62-.723" />
              </svg>
              <span 
                className="text-2xl font-medium text-white"
                style={{
                  letterSpacing: "-0.01em",
                }}
              >
                Dancify
              </span>
            </div>
            <h2
              className="text-5xl font-medium text-white mb-3"
              style={{
                letterSpacing: "-0.02em",
                lineHeight: "0.95",
              }}
            >
              Dance Complete!
            </h2>
            <p 
              className="text-[rgb(156,163,175)] text-lg"
              style={{
                letterSpacing: "-0.01em",
              }}
            >
              Here's how you did
            </p>
          </div>

          {/* Score Display */}
          <div className="text-center mb-12">
            <div
              className={`transition-all duration-500 ${
                animateScore ? "scale-100 opacity-100" : "scale-50 opacity-0"
              }`}
            >
              <div className="relative inline-block">
                {/* Glow effect */}
                <div
                  className="absolute inset-0 blur-3xl opacity-30"
                  style={{
                    background: grade.color,
                  }}
                />
                <div className="relative">
                  <div
                    className="text-9xl font-medium text-white"
                    style={{
                      letterSpacing: "-0.04em",
                      lineHeight: "0.95",
                    }}
                  >
                    {displayScore}
                  </div>
                  <div 
                    className="text-lg text-[rgb(156,163,175)] mt-2 font-normal"
                    style={{
                      letterSpacing: "-0.01em",
                    }}
                  >
                    points
                  </div>
                </div>
              </div>
            </div>
          </div>

          {/* Grade Display */}
          <div className="text-center mb-12">
            <div
              className={`inline-block transition-all duration-700 ${
                animateGrade
                  ? "scale-100 opacity-100 rotate-0"
                  : "scale-0 opacity-0 rotate-180"
              }`}
            >
              <div className="relative inline-block">
                {/* Subtle glow */}
                <div
                  className="absolute inset-0 blur-2xl opacity-20"
                  style={{
                    background: grade.color,
                  }}
                />
                <div
                  className="relative text-8xl font-medium px-12 py-6 rounded-3xl bg-white/5 backdrop-blur-sm border border-white/10"
                  style={{
                    color: "white",
                    letterSpacing: "-0.02em",
                  }}
                >
                  {grade.letter}
                </div>
              </div>
            </div>
          </div>

          {/* Message */}
          <div className="text-center mb-10">
            <p
              className={`text-2xl text-white font-normal transition-all duration-500 ${
                animateMessage
                  ? "translate-y-0 opacity-100"
                  : "translate-y-4 opacity-0"
              }`}
              style={{
                letterSpacing: "-0.01em",
                lineHeight: "1.3",
              }}
            >
              {grade.message}
            </p>
          </div>

          {/* Action Buttons */}
          <div
            className={`flex flex-col sm:flex-row gap-4 justify-center transition-all duration-500 delay-300 ${
              animateMessage ? "translate-y-0 opacity-100" : "translate-y-4 opacity-0"
            }`}
          >
            {onReplay && (
              <button
                onClick={onReplay}
                className="px-8 py-4 rounded-full bg-[rgba(255,255,255,0.1)] hover:bg-[rgba(255,255,255,0.15)] text-white font-medium transition-all border border-white/10"
                style={{
                  letterSpacing: "-0.01em",
                }}
              >
                Try Again
              </button>
            )}
            <button
              onClick={onClose}
              className="px-8 py-4 rounded-full bg-white text-[#101012] hover:bg-[rgba(255,255,255,0.9)] font-medium transition-all"
              style={{
                letterSpacing: "-0.01em",
              }}
            >
              Back to Menu
            </button>
          </div>
          </div>
        </div>
      </div>

      {/* Confetti effect for high scores */}
      {score >= 90 && animateGrade && (
        <div className="absolute inset-0 pointer-events-none overflow-hidden">
          {[...Array(30)].map((_, i) => (
            <div
              key={i}
              className="absolute sparkle-fall"
              style={{
                left: `${Math.random() * 100}%`,
                animationDelay: `${Math.random() * 3}s`,
                animationDuration: `${3 + Math.random() * 2}s`,
                fontSize: `${1 + Math.random() * 1.5}rem`,
                opacity: 0.6 + Math.random() * 0.4,
              }}
            >
              ✨
            </div>
          ))}
        </div>
      )}

      {/* Sparkle falling animation */}
      <style jsx>{`
        @keyframes sparkle-fall {
          0% {
            transform: translateY(-100px) rotate(0deg);
            opacity: 0;
          }
          10% {
            opacity: 1;
          }
          90% {
            opacity: 1;
          }
          100% {
            transform: translateY(100vh) rotate(360deg);
            opacity: 0;
          }
        }

        .sparkle-fall {
          animation: sparkle-fall linear infinite;
        }
      `}</style>
    </div>
  );
}

