import gql from 'graphql-tag'
import client from './fetch-client'
import { setRoute } from './actions-route'
import { fetchBoardList } from './actions-board'

export const fetchPlayGroupList = () => async dispatch => {
  try {
    const response = await client.query({
      query: gql`
        {
          playGroups {
            id
            name
            description
          }
        }
      `
    })

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

export const createPlayGroup = group => async dispatch => {
  try {
    var { name, description } = group

    const response = await client.mutate({
      mutation: gql`
        mutation CreatePlayGroup($playGroup: NewPlayGroup!) {
          createPlayGroup(playGroup: $playGroup) {
            id
            name
            description
            createdAt
            updatedAt
          }
        }
      `,
      variables: {
        playGroup: { name, description }
      }
    })

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

export const updatePlayGroup = group => async dispatch => {
  try {
    var { id, name, description } = group

    const response = await client.mutate({
      mutation: gql`
        mutation UpdatePlayGroup($id: String!, $patch: PlayGroupPatch!) {
          updatePlayGroup(id: $id, patch: $patch) {
            id
            name
            description
            createdAt
            updatedAt
          }
        }
      `,
      variables: {
        id,
        patch: { name, description }
      }
    })

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

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

    const response = await client.mutate({
      mutation: gql`
        mutation($id: String!) {
          deletePlayGroup(id: $id) {
            id
          }
        }
      `,
      variables: {
        id
      }
    })

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

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

    const response = await client.mutate({
      mutation: gql`
        mutation JoinPlayGroup($id: String!, $boardIds: [String]!) {
          joinPlayGroup(id: $id, boardIds: $boardIds) {
            id
            name
            description
            boards {
              id
              name
              description
              createdAt
              updatedAt
            }
            createdAt
            updatedAt
          }
        }
      `,
      variables: {
        id,
        boardIds: [boardId]
      }
    })

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

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

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

    const response = await client.mutate({
      mutation: gql`
        mutation($id: String!, $boardIds: [String]!) {
          leavePlayGroup(id: $id, boardIds: $boardIds) {
            id
            name
            description
            boards {
              id
              name
              description
              createdAt
              updatedAt
            }
            createdAt
            updatedAt
          }
        }
      `,
      variables: {
        id,
        boardIds: [boardId]
      }
    })

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

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