import axios from 'axios'
import util from './util'
import config from '../constants/config'

const request = axios.create({
  baseURL: ''
})

export const getJSON = url =>
  request({
    method: 'GET',
    url: url
  })

export const sceneList = ownerId =>
  request({
    method: 'GET',
    url: `${util.getAPI('scenes')}/${ownerId}`
  })

export const sceneLoad = (ownerId, sceneId, draft) =>
  request({
    method: 'GET',
    url: `${util.getAPI('scenes')}/${ownerId}/${sceneId}${draft ? '.draft' : ''}`
  })

export const sceneCreate = (ownerId, config) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('scenes')}/${ownerId}/${util.generateId()}.draft`,
    data: JSON.stringify(config),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const sceneCopy = (ownerId, sceneId, config) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('scenes')}/${ownerId}/${sceneId}`,
    data: JSON.stringify(config),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const sceneSave = (ownerId, config) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('scenes')}/${ownerId}/${config.sceneId}`,
    data: JSON.stringify(config),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const sceneUpdate = (ownerId, sceneId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('scenes')}/${ownerId}/${sceneId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const sceneDelete = (ownerId, sceneId, draft) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('scenes')}/${ownerId}/${sceneId}${draft ? '.draft' : ''}`
  })

export const assetList = ownerId =>
  request({
    method: 'GET',
    url: `${util.getAPI('assets')}/${ownerId}`
  })

export const assetCreate = (ownerId, file, name) => {
  let requestOption = {
    method: 'POST',
    url: `${util.getAPI('assets')}/${ownerId}`,
  }
  var formData = new FormData()
  formData.append('file', file)
  formData.append('name', name)
  formData.append('metadata', '')
  requestOption.data = formData
  return request(requestOption)
}

export const assetReplace = (ownerId, file, name, assetId) => {
  let requestOption = {
    method: 'PUT',
    url: `${util.getAPI('assets')}/${ownerId}/${assetId}`,
  }
  var formData = new FormData()
  formData.append('file', file)
  formData.append('name', name)
  formData.append('metadata', '')
  requestOption.data = formData
  return request(requestOption)
}

export const assetDelete = (ownerId, assetId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('assets')}/${ownerId}/${assetId}`
  })

export const assetUpdate = (ownerId, assetId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('assets')}/${ownerId}/${assetId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const getMapList = ownerId =>
  request({
    method: 'GET',
    url: `${util.getAPI('maps')}/${ownerId}`
  })

export const mapLoad = (ownerId, mapId, draft, cache) =>
  request({
    method: 'GET',
    url: `${util.getAPI('maps')}/${ownerId}/${mapId}${draft ? '.draft' : ''}`,
    cache: cache || cache === undefined ? true : false
  })

export const mapCreate = (ownerId, map) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('maps')}/${ownerId}/${util.generateId()}.draft`,
    data: JSON.stringify(map),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const mapReplace = (ownerId, mapId, map) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('maps')}/${ownerId}/${mapId}`,
    data: JSON.stringify(map),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const mapUpdate = (ownerId, mapId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('maps')}/${ownerId}/${mapId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const mapDelete = (ownerId, mapId, draft) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('maps')}/${ownerId}/${mapId}${draft ? '.draft' : ''}`
  })

export const getMapConfig = (url) =>
  request({
    method: 'GET',
    url: url
  })

export const getStyleList = ownerId =>
  request({
    method: 'GET',
    url: `${util.getAPI('styles')}/${ownerId}`
  })

export const styleLoad = (ownerId, styleId, draft, cache) =>
  request({
    method: 'GET',
    url: `${util.getAPI('styles')}/${ownerId}/${styleId}${draft ? '.draft' : ''}`,
    cache: cache || cache === undefined ? true : false
  })

export const styleCreate = (ownerId, style) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('styles')}/${ownerId}/${util.generateId()}.draft`,
    data: JSON.stringify(style),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const styleReplace = (ownerId, styleId, style) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('styles')}/${ownerId}/${styleId}`,
    data: JSON.stringify(style),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const styleUpdate = (ownerId, styleId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('styles')}/${ownerId}/${styleId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const styleDelete = (ownerId, styleId, draft) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('styles')}/${ownerId}/${styleId}${draft ? '.draft' : ''}`
  })

export const tilesetsList = (ownerId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('tilesets')}/${ownerId}`
  })

