import {
  axios,
  caseInfo,
  caseSceneList,
  getCasePsw,
  repCaseScenes,
  setCasePsw,
  syncInfo,
  updateCaseFile,
  caseInquestInfo,
  newFileupload,
  getDictFileList,
  getByTree,
  getListFileList,
  addByMediaLibrary,
  addByMediaLibrarys,
  updateFileType,
  saveOrUpdate,
  criminalInfo,
  getSceneList,
  newCaseInfo,
  casesaveOrUpdate,
  saveApiOrUpdate,
  uploadImagesAndSave,
  caseApiDel,
  caseApiList,
  caseApiUpdateSort,
  saveCaseImgTag,
  getCaseImgTag,
  ffmpegMergeImage,
  caseInquestInfoOld,
  caseInquestOpt,
  caseInquestExport,
  sceneListHasAi,
  caseaddOrUpdate,
} from "@/request";
import { router } from "@/router";
import { ModelScene, QuoteScene, Scene, SceneType } from "./scene";
import { CaseFile } from "./caseFile";
import { ref } from "vue";

export type Case = {
  caseId: number;
  caseTitle: string;
  createTime: string;
  name: string;
  tbStatus: string;
  userName: number;
};

export const treeList = ref([]);

export const setCaseSharePWD = (params: { caseId: number; randCode: string }) =>
  axios.post(setCasePsw, params);

export const setCaseaddOrUpdate = (params) =>
  axios.post(caseaddOrUpdate, params);

export const getCaseSharePWD = async (params: { caseId: number }) =>
  (await axios.get<string>(getCasePsw, { params })).data;

export const addByMediaLiBrary = async (params) =>{
  const newUrl = params.uploadIds?.length ? addByMediaLibrarys : addByMediaLibrary
  return (await axios.post<string>(newUrl, params)).data;
}

export const AddsaveOrUpdate = async (params) =>
  (await axios.post<string>(saveOrUpdate, params)).data;

export const getcaseInDate = async (params) =>
  (await axios.get<string>(criminalInfo, {params})).data;
  // (await axios.get<string>(info, { params:params })).data;

export const getSceneListData = async (params) =>
  (await axios.get<string>(getSceneList, { params })).data;
  // (await axios.get<string>(info, { params:params })).data;

export const getCaseInfo = async (caseId) =>
  (await axios.get<Case>(caseInfo, { params: { caseId } })).data;

export const getCaseInquestInfo = async (caseId: number) =>
  (await axios.get(newCaseInfo, { params: { caseId } })).data;

export const casesaveOrUpDate = async (params) =>
  (await axios.post<string>(casesaveOrUpdate, params)).data;


export const updateCaseInfo = async (caseFile: CaseFile) =>
  await axios.post(updateCaseFile, {
    filesId: caseFile.filesId,
    filesTitle: caseFile.filesTitle,
  });

export const getCaseSceneList = async (caseId: number): Promise<Scene[]> => {
  return (await axios.get(caseSceneList, { params: { caseId } })).data;
};

export const getFilepageList = async (params): Promise<Scene[]> => {
  return (await axios.post(getListFileList, params)).data;
};
export const getupdateFileType = async (params): Promise<Scene[]> => {
  return (await axios.post(updateFileType, params)).data;
};

export const getDictFileLists = async (type: number): Promise<Scene[]> => {
  return (await axios.get(getDictFileList, { params: { type: type } })).data;
};

export const updateByTreeFileLists = async (caseId = router.currentRoute.value?.params?.caseId): Promise<Scene[]> => {
  let list = (await axios.get(getByTree, { params: { caseId:caseId } })).data
  treeList.value = list;
  return list;
};
export const updateSelectByTreeFileLists = async (): Promise<Scene[]> => {
  function getTreeList(list: any[]) {
    return list.map(item => {
      if (item.childrenList) {
        return {
          value: item.filesTypeId,
          label: item.filesTypeName,
          children: getTreeList(item.childrenList)
        };
      }
      return {
        value: item.filesTypeId,
        label: item.filesTypeName,
      }
    })
  }
  return getTreeList((await axios.get(getByTree, { params: { caseId: '' } })).data)
};
export const caseImgList = (caseId: number, orderBy: string | null) =>
  axios.post(caseApiList, { orderBy: orderBy || "", caseId });

export const caseDel = (id: number) => axios.post(caseApiDel, { id });

export const saveOrUpDate = (params: CaseImg) =>
  axios.post(saveApiOrUpdate, { ...params });

export const caseUpdateSort = (list: [CaseImg]) =>
  axios.post(caseApiUpdateSort, { paramList: list });

export const saveOrAndSave = (params) =>
  axios.post(uploadImagesAndSave, { ...params });

export const getByTreeFileLists = async () => {
  return treeList.value
};

export const getCaseInquestInfoOld = (caseId: number) =>
  axios.get(caseInquestInfoOld, { params: { inquestFileId: caseId } });

export const saveCaseInquestInfo = (params) =>
  axios.post(caseInquestOpt, params
  );

export const exportCaseInquestInfo = (caseId: number) =>
  axios.get(caseInquestExport, {
    params: { inquestFileId: caseId, ingoreRes: true },
    responseType: "blob",
  });

export const getSyncSceneInfo = async (caseId: number) => {
  return (await axios.post<string>(syncInfo, { caseId })).data;
};

export const getSceneKey = (scene: Scene) =>
  scene.type === SceneType.SWMX
    ? (scene as ModelScene).modelId
    : (scene as QuoteScene).num;

export type CaseScenes = { type: SceneType; numList: (string | number)[] }[];
export const getCaseScenes = (scenes: Scene[]) => {
  const typeIdents = [
    { type: SceneType.SWKJ, numList: [] },
    { type: SceneType.SWKK, numList: [] },
    { type: SceneType.SWMX, numList: [] },
    { type: SceneType.SWSS, numList: [] },
    { type: SceneType.SWSSMX, numList: [] },
    { type: SceneType.SWYDSS, numList: [] },
    { type: SceneType.SWYDMX, numList: [] },
  ] as CaseScenes;

  for (const scene of scenes) {
    const typeIdent = typeIdents.find((ident) => ident.type === scene.type)!;
    typeIdent.numList.push(getSceneKey(scene));
  }
  return typeIdents;
};

export const replaceCaseScenes = (caseId: number, caseScenes: CaseScenes) =>
  axios.post(repCaseScenes, { sceneNumParam: caseScenes, caseId });

export const uploadNewFile = (data) => {
  console.log('uploadNewFile',data);
  return axios<undefined>({
    method: "POST",
    url: newFileupload,
    data: data,
    // onUploadProgress(event: any) {
    //   data.onProgress(Math.round((event.loaded / event.total) * 100) || 0);
    // },
  });
}

export type CaseImg = {
  id: number;
  caseId: number;
  imgInfo: string | null;
  imgUrl: string | null;
  status: number | null;
  sort: number | null;
};



export const saveCaseImgTagData = (params: any) =>
  axios.post(saveCaseImgTag, { ...params });

export const getCaseImgTagData = (caseId: number) =>
  axios.get(getCaseImgTag, { params: { caseId } });

export const getSceneListHasAi = (caseId: number) =>
  axios.get(sceneListHasAi, { params: { caseId } });


export const submitMergePhotos = (data) => axios.post(ffmpegMergeImage, { ...data })

