import axios from './instance'
import { jsonToForm } from '@/shared'
import { SceneType } from './scene'
import {
  GET_PROJECT_LIST,
  GET_PROJECT,
  UPLOAD_BIM,
  ADD_PROJECT,
  REPLACE_SCENES,
  FINE_PROJECT,
  DEL_PROJECT,
  UPDATE_PROJECT,
  DEL_BIM,
  DEL_SCENE,
  UPDATE_BIM_NAME
} from './constant'

import type { PageResult, PageRequest, Scenes, Scene } from './'
import { ui18n } from '@/lang'

export enum ProjectStatus {
  undone,
  done
}

export const ProjectStatusDesc = {
  [ProjectStatus.done]: ui18n.t('project.desc.done'),
  [ProjectStatus.undone]: ui18n.t('project.desc.undone')
}

export interface SimpleProject {
  projectId: number
  projectName: string
  projectMsg: string
  projectImg: string
  projectCreater: string
  bimId: string
  panos: string
  createTime: string
  updateTime: string
  projectStatus: ProjectStatus
}

export enum BimStatus {
  error = 'ERROR',
  done = 'DONE',
  upload = 'UPLOAD',
  transfrom = 'TRANSLATE',
  offline = 'OFFLINE'
}

export const BimStatusDesc = {
  [BimStatus.error]: ui18n.t('project.bimDesc.error'),
  [BimStatus.done]: ui18n.t('project.bimDesc.done'),
  [BimStatus.upload]: ui18n.t('project.bimDesc.upload'),
  [BimStatus.transfrom]: ui18n.t('project.bimDesc.transfrom'),
  [BimStatus.offline]: ui18n.t('project.bimDesc.offline')
}

export type Bim = {
  bimId: number
  bimLocalFilePath: string
  userName: string
  bimName: string
  bimOssFilePath: string
  bimServiceId: null
  bimStatus: BimStatus
  createTime: string
}

export type Project = SimpleProject & {
  sceneList: Scenes
  bimData?: Bim
}

export type SimpleProjects = SimpleProject[]

export const fetchProjects = async (filter: PageRequest) => {
  return axios.post<PageResult<SimpleProject>>(GET_PROJECT_LIST, filter)
}

export const fetchProject = async (id: Project['projectId']) => {
  return axios.get<Project>(GET_PROJECT, {
    params: { projectId: id }
  })
}

export type InsertProjectData = PartialPart<
  Pick<Project, 'projectImg' | 'projectName' | 'projectMsg'>,
  'projectImg'
>
export const insertProject = (data: InsertProjectData) =>
  axios.post<Project>(ADD_PROJECT, data)

export type UpdateProjectData = InsertProjectData & Pick<Project, 'projectId'>

export const updateProject = (data: UpdateProjectData) =>
  axios.post<Project>(UPDATE_PROJECT, data)

export const finishProject = (id: Project['projectId']) =>
  axios.post<Project>(FINE_PROJECT, { projectId: id })

export const deleteProject = (id: Project['projectId']) =>
  axios.post<Project>(DEL_PROJECT, { projectId: id })

export type BimUploadData = {
  name: string
  file: File
}
export const uploadProjectBimScene = async (
  id: Project['projectId'],
  data: BimUploadData
) => {
  await axios({
    url: UPLOAD_BIM,
    method: 'POST',
    data: jsonToForm({
      projectId: id,
      file: data.file,
      projectName: data.name
    }),
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const deleteProjectBimScene = async (bimId: Bim['bimId']) =>
  axios.post(DEL_BIM, { bimId })

export const updateProjectBimSceneName = async (
  data: Pick<Bim, 'bimId' | 'bimName'>
) => axios.post(UPDATE_BIM_NAME, data)

export const deleteProjectScene = async (data: {
  projectId: Project['projectId']
  num: Scene['num']
}) => axios.post(DEL_SCENE, data)

export const relaceProjectScenes = async (data: {
  id: Project['projectId']
  sceneNumParam: {
    type: SceneType
    numList: Scene['num'][]
  }[]
}) =>
  axios.post(REPLACE_SCENES, {
    projectId: data.id,
    sceneNumParam: data.sceneNumParam
  })
