import client from '../../app-shell/client'

import { setRoute } from './actions-route'
import { fetchBoardList } from './actions-board'

export const fetchPlayGroupList = () => async dispatch => {
  try {
    const data = await client.fetchPlayGroupList()

    dispatch({
      type: 'PLAYGROUP-LIST',
      list: data.playGroups
    })
  } catch (error) {
    dispatch({
      type: 'CLEAR-PLAYGROUP-LIST'
    })
  }
}

export const createPlayGroup = group => async dispatch => {
  try {
    const data = await client.createPlayGroup(group)

    dispatch(fetchPlayGroupList())
    dispatch(fetchBoardList('playGroup', data.createPlayGroup.id))
  } catch (error) {
    console.error(error)
  }
}

export const updatePlayGroup = group => async dispatch => {
  try {
    const data = await client.updatePlayGroup(group)

    dispatch(fetchPlayGroupList())
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}

export const deletePlayGroup = group => async dispatch => {
  try {
    var { id } = group

    const data = await client.deletePlayGroup(id)

    dispatch(fetchPlayGroupList())
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}

export const joinPlayGroup = (boardId, group) => async dispatch => {
  try {
    var { id } = group

    const data = await client.joinPlayGroup(boardId, id)

    dispatch(setRoute('list-by-playgroup', id))

    dispatch({
      type: 'BOARD-LIST',
      group: id,
      list: data.joinPlayGroup.boards
    })
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}

export const leavePlayGroup = (boardId, group) => async dispatch => {
  try {
    var { id } = group

    const data = await client.leavePlayGroup(boardId, id)

    dispatch(setRoute('list-by-playgroup', id))

    dispatch({
      type: 'BOARD-LIST',
      group: id,
      list: data.leavePlayGroup.boards
    })
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}
