import {
  PaggingReq,
  PaggingRes,
  axios,
  cancelUModel,
  delScene,
  deleteModel,
  getModelRunProgress,
  getModelSceneList,
  getSceneList,
  uploadModel,
} from "@/request";

interface BaseScene {
  title: string;
  time: string;
  type: SceneType;
  id: string;
}

export type QuoteScene = BaseScene & {
  hasSynchronized: boolean;
  sceneNum: string;
  sceneName: string;
  createTime: string;
  vrLink: string;
  num: string;
  snCode: string;
  viewCount: number;
  status: QuoteSceneStatus;
};

// 普通场景状态
export enum QuoteSceneStatus {
  DEL = -1,
  RUN = 0,
  ERR = 1,
  SUCCESS = 2,
  ARCHIVE = 3,
  RERUN = 4,
}

export interface ModelScene extends BaseScene {
  modelId: number;
  createStatus: ModelSceneStatus;
  modelSize: string;
  modelDateType: string;
  progress?: number;
  createTime: string;
}

export type Scene = QuoteScene | ModelScene;

export enum SceneType {
  SWKK,
  SWKJ,
  SWSS,
  SWMX,
  SWSSMX,
}

// 模型场景状态
export enum ModelSceneStatus {
  ERR = -1,
  RUN = 0,
  SUCCESS = 1,
  CANCEL = -2,
}

export const cancelUploadModelScene = (scene: ModelScene) =>
  axios.get(cancelUModel, { params: { modelId: scene.modelId } });

export const delModelScene = (scene: ModelScene) =>
  axios.post(deleteModel, { modelId: scene.modelId });

export const uploadModelScene = (
  file: File,
  progressCallback: (progress: number) => void
) =>
  axios<undefined>({
    method: "POST",
    url: uploadModel,
    data: { file },
    onUploadProgress(event: any) {
      progressCallback(Math.round((event.loaded / event.total) * 100) || 0);
    },
  });

export const getModelSceneStatus = async (scene: ModelScene) => {
  const progress = (
    await axios.get<number>(getModelRunProgress, {
      params: { modelId: scene.modelId },
    })
  ).data;

  return {
    progress,
    status:
      progress >= 100
        ? ModelSceneStatus.SUCCESS
        : progress < 0
        ? ModelSceneStatus.ERR
        : ModelSceneStatus.RUN,
  };
};

// *-----公用

type ScenePaggingParams = PaggingReq<
  Pick<BaseScene, "type"> & {
    modelTitle: string;
    sceneName: string;
    status?: number;
    caseId?: number;
    deptId: string;
    snCode: string;
  }
>;
export const getScenePagging = async (params: ScenePaggingParams) => {
  return (
    await axios.get(
      params.type === SceneType.SWMX ? getModelSceneList : getSceneList,
      {
        params,
      }
    )
  ).data as PaggingRes<Scene>;
};

export const delQuoteScene = (scene: QuoteScene) =>
  axios.get(delScene, { params: { num: scene.num } });
