/*
 * Info-Icon + Tooltip: Hover zeigt Tooltip und blendet beim Verlassen aus.
 * Touch: nach dem Loslassen schließt das Tooltip automatisch (kein Pin bis Tap außerhalb).
 * Auto-Fade blendet das Icon nach 1,5 s aus.
 * `createIconTooltip({ iconEl, ownerDoc })` gibt `tooltipEl`, `showIcon`, `scheduleIconHide`, `unbind` zurück.
 */

const HIDE_DELAY_MS = 1500;
const TOUCH_TOOLTIP_HIDE_AFTER_MS = 400;
const TOOLTIP_OFFSET_PX = 16;
const TOOLTIP_WIDTH_FALLBACK_PX = 380;

export function createIconTooltip({ iconEl, ownerDoc }) {
  let iconHideTimer = null;
  let touchTooltipHideTimer = null;

  function scheduleIconHide() {
    if (iconHideTimer) clearTimeout(iconHideTimer);
    iconHideTimer = setTimeout(() => {
      if (iconEl) iconEl.classList.add('ce-diagram-icon-faded');
      iconHideTimer = null;
    }, HIDE_DELAY_MS);
  }

  function showIcon() {
    if (iconEl) iconEl.classList.remove('ce-diagram-icon-faded');
    scheduleIconHide();
  }

  const iconTooltipEl = iconEl ? iconEl.querySelector('.ce-diagram-icon-tooltip') : null;
  if (iconTooltipEl && iconTooltipEl.parentNode !== ownerDoc.body) ownerDoc.body.appendChild(iconTooltipEl);

  /** Handles werden außerhalb des if-Blocks referenzierbar gehalten, damit unbind() sie
   *  entfernen kann (siehe Kommentar zu Viewer-Sort-Reinit). */
  let onIconPointerUp = null;
  let onIconPointerCancel = null;
  let onIconPointerEnter = null;
  let onIconPointerMove = null;
  let onIconPointerLeave = null;

  if (iconEl && iconTooltipEl) {
    function updateIconTooltipPos(cx, cy) {
      if (!iconTooltipEl.classList.contains('ce-diagram-icon-tooltip-visible')) return;
      const rect = iconTooltipEl.getBoundingClientRect();
      const w = rect.width || TOOLTIP_WIDTH_FALLBACK_PX, h = rect.height || 120;
      let left = cx - w - TOOLTIP_OFFSET_PX, top = cy + TOOLTIP_OFFSET_PX;
      if (left < 8) left = 8;
      if (left + w > window.innerWidth - 8) left = window.innerWidth - w - 8;
      if (top + h > window.innerHeight - 8) top = window.innerHeight - h - 8;
      iconTooltipEl.style.left = left + 'px'; iconTooltipEl.style.top = top + 'px';
    }
    onIconPointerEnter = (e) => {
      if (iconHideTimer) { clearTimeout(iconHideTimer); iconHideTimer = null; }
      if (iconEl) iconEl.classList.remove('ce-diagram-icon-faded');
      iconTooltipEl.classList.add('ce-diagram-icon-tooltip-visible');
      iconTooltipEl.style.left = (e.clientX - TOOLTIP_WIDTH_FALLBACK_PX - TOOLTIP_OFFSET_PX) + 'px';
      iconTooltipEl.style.top = (e.clientY + TOOLTIP_OFFSET_PX) + 'px';
      requestAnimationFrame(() => updateIconTooltipPos(e.clientX, e.clientY));
    };
    onIconPointerMove = (e) => updateIconTooltipPos(e.clientX, e.clientY);
    function hideIconTooltip() {
      if (touchTooltipHideTimer) { clearTimeout(touchTooltipHideTimer); touchTooltipHideTimer = null; }
      iconTooltipEl.classList.remove('ce-diagram-icon-tooltip-visible');
      scheduleIconHide();
    }
    onIconPointerLeave = () => {
      hideIconTooltip();
    };
    function scheduleTouchIconTooltipHide() {
      if (touchTooltipHideTimer) clearTimeout(touchTooltipHideTimer);
      touchTooltipHideTimer = setTimeout(() => {
        touchTooltipHideTimer = null;
        iconTooltipEl.classList.remove('ce-diagram-icon-tooltip-visible');
        scheduleIconHide();
      }, TOUCH_TOOLTIP_HIDE_AFTER_MS);
    }
    onIconPointerUp = (e) => {
      if (e.pointerType === 'touch') scheduleTouchIconTooltipHide();
    };
    onIconPointerCancel = (e) => {
      if (e.pointerType === 'touch') scheduleTouchIconTooltipHide();
    };
    iconEl.addEventListener('pointerenter', onIconPointerEnter);
    iconEl.addEventListener('pointermove', onIconPointerMove);
    iconEl.addEventListener('pointerleave', onIconPointerLeave);
    iconEl.addEventListener('pointerup', onIconPointerUp);
    iconEl.addEventListener('pointercancel', onIconPointerCancel);
  }

  function unbind() {
    if (iconEl) {
      if (onIconPointerEnter) iconEl.removeEventListener('pointerenter', onIconPointerEnter);
      if (onIconPointerMove) iconEl.removeEventListener('pointermove', onIconPointerMove);
      if (onIconPointerLeave) iconEl.removeEventListener('pointerleave', onIconPointerLeave);
      if (onIconPointerUp) iconEl.removeEventListener('pointerup', onIconPointerUp);
      if (onIconPointerCancel) iconEl.removeEventListener('pointercancel', onIconPointerCancel);
    }
    if (iconHideTimer != null) { clearTimeout(iconHideTimer); iconHideTimer = null; }
    if (touchTooltipHideTimer != null) { clearTimeout(touchTooltipHideTimer); touchTooltipHideTimer = null; }
    if (iconTooltipEl && iconTooltipEl.parentNode) iconTooltipEl.parentNode.removeChild(iconTooltipEl);
  }

  return {
    tooltipEl: iconTooltipEl,
    showIcon,
    scheduleIconHide,
    unbind,
  };
}
