import { defineStore } from 'pinia'
import {
  fetchProject,
  uploadProjectBimScene,
  relaceProjectScenes,
  SceneType,
  deleteProject,
  finishProject,
  updateProject,
  deleteProjectBimScene,
  BimStatus,
  deleteProjectScene,
  updateProjectBimSceneName
} from '@/api'

import type {
  Project,
  Scene,
  BimUploadData,
  UpdateProjectData,
  Bim
} from '@/api'

export const BinType = Symbol('bin')
export const binTypeDesc = 'BIM'
export type { Project, BimUploadData, Bim }

export type ProjectScene = Omit<Scene, 'type' | 'num' | 'status'> &
  (
    | {
        type: Scene['type']
        status: Scene['status']
        num: Scene['num']
      }
    | {
        type: typeof BinType
        status: BimStatus
      }
  )
export type SelectTypeScenes = {
  [SceneType.SWKJ]: Scene['num'][]
  [SceneType.SWKK]: Scene['num'][]
  [SceneType.SWSS]: Scene['num'][]
}

type CurrentState = null | Project
export const useProject = defineStore('project', {
  state: () => ({
    current: null as CurrentState
  }),
  getters: {
    scenes: state => {
      const scenes: ProjectScene[] = state.current?.sceneList || []
      const bimData = state.current?.bimData
      if (bimData) {
        return [
          {
            id: bimData.bimId,
            name: bimData.bimName,
            title: bimData.bimName,
            sceneName: bimData.bimName,
            thumb: bimData.bimOssFilePath,
            createTime: bimData.createTime,
            type: BinType,
            status: bimData.bimStatus,
            phone: ''
          },
          ...scenes
        ]
      } else {
        return scenes
      }
    },
    isSelf: state => (id?: Project['projectId']) =>
      state.current?.projectId === id
  },
  actions: {
    async setCurrent(id: Project['projectId']) {
      this.current = await fetchProject(id)
    },
    async updateCurrent(id?: Project['projectId']) {
      if ((id = id || this.current?.projectId)) {
        this.isSelf(id) && (await this.setCurrent(id))
      }
    },
    async delete(id?: Project['projectId']) {
      if ((id = id || this.current?.projectId)) {
        await deleteProject(id)
        await this.updateCurrent(id)
      }
    },
    async update(data: PartialPart<UpdateProjectData, 'projectId'>) {
      const id = data.projectId || this.current?.projectId
      if (id) {
        await updateProject({
          ...data,
          projectId: id
        })
        await this.updateCurrent(id)
      }
    },
    async finish(id?: Project['projectId']) {
      if ((id = id || this.current?.projectId)) {
        await finishProject(id)
        await this.updateCurrent(id)
      }
    },
    async addBim(data: BimUploadData, id?: Project['projectId']) {
      if ((id = id || this.current?.projectId)) {
        await uploadProjectBimScene(id, data)
        await this.updateCurrent(id)
      }
    },
    async updateBimName(bimName: Bim['bimName'], bimId?: Bim['bimId']) {
      if ((bimId = bimId || this.current?.bimData?.bimId)) {
        await updateProjectBimSceneName({ bimId, bimName })
        this.current?.bimData?.bimId === bimId && (await this.updateCurrent())
      }
    },
    async deleteBim(bimId?: Bim['bimId']) {
      if ((bimId = bimId || this.current?.bimData?.bimId)) {
        await deleteProjectBimScene(bimId)
        this.current?.bimData?.bimId === bimId && (await this.updateCurrent())
      }
    },
    async replaceScenes(
      typeScenes: SelectTypeScenes,
      id?: Project['projectId']
    ) {
      if ((id = id || this.current?.projectId)) {
        const sceneNumParam = Object.entries(typeScenes).map(
          ([type, nums]) => ({
            type: Number(type) as SceneType,
            numList: nums
          })
        )
        await relaceProjectScenes({ id, sceneNumParam })
        await this.updateCurrent(id)
      }
    },
    async deleteScene(num: Scene['num'], id?: Project['projectId']) {
      if ((id = id || this.current?.projectId)) {
        await deleteProjectScene({ projectId: id, num })
        await this.updateCurrent(id)
      }
    }
  }
})

export {
  ProjectStatus,
  ProjectStatusDesc,
  BimStatus,
  BimStatusDesc
} from '@/api'
