import { useState, useEffect } from "react";
import { resolveAdvisoryPlayer, resolveBombSite } from "../utilities/advisoryUtils";

/**
 * Advisories component - displays priority alerts for game events
 * Priority order: Defusing > Planting > Sole Survivor
 */
const Advisories = ({ playerArray, bombData, roundInfo, settings }) => {
  const [activeAdvisory, setActiveAdvisory] = useState(null);

  useEffect(() => {
    if (!playerArray || playerArray.length === 0) {
      setActiveAdvisory(null);
      return;
    }

    // Priority 1: Bomb Defusing
    if (bombData?.m_is_defusing === true || bombData?.state === "defusing") {
      setActiveAdvisory({
        type: "defusing",
        text: "đang gỡ bom",
        icon: "💣",
        color: "var(--ct-color)",
      });
      return;
    }

    // Priority 2: Bomb Planting
    if (bombData?.state === "planting" ||
        (roundInfo?.m_bomb_planted === true && (bombData?.m_blow_time ?? 0) > 0)) {
      // Check if planting is complete (no active planting state but bomb is planted)
      const isPlantingActive = bombData?.state === "planting";
      const isPlanted = roundInfo?.m_bomb_planted === true && (bombData?.m_blow_time ?? 0) > 0;

      if (isPlanted && !isPlantingActive) {
        // Bomb planted - show completion message
        const siteLabel = resolveBombSite(bombData);
        let advisoryText = `bom đã đặt${siteLabel ? ` ${siteLabel}` : ''}`;
        setActiveAdvisory({
          type: "planting",
          text: advisoryText,
          icon: "💣",
          color: "var(--t-color)",
        });
        return;
      }

      // Planting in progress
      const advisoryPlayer = resolveAdvisoryPlayer('planting', playerArray, bombData);

      let advisoryText = `đang đặt bom${advisoryPlayer ? ` ${advisoryPlayer}` : ''}`;

      setActiveAdvisory({
        type: "planting",
        text: advisoryText,
        icon: "💣",
        color: "var(--t-color)",
      });
      return;
    }

    // Priority 3: Sole Survivor - DISABLED per user request
    // const tAlive = playerArray.filter(p => p.m_team === 2 && !p.m_is_dead);
    // const ctAlive = playerArray.filter(p => p.m_team === 3 && !p.m_is_dead);

    // if (tAlive.length === 1 && tAlive.length + ctAlive.length > 1) {
    //   const survivor = tAlive[0];
    //   setActiveAdvisory({
    //     type: "sole-survivor-t",
    //     text: `${survivor.m_name || "TERRORIST"} - SOLE SURVIVOR`,
    //     icon: "⚠️",
    //     color: "var(--t-color)",
    //   });
    //   return;
    // }

    // if (ctAlive.length === 1 && tAlive.length + ctAlive.length > 1) {
    //   const survivor = ctAlive[0];
    //   setActiveAdvisory({
    //     type: "sole-survivor-ct",
    //     text: `${survivor.m_name || "CT"} - SOLE SURVIVOR`,
    //     icon: "⚠️",
    //     color: "var(--ct-color)",
    //   });
    //   return;
    // }

    // No active advisory
    setActiveAdvisory(null);
  }, [playerArray, bombData, roundInfo]);

  if (!activeAdvisory) return null;

  const opacity = settings?.advisoriesOpacity ?? 1.0;

  return (
    <div
      style={{
        position: "absolute",
        top: 16,
        left: "50%",
        transform: "translateX(-50%)",
        zIndex: 200,
        display: "flex",
        alignItems: "center",
        gap: 8,
        padding: "10px 20px",
        background: `rgba(0, 0, 0, ${0.85 * opacity})`,
        border: `2px solid ${activeAdvisory.color}`,
        borderRadius: 8,
        boxShadow: `0 0 20px ${activeAdvisory.color}40`,
        backdropFilter: "blur(8px)",
        animation: "fadeIn 0.3s ease-in-out",
        pointerEvents: "none",
        opacity: opacity,
      }}
    >
      <span style={{ fontSize: 20 }}>{activeAdvisory.icon}</span>
      <span
        style={{
          fontSize: 13,
          fontWeight: 800,
          letterSpacing: "0.1em",
          color: activeAdvisory.color,
          textTransform: "uppercase",
          textShadow: `0 0 10px ${activeAdvisory.color}80`,
        }}
      >
        {activeAdvisory.text}
      </span>
    </div>
  );
};

export default Advisories;
