import {
  axios,
  caseInfo,
  caseSceneList,
  getCasePsw,
  repCaseScenes,
  setCasePsw,
  syncInfo,
  updateCaseFile,
} from "@/request";
import { ModelScene, QuoteScene, Scene, SceneType } from "./scene";
import { CaseFile } from "./caseFile";

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

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

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

export const getCaseInfo = async (caseId: number) =>
  (await axios.get<Case>(caseInfo, { params: { caseId } })).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 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 });
