import * as SecureStore from 'expo-secure-store';
import {LOGGING_IN} from "./mutations";

export async function LOGIN({ commit, state }, { userObj, userGroup, navigate }) {
    commit('LOGGING_IN', true);

    userObj = JSON.stringify(userObj);

    return new Promise((resolve, reject) => {
        commit('LOGIN_SUCCESSFUL', { userObj, userGroup });
        setTimeout(() => {
            SecureStore.setItemAsync('user', userObj).then(() => {
                SecureStore.setItemAsync('userGroup', userGroup).then(() => {
                    navigate('Drawer');
                    resolve();
                });
            });
        }, 1000)
    });
}

export function SET_USER({ commit, state }, { userObj, userGroup }) {
    userObj = JSON.stringify(userObj);
    return commit('LOGIN_SUCCESSFUL', { userObj , userGroup });
}

export function SET_LESSONS({ commit, state }, { lessons }) {
    return commit('LOADED_LESSONS', { lessons });
}

export function SET_CALLS({ commit, state }, { calls }) {
    return commit('LOADED_CALLS', { calls });
}

export function LOGOUT({ commit, state }, callback) {
    return new Promise((resolve, reject) => {
        SecureStore.deleteItemAsync('user').then(() => {
            callback();
            resolve();
        })
    })
}
