'use client';

import React, { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Upload, Library, Search, Music2, ChevronDown } from 'lucide-react';
import { cn } from '@/public/lib/utils';

interface ReplaceMusicModalProps {
  isOpen: boolean;
  activeReplaceMethod: string;
  onClose: () => void;
  onMusicSelect: (music: any) => void;
}

export function ReplaceMusicModal({
  isOpen,
  activeReplaceMethod,
  onClose,
  onMusicSelect
}: ReplaceMusicModalProps) {
  const [activeMethod, setActiveMethod] = useState(activeReplaceMethod);
  const [searchQuery, setSearchQuery] = useState('');

  useEffect(() => {
    setActiveMethod(activeReplaceMethod);
  }, [activeReplaceMethod]);

  // 模拟音乐库数据
  const mockLibraryMusics = [
    { 
      id: 1, 
      name: 'Upbeat Pop',
      duration: '02:35',
      genre: '流行音乐',
      url: 'https://example.com/music1.mp3'
    },
    { 
      id: 2, 
      name: 'Chill Lofi',
      duration: '03:15',
      genre: '轻音乐',
      url: 'https://example.com/music2.mp3'
    },
    { 
      id: 3, 
      name: 'Epic Orchestra',
      duration: '04:20',
      genre: '管弦乐',
      url: 'https://example.com/music3.mp3'
    },
  ];

  const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (file) {
      // 处理文件上传
      console.log('Uploading file:', file);
    }
  };

  return (
    <AnimatePresence>
      {isOpen && (
        <>
          {/* 背景遮罩 */}
          <motion.div
            className="fixed inset-0 bg-black/60 backdrop-blur-sm z-50"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            onClick={onClose}
          />

          {/* 弹窗内容 */}
          <div className="fixed inset-x-0 bottom-0 z-50 flex justify-center">
            <motion.div
              className="w-[66%] min-w-[800px] bg-[#1a1b1e] rounded-t-2xl overflow-hidden"
              initial={{ y: '100%' }}
              animate={{ y: 0 }}
              exit={{ y: '100%' }}
              transition={{
                type: 'spring',
                damping: 25,
                stiffness: 200,
              }}
            >
              {/* 标题栏 */}
              <div className="flex items-center justify-between p-4 border-b border-white/10">
                <div className="flex items-center gap-2">
                  <button 
                    className="p-1 hover:bg-white/10 rounded-full transition-colors"
                    onClick={onClose}
                  >
                    <ChevronDown className="w-5 h-5" />
                  </button>
                  <h2 className="text-lg font-medium">Replace music</h2>
                </div>
              </div>

              {/* 主要内容区域 */}
              <div className="p-6 space-y-6 h-[80vh]">
                {/* 替换方式选择 */}
                <div className="flex gap-4">
                  <motion.button
                    className={cn(
                      'flex-1 flex flex-col items-center gap-2 p-4 rounded-lg border-2 transition-colors',
                      activeMethod === 'upload' 
                        ? 'border-blue-500 bg-blue-500/10' 
                        : 'border-white/10 hover:border-white/20'
                    )}
                    onClick={() => setActiveMethod('upload')}
                    whileHover={{ scale: 1.02 }}
                    whileTap={{ scale: 0.98 }}
                  >
                    <Upload className="w-6 h-6" />
                    <span>Upload music</span>
                  </motion.button>

                  <motion.button
                    className={cn(
                      'flex-1 flex flex-col items-center gap-2 p-4 rounded-lg border-2 transition-colors',
                      activeMethod === 'library' 
                        ? 'border-blue-500 bg-blue-500/10' 
                        : 'border-white/10 hover:border-white/20'
                    )}
                    onClick={() => setActiveMethod('library')}
                    whileHover={{ scale: 1.02 }}
                    whileTap={{ scale: 0.98 }}
                  >
                    <Library className="w-6 h-6" />
                    <span>Library</span>
                  </motion.button>
                </div>

                {/* 内容区域 */}
                <AnimatePresence mode="wait">
                  {activeMethod === 'upload' && (
                    <motion.div
                      key="upload"
                      initial={{ opacity: 0, y: 20 }}
                      animate={{ opacity: 1, y: 0 }}
                      exit={{ opacity: 0, y: -20 }}
                      className="flex flex-col items-center justify-center p-8 border-2 border-dashed border-white/10 rounded-lg"
                    >
                      <input
                        type="file"
                        accept="audio/*"
                        className="hidden"
                        id="music-upload"
                        onChange={handleFileUpload}
                      />
                      <label
                        htmlFor="music-upload"
                        className="flex flex-col items-center gap-4 cursor-pointer"
                      >
                        <motion.div
                          className="p-4 rounded-full bg-white/5"
                          whileHover={{ scale: 1.1, backgroundColor: 'rgba(255,255,255,0.1)' }}
                          whileTap={{ scale: 0.9 }}
                        >
                          <Music2 className="w-8 h-8 text-white/70" />
                        </motion.div>
                        <div className="text-center">
                          <p className="text-white/70">Click to upload or drag and drop music files here</p>
                          <p className="text-sm text-white/50 mt-2">Supports MP3, WAV, M4A formats</p>
                        </div>
                      </label>
                    </motion.div>
                  )}

                  {activeMethod === 'library' && (
                    <motion.div
                      key="library"
                      initial={{ opacity: 0, y: 20 }}
                      animate={{ opacity: 1, y: 0 }}
                      exit={{ opacity: 0, y: -20 }}
                      className="space-y-4"
                    >
                      {/* 搜索框 */}
                      <div className="relative">
                        <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-white/50" />
                        <input
                          type="text"
                          placeholder="Search music..."
                          className="w-full pl-10 pr-4 py-2 bg-white/5 border border-white/10 rounded-lg 
                            focus:outline-none focus:border-blue-500 transition-colors"
                          value={searchQuery}
                          onChange={(e) => setSearchQuery(e.target.value)}
                        />
                      </div>

                      {/* 音乐列表 */}
                      <div className="space-y-2">
                        {mockLibraryMusics.map((music) => (
                          <motion.div
                            key={music.id}
                            className="group relative p-4 rounded-lg bg-white/5 hover:bg-white/10 
                              cursor-pointer transition-colors"
                            whileHover={{ scale: 1.01 }}
                            whileTap={{ scale: 0.99 }}
                            onClick={() => onMusicSelect(music)}
                          >
                            <div className="flex items-center gap-4">
                              <div className="flex-shrink-0">
                                <Music2 className="w-8 h-8 text-white/70" />
                              </div>
                              <div className="flex-1 min-w-0">
                                <div className="flex items-center justify-between">
                                  <h3 className="text-sm font-medium truncate">{music.name}</h3>
                                  <span className="text-xs text-white/50">{music.duration}</span>
                                </div>
                                <p className="text-xs text-white/50 mt-1">{music.genre}</p>
                              </div>
                            </div>
                          </motion.div>
                        ))}
                      </div>
                    </motion.div>
                  )}
                </AnimatePresence>
              </div>

              {/* 底部操作栏 */}
              <div className="p-4 border-t border-white/10 bg-black/20">
                <div className="flex justify-end gap-3">
                  <motion.button
                    className="px-4 py-2 rounded-lg bg-white/10 text-white hover:bg-white/20 transition-colors"
                    whileHover={{ scale: 1.02 }}
                    whileTap={{ scale: 0.98 }}
                    onClick={onClose}
                  >
                    Reset
                  </motion.button>
                  <motion.button
                    className="px-4 py-2 rounded-lg bg-blue-500 text-white hover:bg-blue-600 transition-colors"
                    whileHover={{ scale: 1.02 }}
                    whileTap={{ scale: 0.98 }}
                  >
                    Apply
                  </motion.button>
                </div>
              </div>
            </motion.div>
          </div>
        </>
      )}
    </AnimatePresence>
  );
} 