/* eslint-disable */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import log from '../../../assets/Log'

Vue.use(Vuex)
export default {
    namespaced: true,
    state: {
        isLoggedIn: log.isLoggedIn(),
        flag: false,
        status: '',
        token: undefined,
        user: undefined,
        person: undefined,
        agency: undefined,
    },
    mutations: {
        auth_request(state) {
            state.status = 'loading'
        },
        auth_success(state, user) {
            state.status = 'success'
            state.token = user.token
            state.user = user.user
            state.person = user.person
            state.agency = user.agency
        },
        auth_error(state) {
            state.status = 'error'
        },
        logout(state) {
            state.status = ''
            state.token = undefined
            state.person = undefined
            state.agency = undefined
            state.user = undefined
        },
        runInit(state, item) {
            state.flag = true
            state.user = item.user
            state.token = item.token
            state.person = item.person
            state.agency = item.agency
        },
        updateFlag(state) {
            state.flag = true
        },
    },
    getters: {
        authStatus: (state) => state.status,
        getPerson: () => log.getPerson(),
        getUser: () => log.getUser(),
    },
    actions: {
        init({ commit }) {
            if (!log.isLoggedIn() && log.tokenExpired) {
                log.clearValues()
            } else log.updateTokenExpiry()
            commit('runInit', {
                user: log.getJSONObject('user'),
                token: log.getJSONObject('token'),
                person: log.getJSONObject('person'),
                agency: log.getJSONObject('agency'),
            })
        },
        async login({ commit }, user) {
            await axios({
                    url: '/api/register/authenticate',
                    data: {
                        username: user.username,
                        password: user.password,
                    },
                    method: 'POST',
                })
                .then((resp) => {
                    log.clearValues()
                    log.setValues(resp.data)
                    commit('auth_success', resp.data)
                    return Promise.resolve()
                })
                .catch((ex) => {
                    commit('auth_error')
                    log.clearValues()
                    commit('auth_error', 'error occurred')
                    console.log(Promise.reject(ex.response));
                    return Promise.reject(ex.response)
                })
        },
        logout({ commit }) {
            return new Promise(() => {
                commit('logout')
                log.clearValues()
                delete axios.defaults.headers.common.Authorization
            })
        },
    },
}