export const tilesetDelete = (ownerId, tilesetId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('tilesets')}/${ownerId}/${tilesetId}`
  })

export const tilesetCreate = (ownerId, tilesetId, file, name) => {
  let requestOption = {
    method: 'PUT',
    url: `${util.getAPI('tilesets')}/${ownerId}/${tilesetId}`,
  }
  var formData = new FormData()
  formData.append('file', file)
  formData.append('name', name || file.name)
  formData.append('metadata', '')
  requestOption.data = formData
  return request(requestOption)
}

export const tilesetLoad = (ownerId, tilesetId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('tilesets')}/${ownerId}/${tilesetId}`
  })

export const tilesetUpdate = (ownerId, tilesetId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('tilesets')}/${ownerId}/${tilesetId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

// tilejson
export const tilesetDataLoad = (ownerId, tilesetId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('tilesets')}/${ownerId}/${tilesetId}/tilejson`
  })

export const spriteDuplicate = (ownerId, spriteId, srcSpriteId) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('sprites')}/${ownerId}/${spriteId}/icons`,
    cache: false,
    data: JSON.stringify({
      srcOwner: ownerId,
      srcSpriteId: srcSpriteId
    }),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const spriteDelete = (ownerId, styleId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('sprites')}/${ownerId}/${styleId}`,
    cache: false
  })

export const tableLoad = (ownerId, tableId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('tables')}/${ownerId}/${tableId}`
  })

export const tableCreate = (ownerId, tableId, options) => {
  let requestOption = {
    method: 'PUT',
    url: `${util.getAPI('tables')}/${ownerId}/${tableId}`
  }
  if (options.type === 'file') {
    var formData = new FormData()
    formData.append('file', options.data)
    requestOption.data = formData
  } else {
    requestOption.data = JSON.stringify(options.data)
    requestOption.headers = {
      'Content-Type': 'application/json'
    }
  }
  return request(requestOption)
}

export const getTableFields = (ownerId, tableId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('tables')}/${ownerId}/${tableId}/columns`
  })

export const tableDataLoad = (ownerId, tableId, params) =>
  request({
    method: 'GET',
    url: `${util.getAPI('tables')}/${ownerId}/${tableId}/data`,
    params
  })

export const tableUpdate = (ownerId, tableId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('tables')}/${ownerId}/${tableId}`,
    data: JSON.stringify(data)
  })

export const tableDelete = (ownerId, tableId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('tables')}/${ownerId}/${tableId}`
  })

/* database*/
export const getDatabaseList = ownerId =>
  request({
    method: 'GET',
    url: `${util.getAPI('databases')}/${ownerId}`
  })

export const databaseLoad = (ownerId, databaseId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('databases')}/${ownerId}/${databaseId}`
  })

export const getDatabaseTablesList = (ownerId, databaseId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('databases')}/${ownerId}/${databaseId}/tables`
  })

export const databaseUpdate = (ownerId, databaseId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('databases')}/${ownerId}/${databaseId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const databaseTest = (data) =>
  request({
    method: 'POST',
    url: `${util.getAPI('databases')}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const databaseCreate = (ownerId, data) =>
  request({
    method: 'POST',
    url: `${util.getAPI('databases')}/${ownerId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const databaseDelete = (ownerId, databaseId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('databases')}/${ownerId}/${databaseId}`
  })

/* catalog */
export const getCatalogList = (ownerId, parentId) => {
  let url = `${util.getAPI('catalogs')}/${ownerId}`
  if (parentId) {
    url += `?parentId=${parentId}`
  }
  return request({
    method: 'GET',
    url
  })
}

export const catalogUpdate = (ownerId, catalogId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('catalogs')}/${ownerId}/${catalogId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const catalogCreate = (ownerId, catalogId, data) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('catalogs')}/${ownerId}/${catalogId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const catalogDelete = (ownerId, catalogId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('catalogs')}/${ownerId}/${catalogId}`
  })

/* model */
export const modelLoad = (ownerId, modelId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('models')}/${ownerId}/${modelId}`
  })

export const getModelList = (ownerId) =>
  request({
    method: 'GET',
    url: `${util.getAPI('models')}/${ownerId}`
  })

export const modelCreate = (ownerId, modelId, data) =>
  request({
    method: 'PUT',
    url: `${util.getAPI('models')}/${ownerId}/${modelId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const modelUpdate = (ownerId, modelId, data) =>
  request({
    method: 'PATCH',
    url: `${util.getAPI('models')}/${ownerId}/${modelId}`,
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })

export const modelDelete = (ownerId, modelId) =>
  request({
    method: 'DELETE',
    url: `${util.getAPI('models')}/${ownerId}/${modelId}`
  })