'use client';

import * as React from 'react';
import { Upload, Plus } from 'lucide-react';
import { Modal } from '@/components/ui/Modal';
import { Button } from '@/components/ui/Button';
import { decodeAudioFile } from '@/lib/audio/decoder';

export interface ImportTrackDialogProps {
  open: boolean;
  onClose: () => void;
  onImportTrack: (buffer: AudioBuffer, name: string) => void;
}

export function ImportTrackDialog({
  open,
  onClose,
  onImportTrack,
}: ImportTrackDialogProps) {
  const [isDragging, setIsDragging] = React.useState(false);
  const [isLoading, setIsLoading] = React.useState(false);
  const fileInputRef = React.useRef<HTMLInputElement>(null);

  const handleFiles = async (files: FileList) => {
    setIsLoading(true);

    // Convert FileList to Array to prevent any weird behavior
    const fileArray = Array.from(files);
    console.log(`[ImportTrackDialog] Processing ${fileArray.length} files`, fileArray);

    try {
      // Process files sequentially
      for (let i = 0; i < fileArray.length; i++) {
        console.log(`[ImportTrackDialog] Loop iteration ${i}, fileArray.length: ${fileArray.length}`);
        const file = fileArray[i];
        console.log(`[ImportTrackDialog] Processing file ${i + 1}/${fileArray.length}: ${file.name}, type: ${file.type}`);

        if (!file.type.startsWith('audio/')) {
          console.warn(`Skipping non-audio file: ${file.name} (type: ${file.type})`);
          continue;
        }

        try {
          console.log(`[ImportTrackDialog] Decoding file ${i + 1}/${files.length}: ${file.name}`);
          const buffer = await decodeAudioFile(file);
          const trackName = file.name.replace(/\.[^/.]+$/, ''); // Remove extension
          console.log(`[ImportTrackDialog] Importing track: ${trackName}`);
          onImportTrack(buffer, trackName);
          console.log(`[ImportTrackDialog] Track imported: ${trackName}`);
        } catch (error) {
          console.error(`Failed to import ${file.name}:`, error);
        }
        console.log(`[ImportTrackDialog] Finished processing file ${i + 1}`);
      }

      console.log('[ImportTrackDialog] Loop completed, all files processed');
    } catch (error) {
      console.error('[ImportTrackDialog] Error in handleFiles:', error);
    } finally {
      setIsLoading(false);
      console.log('[ImportTrackDialog] Closing dialog');
      onClose();
    }
  };

  const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    const files = e.target.files;
    if (files && files.length > 0) {
      handleFiles(files);
    }
    // Reset input
    e.target.value = '';
  };

  const handleDragOver = (e: React.DragEvent) => {
    e.preventDefault();
    setIsDragging(true);
  };

  const handleDragLeave = (e: React.DragEvent) => {
    e.preventDefault();
    setIsDragging(false);
  };

  const handleDrop = (e: React.DragEvent) => {
    e.preventDefault();
    setIsDragging(false);

    const files = e.dataTransfer.files;
    if (files && files.length > 0) {
      handleFiles(files);
    }
  };

  return (
    <Modal
      open={open}
      onClose={onClose}
      title="Import Audio as Tracks"
      description="Import one or more audio files as new tracks"
    >
      <div className="space-y-4">
        {/* Drag and Drop Area */}
        <div
          onDragOver={handleDragOver}
          onDragLeave={handleDragLeave}
          onDrop={handleDrop}
          className={`
            border-2 border-dashed rounded-lg p-8 text-center transition-colors
            ${isDragging
              ? 'border-primary bg-primary/10'
              : 'border-border hover:border-primary/50'
            }
            ${isLoading ? 'opacity-50 pointer-events-none' : ''}
          `}
        >
          <Upload className="h-12 w-12 mx-auto mb-4 text-muted-foreground" />
          <p className="text-sm text-foreground mb-2">
            {isLoading ? 'Importing files...' : 'Drag and drop audio files here'}
          </p>
          <p className="text-xs text-muted-foreground mb-4">
            or
          </p>
          <Button
            onClick={() => fileInputRef.current?.click()}
            disabled={isLoading}
          >
            <Plus className="h-4 w-4 mr-2" />
            Choose Files
          </Button>
          <input
            ref={fileInputRef}
            type="file"
            accept="audio/*"
            multiple
            onChange={handleFileSelect}
            className="hidden"
          />
        </div>

        {/* Supported Formats */}
        <div className="text-xs text-muted-foreground">
          <p className="font-medium mb-1">Supported formats:</p>
          <p>MP3, WAV, OGG, FLAC, M4A, AAC, and more</p>
          <p className="mt-2">
            💡 Tip: Select multiple files at once or drag multiple files to import them all as separate tracks
          </p>
        </div>

        {/* Actions */}
        <div className="flex justify-end gap-2 pt-2 border-t border-border">
          <Button variant="outline" onClick={onClose} disabled={isLoading}>
            Cancel
          </Button>
        </div>
      </div>
    </Modal>
  );
}
