import { useEffect, useRef } from 'react';
import FilerobotImageEditor, { TABS } from 'react-filerobot-image-editor';
import { StyleSheetManager } from 'styled-components';
import isPropValid from '@emotion/is-prop-valid';
import { useFocusTrap } from '../../hooks/useFocusTrap';
import '../../styles/PhotoPreEditor.css';

export function PhotoPreEditor({ imageSrc, onComplete, onClose }) {
  const modalContentRef = useRef(null);
  const previousFocusRef = useRef(null);

  // Focus trap (S24) — keyboard navigation can't escape the photo
  // editor while it's open. Filerobot itself manages focus inside the
  // editor; the trap just prevents Tab from leaking back into the
  // shell editor underneath.
  useFocusTrap(true, modalContentRef);

  useEffect(() => {
    previousFocusRef.current = document.activeElement;
    const handleKeyDown = (e) => { if (e.key === 'Escape') onClose(); };
    document.addEventListener('keydown', handleKeyDown);
    return () => {
      document.removeEventListener('keydown', handleKeyDown);
      previousFocusRef.current?.focus();
    };
  }, [onClose]);

  const base64ToBlob = (base64DataUrl) => {
    const [header, data] = base64DataUrl.split(',');
    const mimeMatch = header?.match(/data:(.*?);base64/);
    const mime = mimeMatch?.[1] || 'image/png';
    const binary = atob(data || '');
    const bytes = new Uint8Array(binary.length);
    for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
    return new Blob([bytes], { type: mime });
  };

  const handleSave = async (savedImageData) => {
    try {
      // Prefer base64 when available (works without CORS/network).
      if (savedImageData?.imageBase64) {
        const blob = base64ToBlob(savedImageData.imageBase64);
        onComplete(URL.createObjectURL(blob));
        return;
      }

      // Fallback to canvas when provided by the library.
      if (savedImageData?.imageCanvas instanceof HTMLCanvasElement) {
        const blob = await new Promise((resolve) =>
          savedImageData.imageCanvas.toBlob(resolve, savedImageData.mimeType || 'image/png', savedImageData.quality),
        );
        if (blob) onComplete(URL.createObjectURL(blob));
        else throw new Error('Canvas export failed');
        return;
      }

      // Final fallback: cloudimageUrl (fetch then blob).
      if (savedImageData?.cloudimageUrl) {
        const res = await fetch(savedImageData.cloudimageUrl);
        const blob = await res.blob();
        onComplete(URL.createObjectURL(blob));
        return;
      }

      throw new Error('No export data returned from image editor');
    } catch (error) {
      console.error('Export failed:', error);
      onClose();
    }
  };

  return (
    <div className="filerobot-overlay" role="dialog" aria-modal="true" aria-labelledby="photo-editor-title">
      <div className="filerobot-container" ref={modalContentRef} role="document">
        <StyleSheetManager
          // Filerobot/@scaleflex styled-components pass a bunch of styling props to DOM nodes (e.g. isPhoneScreen).
          // Filtering them here prevents noisy React console warnings.
          shouldForwardProp={(prop, element) => (typeof element === 'string' ? isPropValid(prop) : true)}
        >
          <FilerobotImageEditor
            source={imageSrc}
            onBeforeSave={() => false}
            onSave={handleSave}
            onClose={() => onClose()}
            tabsIds={[TABS.ADJUST, TABS.FILTERS, TABS.FINETUNE]}
            defaultTabId={TABS.ADJUST}
            // Crop defaults:
            //   - ratio: 'original' — selects the source image's native ratio so
            //     the initial crop covers the whole image rather than dropping a
            //     small "custom" rectangle in the middle.
            //   - autoResize: false — the prior `autoResize: true` together with
            //     defaultSizePercentage:1 was being interpreted as "size to fit
            //     the canvas viewport", which on small previews shrank the crop
            //     to a sub-image region.
            //   - presetCropAreaPosition: 'cover' — explicitly anchors the crop
            //     to cover the full image on first paint.
            Crop={{
              autoResize: false,
              ratio: 'original',
              presetCropAreaPosition: 'cover',
            }}
            theme={{ accentColor: '#ec4899', palettePrimary: '#ec4899' }}
            forceToPngInEllipticalCrop
            closeAfterSave
            defaultSavedImageName="edited-image"
            defaultSavedImageType="png"
            defaultSavedImageQuality={1}
            savingPixelRatio={4}
            previewPixelRatio={4}
          />
        </StyleSheetManager>
      </div>
      <h2 id="photo-editor-title" className="sr-only">Photo Editor</h2>
    </div>
  );
}
