'use client';

import {forwardRef,useEffect,useImperativeHandle,useRef,useState} from 'react';
import {Globe2,Pause,Play} from 'lucide-react';
import {heroAnimationFrame,type HeroAnimationFrame} from '@/lib/hero-animation';
import type {HeroMapController} from '@/lib/hero-map/renderer.js';

type MotionProps={paused:boolean;reducedMotion:boolean;onTogglePause:()=>void};
export type HeroAssetMapHandle={render:(frame:HeroAnimationFrame)=>void};
export function HeroMotionButton({paused,reducedMotion,onTogglePause}:MotionProps){
 const label=reducedMotion?'حرکت طبق تنظیمات کاهش حرکت متوقف است':paused?'ادامه انیمیشن هیرو':'توقف انیمیشن هیرو';
 return <button type="button" className="map-motion" onClick={onTogglePause} disabled={reducedMotion} aria-label={label} title={label} aria-pressed={paused||reducedMotion}>{paused||reducedMotion?<Play size={17} aria-hidden="true"/>:<Pause size={17} aria-hidden="true"/>}</button>;
}
export const HeroAssetMap=forwardRef<HeroAssetMapHandle,MotionProps>(function HeroAssetMap({paused,reducedMotion,onTogglePause},ref) {
 const host=useRef<HTMLDivElement>(null);
 const renderer=useRef<HeroMapController|null>(null);
 const latestFrame=useRef(heroAnimationFrame(0));
 const options=useRef({light:false,paused,reducedMotion});
 const [failed,setFailed]=useState(false);
 useImperativeHandle(ref,()=>({render(frame){latestFrame.current=frame;renderer.current?.render(frame)}}),[]);
 useEffect(()=>{options.current={...options.current,paused,reducedMotion};renderer.current?.update(options.current)},[paused,reducedMotion]);
 useEffect(()=>{
  let disposed=false;
  const root=host.current;
  if(!root)return;
  const updateTheme=()=>{
   options.current.light=document.documentElement.dataset.theme==='light';
   renderer.current?.update(options.current);
  };
  updateTheme();
  const observer=new MutationObserver(updateTheme);
  observer.observe(document.documentElement,{attributes:true,attributeFilter:['data-theme']});
  import('@/lib/hero-map/renderer.js').then(module=>{
   if(disposed)return;
   renderer.current=module.createAssetMap(root,options.current);
   renderer.current.render(latestFrame.current);
  }).catch(()=>{if(!disposed)setFailed(true)});
  return()=>{disposed=true;observer.disconnect();renderer.current?.dispose();renderer.current=null};
 },[]);
 return <div ref={host} className="hero-asset-map" role="group" aria-label="نقشه تعاملی دارایی‌ها">
  <div className="hero-map-stage" hidden={failed}>
   <canvas tabIndex={0} role="img" aria-label="نقشه دارایی‌های ایران و جهان؛ برای چرخاندن بکشید یا از کلیدهای جهت استفاده کنید"/>
   <div className="hero-map-landmarks" aria-hidden="true"/>
   <div className="hero-map-pins"/>
  </div>
  <div className="hero-map-controls" role="group" aria-label="کنترل نقشه و انیمیشن هیرو" dir="rtl">
   <HeroMotionButton paused={paused} reducedMotion={reducedMotion} onTogglePause={onTogglePause}/>
   <span className="map-divider" aria-hidden="true"/>
   <button type="button" className="map-iran" aria-pressed="false" aria-label="ایران" title="ایران"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round" aria-hidden="true"><path d="M6.6 15.4 6.1 14.7 6.1 14.1 5.7 14.1 5.9 13.2 5.4 12.3 4.1 11.6 3.4 10.4 3.6 9.4 4.1 9 4 8.2 3.4 7.9 2.7 6.3 2.1 5.3 2.3 4.8 2 3.3 2.7 2.9 2.9 3.4 3.4 4.1 4.5 4.2 5.7 3.2 6.1 3.1 6.4 3.5 6.1 4.2 6.7 4.9 7 4.8 7.3 5.8 8.3 6 9 6.7 10.5 6.9 12.1 6.6 12.2 6.3 13.1 6 13.9 5.3 14.6 5.3 15 5.1 15.8 5.2 16.9 5.8 17.7 6 18.9 7.1 19.7 7.2 19.8 8.3 19.4 9.9 19.1 10.8 19.6 11 19.1 11.6 19.4 12.6 19.5 13.4 20.3 13.6 20.4 14.4 19.5 15.5 20 16.1 20.4 16.8 21.4 17.4 21.4 18.4 21.9 18.6 22 19.1 20.5 19.7 20.1 21.1 18.1 20.7 17 20.5 15.8 20.3 15.4 18.9 14.9 18.7 14.1 18.9 13 19.4 11.8 19.1 10.7 18.2 9.7 17.8 9 16.7 8.3 15.1 7.7 15.3 7 14.9Z"/></svg></button>
   <button type="button" className="map-world" aria-pressed="true" aria-label="بین‌الملل" title="بین‌الملل؛ گردش خودکار از ایران و بازگشت به ایران"><Globe2 size={17} aria-hidden="true"/></button>
  </div>
  <span className="hero-map-selection" role="status" aria-live="polite"/>
 </div>;
});
