/* Animated decorative signal sculpture. The signal is fictional, not a live receiver feed. */
const TAU=Math.PI*2;
const hash=n=>{const x=Math.sin(n*127.121+2.411)*9731.271;return x-Math.floor(x)};
export function mountArt(canvas){
  if(!canvas||!canvas.getContext)return;
  const ctx=canvas.getContext('2d',{alpha:true});if(!ctx)return;
  const reduced=window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  let w=0,h=0,dpr=1,frame=0,old=0;
  function resize(){
    const r=canvas.getBoundingClientRect();w=Math.max(1,r.width);h=Math.max(1,r.height);
    dpr=Math.min(window.devicePixelRatio||1,2);
    canvas.width=Math.round(w*dpr);canvas.height=Math.round(h*dpr);ctx.setTransform(dpr,0,0,dpr,0,0);
    if(reduced)draw(6);
  }
  function draw(t){
    ctx.clearRect(0,0,w,h);
    const dark=ctx.createLinearGradient(0,0,w,h);dark.addColorStop(0,'#101e32');dark.addColorStop(1,'#091628');ctx.fillStyle=dark;ctx.fillRect(0,0,w,h);
    const glow=ctx.createRadialGradient(w*.58,h*.54,0,w*.58,h*.54,w*.55);
    glow.addColorStop(0,'rgba(45,92,137,.28)');glow.addColorStop(1,'rgba(8,18,30,0)');ctx.fillStyle=glow;ctx.fillRect(0,0,w,h);
    ctx.lineWidth=.7;
    for(let x=16;x<w;x+=28){ctx.strokeStyle='rgba(164,199,213,.065)';ctx.beginPath();ctx.moveTo(x,0);ctx.lineTo(x,h);ctx.stroke();}
    for(let y=12;y<h;y+=28){ctx.strokeStyle='rgba(164,199,213,.065)';ctx.beginPath();ctx.moveTo(0,y);ctx.lineTo(w,y);ctx.stroke();}
    const count=72;
    // Spectral bars, vividly colored and highly localised; stay within the display viewport.
    for(let i=0;i<count;i++){
      const px=(i+.5)/count*w;
      let peak=0;
      for(const [at,str,width] of [[.18,.65,.022],[.31,.92,.023],[.52,.78,.03],[.70,1,.024],[.83,.53,.019]]){
        peak+=Math.exp(-Math.pow((i/count-at)/width,2))*str;
      }
      const noise=.055+hash(i*32)*.09;
      const flutter=Math.sin(t*2.1+i*.61)*.085+Math.sin(t*1.13+i*.41)*.035;
      const val=Math.min(1.06,Math.max(.025,peak*(.85+flutter)+noise));
      const barH=(h*.18+h*.56*val);
      let palette;
      if(i>46&&i<55)palette=[250,119,89];
      else if(i>18&&i<31)palette=[177,246,110];
      else if(i>58)palette=[118,132,255];
      else palette=[85,210,232];
      const a=.25+.65*val;
      ctx.fillStyle=`rgba(${palette.join(',')},${a*.13})`;ctx.fillRect(px-1.8,h*.82-barH,3.6,barH+4);
      ctx.fillStyle=`rgba(${palette.join(',')},${a})`;
      ctx.fillRect(px-.8,h*.82-barH,1.6,barH);
      if(val>.4){ctx.fillStyle=`rgba(${palette.join(',')},.85)`;ctx.fillRect(px-1.5,h*.82-barH,3,1.7);}
    }
    // A single fine signal trace that ties the signal peaks together.
    ctx.beginPath();
    for(let i=0;i<=w;i+=2){
      const n=i/w;let amp=.03;
      for(const [x,a,b] of [[.18,.15,.025],[.31,.32,.028],[.52,.23,.038],[.70,.35,.028],[.83,.13,.024]]){
        amp+=a*Math.exp(-Math.pow((n-x)/b,2));
      }
      const y=h*.82-h*.7*(amp*(.9+.075*Math.sin(t*2+n*34))+Math.sin(n*125+t*1.2)*.012);
      if(i===0)ctx.moveTo(i,y);else ctx.lineTo(i,y);
    }
    ctx.strokeStyle='rgba(208,247,245,.61)';ctx.lineWidth=.9;ctx.stroke();
    // Luminous scanline.
    const scanX=(t*31)%(w+80)-40;
    const g=ctx.createLinearGradient(scanX-30,0,scanX+30,0);
    g.addColorStop(0,'rgba(133,216,244,0)');g.addColorStop(.5,'rgba(133,216,244,.09)');g.addColorStop(1,'rgba(133,216,244,0)');ctx.fillStyle=g;ctx.fillRect(scanX-30,0,60,h);
    ctx.strokeStyle='rgba(179,229,231,.17)';ctx.beginPath();ctx.moveTo(0,h*.82);ctx.lineTo(w,h*.82);ctx.stroke();
    // Morse dots and dashes, just below the trace (not another huge scene).
    const seq=[1,3,1,1,3,3,1,3,1,1,3,1,3,1,1,1];
    const total=seq.reduce((a,b)=>a+b,0)+seq.length*1.5;
    let x=w*.14;const unit=Math.min(6,w*.72/total);
    seq.forEach((v,i)=>{
      const light=(.65+.35*Math.sin(t*2.8+i*.4));
      ctx.fillStyle=i%5===0?`rgba(255,129,96,${light})`:`rgba(162,235,174,${light*.64})`;
      ctx.fillRect(x,h*.935,Math.max(2.6,v*unit),2.2);x+=(v+1.5)*unit;
    });
  }
  const observer=new ResizeObserver(resize);observer.observe(canvas);resize();
  function tick(t){if(document.hidden){frame=requestAnimationFrame(tick);return;}if(t-old>=32){old=t;draw(t/1000);}frame=requestAnimationFrame(tick)}
  if(!reduced){frame=requestAnimationFrame(tick)}
  return()=>{observer.disconnect();cancelAnimationFrame(frame)};
}
