import http from '../../../http'
import { REP_LIST, DEL_ITEM, CHANGE_PAGING, UPDATE_ITEM } from './actionType'

export const updateItemAction = (dispatch: Function, item: Model): Model => {
  let citem = {...item}
  dispatch({
    type: UPDATE_ITEM,
    plyload: {
      oldItem: item,
      newItem: citem
    }
  })
  return citem
}

/**
 * 状态
 * 0：切片失败
 * 1：未解压
 * 2：未判断坐标
 * 3：未转geojson
 * 4：未切片
 * 5：切片完成
 * 6：切片中
 * 7: 坐标转换失败
 * 8: 服务已发布
 * 9：判断中
 * 10: 发布失败
 * 11: 修改样式
 * 12: 样式修改失败
 */
function getStateLocal(item: any) {
  const map : {[key: string]: Array<string>} = {
    '0': ['slicePath'],
    '1': ['coordStrictPath', 'uploadPath'],
    '2': ['coordStrictPath', 'unZipPath', 'uploadPath'],
    '3': ['coordStrictPath', 'unZipPath', 'uploadPath'],
    '4': ['geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '5': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '6': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '7': ['geojsonPath'],
    '8': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '9': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '10': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '11': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath'],
    '12': ['slicePath', 'geojsonPath', 'coordStrictPath', 'unZipPath', 'uploadPath']
  }

  let key = map[item.status].find(key => item[key])
  return key ? item[key] : ''
}


export const getListAction = async (dispatch: Function, url: string, current: number) => {
  let res = await http.post(url, { "pageNum": current - 1, "pageSize": 10})
  let list = res.data.data.content

  list = list.map((item: any) => ({
    ...item,
    fileUrl: getStateLocal(item)
  }))

  list.forEach((item: any) => {
    if (item.status === 6) {
      item.sectStep = sessionStorage.getItem(item.id) || 0
    }
  })

  dispatch({
    type: REP_LIST,
    plyload: list
  })

  dispatch({
    type: CHANGE_PAGING,
    plyload: {
      total: res.data.data.totalPages,
      number: res.data.data.totalElements,
      current: current
    }
  })
}

export const delItemAction = async (dispatch: Function, url: string, item: Model) => {
  await http.get(url)
  dispatch({
    type: DEL_ITEM,
    plyload: item
  })
}

export const zipItemAction = async (dispatch: Function, url: string, item: Model) => {
  let res = await http.get(url)
  return res.data
}

export const judgeItemAction = async (dispatch: Function, url: string, item: Model) => {
  let res = await http.get(url)
  return res.data
}

export const sectionItemAction = async (dispatch: Function, url: string, item: Model) => {
  let res = await http.get(url)
  return res.data
}

export const sectionStepAction = async (dispatch: Function, url: string, item: Model) => {
  let res = await http.get(url)
  if (res.data.data && res.data.data.progress) {
    sessionStorage.setItem(res.data.data.id, res.data.data.progress)
  }
  return res.data
}

export const transferItemAction = async (dispatch: Function, url: string, item: Model, body: any) => {
  let res = await http.post(url, body)
  return res.data
}
