import url from '@/plugins/routes'
import axios from 'axios'

const state = {
  users: [],
  status: [],
  usersLoading: false,
  statusLoading: false,
}

const mutations = {
  setUsers: (state, users) => {
    state.users = users
  },
  setStatus: (state, status) => {
    state.status = status

  },
  updateUser: (state, user) => {
    let exists = state.users.find(a => {
      return a.username === user.username
    })
    if (exists) {
      exists.firstname = user.firstname
      exists.lastname = user.lastname
      exists.mail = user.mail
      exists.statusgroup = user.statusgroup
      exists.voting = user.voting
    } else {
      state.users.push({
        username: user.username,
        firstname: user.firstname,
        lastname: user.lastname,
        mail: user.mail,
        statusgroup: user.statusgroup,
        voting: user.voting
      })
    }
  },
  updateStatus: (state, status) => {
    let exists = state.status.find(a => {
      return a.id === status.id
    })
    if (exists) {
      exists.name = status.name
    } else {
      state.status.push(status)
    }
  },
  deleteStatus: (state, status) => {
    let index = state.status.indexOf(
      state.status.find(a => {
        return a.id === status.id
      })
    )
    state.status.splice(index, 1)
  },
  setUsersLoading: (state, value) => {
    state.usersLoading = value
  },
  setStatusLoading: (state, value) => {
    state.statusLoading = value
  }
}

const actions = {
  async getUsers({ commit, rootState, dispatch }) {
    try {
      commit('setUsersLoading', true)
      const response = await axios.get(url.getUsers, {
        headers: { Token: rootState.login.user.accessToken }
      })
      commit('setUsers', response.data)
      commit('setUsersLoading', false)
    } catch (e) {
      commit('setUsersLoading', false)
      if (e.response)
        if (e.response.status === 401) dispatch('logout', null, { root: true })
    }
  },
  async getStatus({ commit, rootState, dispatch }) {
    try {
      commit('setStatusLoading', true)
      const response = await axios.get(url.user.getAllStatus, {
        headers: { Token: rootState.login.user.accessToken }
      })
      commit('setStatus', response.data)
      commit('setStatusLoading', false)
    } catch (e) {
      commit('setStatusLoading', false)
      if (e.response)
        if (e.response.status === 401) dispatch('logout', null, { root: true })
    }
  },
  async updateStatusUser({ commit, rootState, dispatch }, data) {
    try {
      commit('setUsersLoading', true)
      const response = await axios.post(
        url.vorstand.um.updateStatusUser,
        { ...data },
        { headers: { Token: rootState.login.user.accessToken } }
      )
      commit('updateUser', response.data)
      commit('setUsersLoading', false)
    } catch (e) {
      commit('setUsersLoading', false)
      if (e.response)
        if (e.response.status === 401) dispatch('logout', null, { root: true })
    }
  },
  async updateVoting({ commit, rootState, dispatch }, data) {
    try {
      commit('setUsersLoading', true)
      const response = await axios.post(
        url.vorstand.um.updateVoting,
        { ...data },
        { headers: { Token: rootState.login.user.accessToken } }
      )
      commit('updateUser', response.data)
      commit('setUsersLoading', false)
    } catch (e) {
      commit('setUsersLoading', false)
      if (e.response)
        if (e.response.status === 401) dispatch('logout', null, { root: true })
    }
  }
}

const getters = {
  users: state => {
    return state.users
  },
  status: state => {
    return state.status
  },
  usersLoading: state => {
    return state.usersLoading
  },
  statusLoading: state => {
    return state.statusLoading
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}