
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";
import { VideoSegmentEntity } from "@/app/service/domain/Entities";

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<VideoSegmentEntity[]>([]);

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

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

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

  const {
    roleList,
    selectedRole,
    userRoleLibrary,
    fetchRoleList,
    selectRole,
    fetchUserRoleLibrary,
    optimizeRoleText,
    updateRoleText,
    regenerateRole,
    uploadImageAndUpdateRole,
    saveRoleToLibrary,
    changeTabCallback,
  } = useRoleServiceHook();

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

  useEffect(() => {
    console.log('useEditData-----selectedRole', selectedRole);
    if(selectedRole){
      setSelectedRole(selectedRole);
      setDraftRoleList(roleList || []);
    }

  }, [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, scriptRoles);
    setShotData(videoSegments);
    // setRoleData(scriptRoles);
  }, [videoSegments, scriptRoles]);

  useEffect(() => {
    console.log('useEditData-----roleList', roleList);
    setRoleData(roleList);
    // setRoleData(mockRoleData);
  }, [roleList]);

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