import { FETCH_CONFIG } from './helper.js'
import {
  TIMEZONE,
  setTimezone,
  USER_LOGIN,
  USER_LOGOUT,
  USER_ROLE,
  USER_CONNECTED,
  USER_KNOWN_MEMBER_LIST,
  setUserRole,
  WORKSPACE,
  WORKSPACE_LIST,
  WORKSPACE_DETAIL,
  WORKSPACE_MEMBER_LIST,
  WORKSPACE_MEMBER_ADD,
  FOLDER,
  setFolderData,
  APP_LIST,
  CONTENT_TYPE_LIST,
  WORKSPACE_CONTENT_ARCHIVED,
  WORKSPACE_CONTENT_DELETED,
  WORKSPACE_RECENT_ACTIVITY,
  WORKSPACE_READ_STATUS
} from './action-creator.sync.js'

/*
 * fetchWrapper(obj)
 *
 * Params:
 *   An Object with the following attributes :
 *     url - string - url of the end point to call
 *     param - object - param to send with fetch call (eg. header)
 *       param.method - string - REQUIRED - method of the http call
 *     actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
 *     dispatch - func - redux dispatcher function
 *
 * Returns:
 *   An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
 *
 * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
 * It also adds, to the Response of the fetch request, the json value so that the redux action have access to the status and the data
 */
// Côme - 2018/08/02 - fetchWrapper should come from tracim_lib so that all apps uses the same
const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
  dispatch({type: `${param.method}/${actionName}/PENDING`})

  const fetchResult = await fetch(url, param)
  fetchResult.json = await (async () => {
    switch (fetchResult.status) {
      case 200:
      case 304:
        return fetchResult.json()
      case 204:
      case 400:
      case 404:
      case 409:
      case 500:
      case 501:
      case 502:
      case 503:
      case 504:
        return '' // @TODO : handle errors
    }
  })()
  if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)

  // if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  // else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  switch (fetchResult.status) {
    case 200:
    case 204:
    case 304:
      dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
      break
    case 400:
    case 404:
    case 500:
      dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
      break
  }

  return fetchResult
}

export const getTimezone = () => async dispatch => {
  const fetchGetTimezone = await fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/timezone`,
    param: {
      headers: {...FETCH_CONFIG.headers},
      method: 'GET'
    },
    actionName: TIMEZONE,
    dispatch
  })
  if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
}

export const postUserLogin = (login, password, rememberMe) => async dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/sessions/login`, // FETCH_CONFIG.apiUrl
    param: {
      headers: {...FETCH_CONFIG.headers},
      method: 'POST',
      body: JSON.stringify({
        email: login,
        password: password
        // remember_me: rememberMe
      })
    },
    actionName: USER_LOGIN,
    dispatch
  })
}

export const postUserLogout = () => async dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/sessions/logout`, // FETCH_CONFIG.apiUrl
    param: {
      headers: {...FETCH_CONFIG.headers},
      method: 'POST'
    },
    actionName: USER_LOGOUT,
    dispatch
  })
}

export const getUserIsConnected = user => async dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: USER_CONNECTED,
    dispatch
  })
}

export const getUserRole = user => async dispatch => {
  const fetchGetUserRole = await fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/roles`,
    param: {
      headers: {...FETCH_CONFIG.headers},
      method: 'GET'
    },
    actionName: USER_ROLE,
    dispatch
  })
  if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
}

export const getUserKnownMember = (user, userNameToSearch) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/known_members?acp=${userNameToSearch}`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: USER_KNOWN_MEMBER_LIST,
    dispatch
  })
}

export const putUserWorkspaceRead = (user, idWorkspace) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/read`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'PUT'
    },
    actionName: USER_KNOWN_MEMBER_LIST,
    dispatch
  })
}

export const getWorkspaceList = user => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: WORKSPACE_LIST,
    dispatch
  })
}

export const getWorkspaceDetail = (user, idWorkspace) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: WORKSPACE_DETAIL,
    dispatch
  })
}

export const getWorkspaceMemberList = (user, idWorkspace) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: WORKSPACE_MEMBER_LIST,
    dispatch
  })
}

export const getWorkspaceContentList = (user, idWorkspace, idParent) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents?parent_id=${idParent}`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: WORKSPACE,
    dispatch
  })
}

export const getWorkspaceRecentActivityList = (user, idWorkspace, beforeId = null) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/contents/recently_active?limit=10${beforeId ? `&before_content_id=${beforeId}` : ''}`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: WORKSPACE_RECENT_ACTIVITY,
    dispatch
  })
}

export const getWorkspaceReadStatusList = (user, idWorkspace) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/contents/read_status`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: WORKSPACE_READ_STATUS,
    dispatch
  })
}

export const postWorkspaceMember = (user, idWorkspace, newMember) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'POST',
      body: JSON.stringify({
        user_id: newMember.id,
        user_email_or_public_name: newMember.name,
        role: newMember.role
      })
    },
    actionName: WORKSPACE_MEMBER_ADD,
    dispatch
  })
}

export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
  const fetchGetFolderContent = await fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
    param: {
      headers: {...FETCH_CONFIG.headers},
      method: 'GET'
    },
    actionName: `${WORKSPACE}/${FOLDER}`,
    dispatch
  })
  if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
}

export const getAppList = user => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/system/applications`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: APP_LIST,
    dispatch
  })
}

export const getContentTypeList = user => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'GET'
    },
    actionName: CONTENT_TYPE_LIST,
    dispatch
  })
}

export const putWorkspaceContentArchived = (user, idWorkspace, idContent) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/archive`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'PUT'
    },
    actionName: WORKSPACE_CONTENT_ARCHIVED,
    dispatch
  })
}

export const putWorkspaceContentDeleted = (user, idWorkspace, idContent) => dispatch => {
  return fetchWrapper({
    url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/delete`,
    param: {
      headers: {
        ...FETCH_CONFIG.headers,
        'Authorization': 'Basic ' + user.auth
      },
      method: 'PUT'
    },
    actionName: WORKSPACE_CONTENT_DELETED,
    dispatch
  })
}
