import {
  BONUS_RANKS_LIST,
  MEMBER_DASHBOARD,
  MEMBER_DASHBOARD_MENTOR_BONUS_DETAIL,
  MEMBER_DASHBOARD_TEAM_BONUS_DETAIL,
  MEMBER_DASHBOARD_WELCOME_BONUS_DETAIL,
  MEMBER_DASHBOARD_BA_INACTIVE_DETAIL,
} from '../../utils/apiUrls'
import { get, post } from '../../utils/api'
import {
  PROCESSING_FINISHED,
  PROCESSING_STARTED,
  SET_MEMBER_DASHBOARD,
  SET_TEAM_BONUS_RANKS,
  SET_MEMBER_DASHBOARD_MENTOR_BONUS_DETAIL,
  SET_MEMBER_DASHBOARD_TEAM_BONUS_DETAIL,
  SET_MEMBER_DASHBOARD_WELCOME_BONUS_DETAIL,
  SET_MEMBER_DASHBOARD_BA_INACTIVE_DETAIL,
} from '../../utils/types'

export const loadTeamBonusRanks = ({ commit, dispatch }) => {
  commit(PROCESSING_STARTED, 'teamBonusRanksLoading')
  dispatch('removeError', 'team_bonus_ranks_loading')
  return post(BONUS_RANKS_LIST, { limit: 0, sort: 'id', order: 'asc' })
    .then(response => {
      commit(PROCESSING_FINISHED, 'teamBonusRanksLoading')
      commit(SET_TEAM_BONUS_RANKS, response.data.data)
    })
    .catch(e => {
      commit(PROCESSING_FINISHED, 'teamBonusRanksLoading')
      dispatch('logError', { team_bonus_ranks_loading: e })
    })
}

export const loadMemberDashboard = ({ commit, dispatch }) => {
  commit(PROCESSING_STARTED, 'memberDashboardLoading')
  dispatch('removeError', 'member_dashboard_loading')
  return get(MEMBER_DASHBOARD)
    .then(response => {
      commit(PROCESSING_FINISHED, 'memberDashboardLoading')
      commit(SET_MEMBER_DASHBOARD, response.data)
    })
    .catch(e => {
      commit(PROCESSING_FINISHED, 'memberDashboardLoading')
      dispatch('logError', { member_dashboard_loading: e })
    })
}

export const loadMentorBonusDetail = ({ commit, dispatch }, level) => {
  commit(PROCESSING_STARTED, 'mentorBonusDetailLoading')
  dispatch('removeError', 'mentor_bonus_detail_loading')
  return get(MEMBER_DASHBOARD_MENTOR_BONUS_DETAIL + '/' + level)
    .then(response => {
      commit(PROCESSING_FINISHED, 'mentorBonusDetailLoading')
      commit(SET_MEMBER_DASHBOARD_MENTOR_BONUS_DETAIL, response.data)
    })
    .catch(e => {
      commit(PROCESSING_FINISHED, 'mentorBonusDetailLoading')
      commit(SET_MEMBER_DASHBOARD_MENTOR_BONUS_DETAIL, [])
      dispatch('logError', { mentor_bonus_detail_loading: e })
    })
}

export const loadTeamBonusDetail = ({ commit, dispatch }) => {
  commit(PROCESSING_STARTED, 'teamBonusDetailLoading')
  dispatch('removeError', 'team_bonus_detail_loading')
  return get(MEMBER_DASHBOARD_TEAM_BONUS_DETAIL)
    .then(response => {
      commit(PROCESSING_FINISHED, 'teamBonusDetailLoading')
      commit(SET_MEMBER_DASHBOARD_TEAM_BONUS_DETAIL, response.data)
    })
    .catch(e => {
      commit(PROCESSING_FINISHED, 'teamBonusDetailLoading')
      commit(SET_MEMBER_DASHBOARD_TEAM_BONUS_DETAIL, [])
      dispatch('logError', { team_bonus_detail_loading: e })
    })
}

export const loadWelcomeBonusDetail = ({ commit, dispatch }, level) => {
  commit(PROCESSING_STARTED, 'welcomeBonusDetailLoading')
  dispatch('removeError', 'welcome_bonus_detail_loading')
  return get(MEMBER_DASHBOARD_WELCOME_BONUS_DETAIL + '/' + level)
    .then(response => {
      commit(PROCESSING_FINISHED, 'welcomeBonusDetailLoading')
      commit(SET_MEMBER_DASHBOARD_WELCOME_BONUS_DETAIL, response.data)
    })
    .catch(e => {
      commit(PROCESSING_FINISHED, 'welcomeBonusDetailLoading')
      commit(SET_MEMBER_DASHBOARD_WELCOME_BONUS_DETAIL, [])
      dispatch('logError', { welcome_bonus_detail_loading: e })
    })
}

export const loadBaInactiveDetail = ({ commit, dispatch }) => {
  commit(PROCESSING_STARTED, 'baInactiveDetailLoading')
  dispatch('removeError', 'ba_inactive_detail_loading')
  return get(MEMBER_DASHBOARD_BA_INACTIVE_DETAIL)
    .then(response => {
      commit(PROCESSING_FINISHED, 'baInactiveDetailLoading')
      commit(SET_MEMBER_DASHBOARD_BA_INACTIVE_DETAIL, response.data)
    })
    .catch(e => {
      commit(PROCESSING_FINISHED, 'baInactiveDetailLoading')
      commit(SET_MEMBER_DASHBOARD_BA_INACTIVE_DETAIL, [])
      dispatch('logError', { ba_inactive_detail_loading: e })
    })
}
