import 'whatwg-fetch'
import { bindActionCreators } from 'redux'
import api, { tokenName } from 'actions-api'
import uuidv1 from 'uuid/v1'

export const actionTypes = {
  LOGIN:'LOGIN',
  LOGOUT:'LOGOUT',
  TOGGLE_LOGIN:'TOGGLE_LOGIN',
  FORM_UPDATE_VALUE: 'FORM_UPDATE_VALUE',
  FORM_RESET: 'FORM_RESET',
  LOADING:'LOADING',
  FINISHED_LOADING:'FINISHED_LOADING',
  ADD_ERROR:'ADD_ERROR',
  REMOVE_ERROR:'REMOVE_ERROR',
  FORM_ERROR:'FORM_ERROR',
  ADMIN_POST_UPDATE_VALUE: 'ADMIN_POST_UPDATE_VALUE',
  ADMIN_POST_FORM_RESET: 'ADMIN_POST_FORM_RESET',
  ADMIN_POST_FORM_ERROR: 'ADMIN_POST_FORM_ERROR',
  GET_FEED: 'GET_FEED',
}

// Helpers
const addErrors = messages => dispatch => {
  const uuid = uuidv1()
  dispatch({ type:actionTypes.ADD_ERROR, payload:messages.map(message => { return { message, uuid } }) })
  setTimeout(() => dispatch({ type:actionTypes.REMOVE_ERROR, payload:uuid }), 3000);
}

const handleError = type => ({ messages, form }) => dispatch => {
  dispatch({ type:actionTypes.FINISHED_LOADING })
  if(messages) addErrors(messages)(dispatch)
  if(form) dispatch({ type, payload:form })
  if(!messages && !form) addErrors(['Woops something is broken. Please come back later and try again.'])(dispatch)
}

const handleFormError = handleError(actionTypes.FORM_ERROR)
const handleAdminError = handleError(actionTypes.ADMIN_POST_FORM_ERROR)




// Actions

const authenticate = dispatch => res => {
  localStorage.setItem(tokenName, res.token)
  dispatch({ type:actionTypes.FINISHED_LOADING })
  dispatch({ type:actionTypes.LOGIN, payload:res })
}

const login = user => dispatch => {
  dispatch({ type:actionTypes.LOADING })
  return api.login(user)
    .then(authenticate(dispatch))
    .catch(e => handleFormError(e)(dispatch))
}

const signup = user => dispatch => {
  dispatch({ type:actionTypes.LOADING })
  return api.signup(user)
    .then(authenticate(dispatch))
    .catch(e => handleFormError(e)(dispatch))
}

const logout = () => dispatch => {
  localStorage.removeItem(tokenName)
  return dispatch({ type:actionTypes.FORM_RESET })
}

const toggleLogin = () => dispatch => {
  return dispatch({ type:actionTypes.TOGGLE_LOGIN })
}

const checkToken = isEmpty => dispatch => {
  let hasError = false
  if(localStorage.getItem(tokenName) && isEmpty) {
    api.getUser().then(res => {
      if(res.status >=300) hasError=true;
      return res.json()
    })
    .then(res => {
      if(!hasError){
        dispatch({ type:actionTypes.LOGIN, payload:res })
      }
    })
  }
}

// const getPosts = () => dispatch => api.http('/api/posts', "GET")
//   .then(res => dispatch({ type:actionTypes.GET_FEED, payload:res.data }))
//   .catch(e => addErrors(e.messages)(dispatch))
//
// const removePost = post => dispatch => api.http(`/api/posts/${post.id}`, 'DELETE')
//   .then(res => getPosts()(dispatch))
//   .catch(e => addErrors(e.messages)(dispatch))


export const actions = (dispatch) => {
  return {
    dispatch,
    getPosts:bindActionCreators(getPosts, dispatch),
    removePost:bindActionCreators(removePost, dispatch),
    checkToken:bindActionCreators(checkToken, dispatch),
    signup:bindActionCreators(login, dispatch),
    login:bindActionCreators(login, dispatch),
    signup:bindActionCreators(signup, dispatch),
    logout:bindActionCreators(logout, dispatch),
    toggleLogin:bindActionCreators(toggleLogin, dispatch),
    addErrors:bindActionCreators(addErrors, dispatch),
    handleAdminError:bindActionCreators(handleAdminError, dispatch),
  }
}
