"use client";

import { useState } from 'react';
import { motion } from 'framer-motion';
import { DragDropContext, DropResult } from 'react-beautiful-dnd';
import { ScriptMetaInfo } from '../script-overview/script-meta-info';
import { SceneFilmstrip } from '../script-overview/scene-filmstrip';
import { StoryboardCardList } from '../storyboard/storyboard-card-list';

// 分镜场景数据结构
export interface StoryboardScene {
  id: string;
  name: string;
  description: string;
  shot: string;
  frame: string;
  atmosphere: string;
  imageUrl: string;
}

// 剧本元信息数据结构
export interface ScriptMeta {
  title: string;
  type: string;
  genre: string;
  tags: string[];
  duration: string;
  uploadTime: string;
}

export default function StoryboardView() {
  // 示例数据
  const [scriptMeta] = useState<ScriptMeta>({
    title: "Lost Stars",
    type: "Short Drama",
    genre: "Sci-Fi/Mystery",
    tags: ["Space", "Psychology", "Future"],
    duration: "20-25 minutes",
    uploadTime: "2024-03-20"
  });

  const [scenes, setScenes] = useState<StoryboardScene[]>([
    {
      id: "scene-1",
      name: "Opening Shot",
      description: "Establishing shot of the space station",
      shot: "Wide angle, slow pan from right to left",
      frame: "Space station silhouetted against Earth",
      atmosphere: "Mysterious, serene, with soft starlight",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-2",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-3",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-4",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-5",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-6",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-7",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-8",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-9",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-10",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    },
    {
      id: "scene-11",
      name: "Control Room",
      description: "Interior of the main control center",
      shot: "Medium close-up, steady cam",
      frame: "Monitors displaying unusual signal patterns",
      atmosphere: "Tense, with blinking lights and shadows",
      imageUrl: "https://smart-video-ai.oss-cn-beijing.aliyuncs.com/frames/d877fa43-4856-4acb-9a3b-627c28275343/frame_000007.jpg/1750507514_tmphfb431oc_000007.jpg",
    }
  ]);

  const [selectedSceneId, setSelectedSceneId] = useState<string>();

  // 处理场景拖拽排序
  const handleDragEnd = (result: DropResult) => {
    if (!result.destination) return;

    const items = Array.from(scenes);
    const [reorderedItem] = items.splice(result.source.index, 1);
    items.splice(result.destination.index, 0, reorderedItem);

    setScenes(items);
  };

  // 处理场景更新
  const handleSceneUpdate = (sceneId: string, updates: Partial<StoryboardScene>) => {
    setScenes(scenes.map(scene => 
      scene.id === sceneId ? { ...scene, ...updates } : scene
    ));
  };

  // 处理场景删除
  const handleSceneDelete = (sceneId: string) => {
    setScenes(scenes.filter(scene => scene.id !== sceneId));
    if (selectedSceneId === sceneId) {
      setSelectedSceneId(undefined);
    }
  };

  // 处理场景复制
  const handleSceneDuplicate = (sceneId: string) => {
    const sceneToDuplicate = scenes.find(scene => scene.id === sceneId);
    if (!sceneToDuplicate) return;

    const newScene = {
      ...sceneToDuplicate,
      id: `scene-${Date.now()}`,
      name: `${sceneToDuplicate.name} (Copy)`
    };

    setScenes([...scenes, newScene]);
  };

  return (
    <div className="h-full bg-[#0C0E11] text-white">
      <div className="h-full flex">
        {/* Left: Script Meta Info */}
        <div className="flex-shrink-0 h-full">
          <ScriptMetaInfo meta={scriptMeta} />
        </div>

        {/* Right: Scene Content */}
        <div className="flex-grow min-w-0 h-full overflow-hidden flex flex-col">
          {/* Scene Card List */}
          <div className="flex-grow overflow-y-auto">
            <DragDropContext onDragEnd={handleDragEnd}>
              <StoryboardCardList
                scenes={scenes}
                selectedSceneId={selectedSceneId}
                onSceneUpdate={handleSceneUpdate}
                onSceneDelete={handleSceneDelete}
                onSceneDuplicate={handleSceneDuplicate}
              />
            </DragDropContext>
          </div>

          {/* Filmstrip Preview */}
          <div className="flex-shrink-0 py-6 px-8">
            <SceneFilmstrip 
              scenes={scenes}
              selectedSceneId={selectedSceneId}
              onSceneSelect={(sceneId: string) => {
                setSelectedSceneId(sceneId);
              }}
            />
          </div>
        </div>
      </div>
    </div>
  );
} 