/* global React, window */
const { useEffect, useRef, useState } = React;

function MusicToggle() {
  const audioRef = useRef(null);
  const [on, setOn] = useState(true);
  const [unlocked, setUnlocked] = useState(false);

  const playAudio = () => {
    const audio = audioRef.current;
    if (!audio) return;
    audio.volume = 0.75;
    const p = audio.play();
    if (p && p.then) {
      p.then(() => setUnlocked(true)).catch(() => setUnlocked(false));
    } else {
      setUnlocked(true);
    }
  };

  useEffect(() => {
    const video = document.getElementById("hero-video");
    if (video) {
      video.muted = true;
      video.volume = 0;
    }
  }, []);

  useEffect(() => {
    const audio = audioRef.current;
    if (!audio) return;
    if (!on) {
      audio.pause();
    }
  }, [on]);

  useEffect(() => {
    const unlock = () => {
      if (on) playAudio();
      window.removeEventListener("pointerdown", unlock);
      window.removeEventListener("keydown", unlock);
    };
    window.addEventListener("pointerdown", unlock, { passive: true });
    window.addEventListener("keydown", unlock);
    return () => {
      window.removeEventListener("pointerdown", unlock);
      window.removeEventListener("keydown", unlock);
    };
  }, [on]);

  const toggle = () => {
    setOn((current) => {
      const next = !current;
      if (!next && audioRef.current) audioRef.current.pause();
      return next;
    });
  };

  return (
    <>
      <audio
        ref={audioRef}
        src="assets/hero.mp4"
        loop
        preload="none"
      />
      <button
        type="button"
        onClick={toggle}
        title={on ? "关闭背景音乐" : "打开背景音乐"}
        aria-label={on ? "Mute background music" : "Unmute background music"}
        className="fixed top-12 right-4 md:top-4 md:right-5 z-[90] group flex h-11 items-center gap-2 px-3 md:px-3.5 rounded-full border border-white/15 bg-black/40 backdrop-blur-xl hover:bg-black/60 hover:border-white/30 transition-all duration-300"
        style={{ boxShadow: "0 6px 24px rgba(0,0,0,0.35)" }}
      >
        {on && unlocked ? (
          <span className="relative w-5 h-5 flex items-end justify-center gap-[2px]">
            <span className="bar w-[2.5px] bg-white rounded-full" style={{ animation: "eqA 0.9s ease-in-out infinite" }}></span>
            <span className="bar w-[2.5px] bg-white rounded-full" style={{ animation: "eqB 0.7s ease-in-out infinite" }}></span>
            <span className="bar w-[2.5px] bg-white rounded-full" style={{ animation: "eqC 1.1s ease-in-out infinite" }}></span>
            <span className="bar w-[2.5px] bg-white rounded-full" style={{ animation: "eqB 0.85s ease-in-out infinite reverse" }}></span>
          </span>
        ) : (
          <span className="relative w-5 h-5 flex items-center justify-center">
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
              <path d="M2 6h2l4-3v10L4 10H2V6z" stroke="white" strokeWidth="1.2" strokeLinejoin="round" />
              <path d="M11 5l4 6M15 5l-4 6" stroke="white" strokeWidth="1.2" strokeLinecap="round" />
            </svg>
          </span>
        )}
        <span className="hidden md:inline font-mono text-[10px] tracking-[0.22em] text-white/85">
          {on ? "SOUND ON" : "SOUND OFF"}
        </span>
      </button>
    </>
  );
}

window.MusicToggle = MusicToggle;
