let info = undefined;
let userInfoKey = undefined;

export const storage = {
    setUserInfoKey(key) {
        userInfoKey = key;
    },
    setUserInfo: (userInfo) => {
        info = userInfo;
        return localStorage.setItem(userInfoKey, JSON.stringify(userInfo));
    },

    getUserInfo: () => {
        return info || JSON.parse(localStorage.getItem(userInfoKey)) || {};
    },

    removeUserInfo: () => {
        info = null;
        return localStorage.removeItem(userInfoKey);
    },

    getToken() {
        return this.getUserInfo().token;
    },

    getPermissions() {
        return this.getUserInfo().permissions;
    },

    getUsername() {
        return this.getUserInfo().username;
    },

    getFullName() {
        return this.getUserInfo().fullName;
    },

    getAvatar() {
        return this.getUserInfo().avatar;
    },

    getNickname() {
        return this.getUserInfo().nickname;
    },

    getUserId() {
        return this.getUserInfo().id;
    },

    setString: (key, value) => {
        console.log("setString -  key:" + key + "  value:" + value);
        sessionStorage.setItem(key, value);
    },

    getString: (key, defaultValue) => {
        console.log("getString -  key:" + key);
        return sessionStorage.getItem(key) || defaultValue || "";
    },
    removeString: (key) => {
        console.log("remove -  key:" + key);
        sessionStorage.removeItem(key);
    },

    setData: (key, value) => {
        console.log("setData -  key:" + key + "  value:" + JSON.stringify(value));
        sessionStorage.setItem(key, JSON.stringify(value));
    },

    getData: (key, defaultValue) => {
        console.log("getData -  key:" + key);
        return JSON.parse(sessionStorage.getItem(key)) || defaultValue || {};
    },
    removeData: (key) => {
        console.log("remove -  key:" + key);
        sessionStorage.removeItem(key);
    }
}

export default {
    install: function (Vue) {
        Vue.prototype.$storage = storage;
    }
};
