var HuSNS = window.HuSNS || {};
HuSNS.User = {
    _currentUser: null,
    
    init: function(userData) {
        this._currentUser = userData;
    },
    
    getId: function() {
        return this._currentUser ? this._currentUser.id : null;
    },
    
    get: function() {
        return this._currentUser;
    },
    
    isLoggedIn: function() {
        return !!this._currentUser;
    },
    
    getAvatar: function() {
        return this._currentUser ? this._currentUser.avatar : null;
    },
    
    getUsername: function() {
        return this._currentUser ? this._currentUser.username : null;
    },
    
    isAdmin: function() {
        return this._currentUser ? this._currentUser.is_admin === true : false;
    },
    
    logout: function() {
        this._currentUser = null;
        if (typeof HuSNS.Request !== 'undefined') {
            HuSNS.Request.post('/api/user/logout')
                .then(function() {
                    window.location.href = HuSNS.Url.page('mobile/login');
                })
                .catch(function() {
                    window.location.href = HuSNS.Url.page('mobile/login');
                });
        } else {
            window.location.href = '/mobile/login';
        }
    }
};

if (typeof module !== 'undefined' && module.exports) {
    module.exports = HuSNS.User;
}