import { confirm } from "@/helper/message";
import {
  PaggingReq,
  PaggingRes,
  axios,
  cancelUModel,
  checkGenMeshScene,
  delScene,
  deleteModel,
  downloadSceneList,
  genMeshSceneByCloud,
  getModelRunProgress,
  getModelSceneList,
  getSceneList,
  updateModelScene,
  uploadModel,
} from "@/request";
import { ElMessage } from "element-plus";

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

// 只有当location 为4 时，才能生成obj
export enum LocationEnum {
  Scene_Location_Slam, //slam\n" +
  Scene_Location_SFM, //sfm\n" +F
  Scene_Location_SFMAI, //SFM + AI\n" +
  Scene_Location_MutiFloor, //多楼层\n" +
  Scene_Location_PointCloud, //点云\n" +
  Scene_Location_SLAMPoint, //slam实时拍\n" +
  Scene_Location_SLAMPointAndSFMAI, //slam实时拍+站点\n" +
}

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

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

export interface ModelScene extends BaseScene {
  modelTitle: string;
  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,

  SWYDSS,
  SWYDMX,
}

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

export const setModelScene = (scene: ModelScene) =>
  axios.post(updateModelScene, {
    modelId: scene.modelId,
    modelTitle: scene.modelTitle,
  });

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 } });

export type QueryDownloadQuoteSceneParams = PaggingReq<{
  deptId: string;
  userName: string;
  nickName: string;
  createTime: string;
  sceneTitle: string;
  sceneNum: string;
  snCode: string;
}>;

export type DownloadQuoteSceneLog = {
  id: number;
  sceneNum: string;
  sceneTitle: string;
  userName: string;
  nickName: string;
  snCode: string;
  deptLevelStr: string;
  deptName: string;
  deptId: string;
  createTime: string;
};
export const getDownloadQuoteScene = async (
  params: Omit<QueryDownloadQuoteSceneParams, "createTime"> & {
    startCreateTime: string | null;
    endCreateTime: string | null;
  }
) =>
  (await axios.post(downloadSceneList, params))
    .data as PaggingRes<DownloadQuoteSceneLog>;

export const genMeshScene = async (scene: QuoteScene) => {
  const res = (await axios.post(checkGenMeshScene, { id: scene.id })).data;
  if (res?.buildObjStatus === 2) {
    ElMessage.error("Mesh场景正在计算中，请耐心等待。");
  } else if (
    res?.buildObjStatus !== 1 ||
    (await confirm("重新生成obj，Mesh场景将覆盖现有信息，确定要重新生成吗？"))
  ) {
    await axios.post(genMeshSceneByCloud, { id: scene.id });
    ElMessage.success("生成obj将同时生成Mesh场景，需要较长时间，请耐心等待；");
  }
};
