
import { useEffect, useState } from "react";
import { useShotService } from "@/app/service/Interaction/ShotService";
import { useSearchParams } from 'next/navigation';
import { useRoleServiceHook } from "@/app/service/Interaction/RoleService";
import { useRoleShotServiceHook } from "@/app/service/Interaction/RoleShotService";
import { useScriptService } from "@/app/service/Interaction/ScriptService";

export const useEditData = (tabType: string, originalText?: string) => {
  const searchParams = useSearchParams();
  const projectId = searchParams.get('episodeId') || '';
  const [loading, setLoading] = useState(true);
  const [scriptData, setScriptData] = useState<any[]>([]);
  const [shotData, setShotData] = useState<any[]>([]);

  const [roleData, setRoleData] = useState<any[]>([]);

  const {
    scriptBlocksMemo, // 渲染剧本数据
    initializeFromProject,
    setAnyAttribute,
    applyScript
  } = useScriptService();

  const {
    videoSegments,
    getVideoSegmentList,
    setSelectedSegment,
    regenerateVideoSegment,
    filterRole,
    calculateRecognitionBoxes
  } = useShotService();

  const {
    roleList,
    selectedRole,
    userRoleLibrary,
    fetchRoleList,
    selectRole,
    fetchUserRoleLibrary,
    optimizeRoleText,
    updateRoleText,
    regenerateRole,
    uploadImageAndUpdateRole,
    saveRoleToLibrary,

  } = useRoleServiceHook();

  const {
    shotSelectionList,
    selectedRoleId,
    isAllVideoSegmentSelected,
    selectedVideoSegmentCount,
    fetchRoleShots,
    toggleSelectAllShots,
    toggleShotSelection,
    applyRoleToSelectedShots,
    clearShotSelection,
    setSelectedRole
  } = useRoleShotServiceHook(projectId, selectedRole || undefined);

  useEffect(() => {
    console.log('useEditData-----selectedRole', selectedRole);
    if(selectedRole){
      setSelectedRole(selectedRole);
    }

  }, [selectedRole]);

  useEffect(() => {
    if (tabType === 'script') {
      initializeFromProject(projectId, originalText || '').then(() => {
        setLoading(false);
      }).catch((err) => {
        console.log('useEditData-----err', err);
        setScriptData([]);
        setLoading(false);
      });
    } else if (tabType === 'shot') {
      getVideoSegmentList(projectId).then(() => {
        setLoading(false);
      }).catch((err) => {
        console.log('useEditData-----err', err);
        setShotData([]);
        setLoading(false);
      });
    } else if (tabType === 'role') {
      fetchRoleList(projectId).then(() => {
        setLoading(false);
      }).catch((err) => {
        console.log('useEditData-----err', err);
        setRoleData([]);
        setLoading(false);
      });
    }
  }, [tabType]);

  useEffect(() => {
    if (scriptBlocksMemo.length > 0) {
      setScriptData(scriptBlocksMemo);
    }
  }, [scriptBlocksMemo]);

  useEffect(() => {
    console.log('useEditData-----videoSegments', videoSegments);
    setShotData(videoSegments);
  }, [videoSegments]);

  useEffect(() => {
    setRoleData(roleList);
    // setRoleData(mockRoleData);
  }, [roleList]);

  return {
    loading,
    // script
    scriptData,
    setAnyAttribute,
    applyScript,
    // shot
    shotData,
    setSelectedSegment,
    regenerateVideoSegment,
    filterRole,
    calculateRecognitionBoxes,
    // role
    roleData,
    selectRole,
    selectedRole,
    userRoleLibrary,
    optimizeRoleText,
    updateRoleText,
    regenerateRole,
    fetchUserRoleLibrary,
    uploadImageAndUpdateRole,
    // role shot
    shotSelectionList,
    selectedRoleId,
    isAllVideoSegmentSelected,
    selectedVideoSegmentCount,
    fetchRoleShots,
    toggleSelectAllShots,
    toggleShotSelection,
    applyRoleToSelectedShots,
    clearShotSelection,
    saveRoleToLibrary
  }
}
