import common from '../common/js/common'

export default {

    TYPE_FRAME: 'FRAME',

    TYPE_BIZ: 'BIZ',

    TYPE_USER: 'USER',

    TYPE_PERSON_BIZ: 'BIZ_PERSON',

    // ------------------------------ User -----------------------------------

    uid: function() {
        return this.currUser() ? this.currUser().id : null
    },

    setUser: function(value) {
        localStorage.setItem(this.userKey(), JSON.stringify(value))
    },

    currUser: function() {
        return common.castEval(localStorage.getItem(this.userKey()) || '')
    },

    removeUser: function() {
        localStorage.removeItem(this.userKey())
    },

    // ------------------------------ Biz -----------------------------------

    bid: function() {
        return this.currBiz() ? this.currBiz().id : null
    },

    setBiz: function(value) {
        sessionStorage.setItem(this.bizKey(), JSON.stringify(value))
    },

    removeBiz: function() {
        sessionStorage.removeItem(this.bizKey())
    },

    currBiz: function() {
        return common.castEval(sessionStorage.getItem(this.bizKey()) || null)
    },

    // ------------------------------ Token -----------------------------------

    setToken: function(token) {
        localStorage.setItem(this.tokenKey(), token)
    },

    getToken: function() {
        return localStorage.getItem(this.tokenKey()) || null
    },

    removeToken: function() {
        localStorage.removeItem(this.tokenKey())
    },

    // ------------------------------ key -----------------------------------

    bizKey: function() {
        const key = this.hashCode(window.location.host + '_biz_' + this.getUserType())
        return key
    },

    userKey: function() {
        const key = this.hashCode(window.location.host + '_user_' + this.getUserType())
        return key
    },

    tokenKey: function() {
        const key = this.hashCode(window.location.host + '_token_' + this.getUserType())
        return key
    },

    // Frame
    setLoginInfo: function(value) {
        localStorage.setItem('KEY_ADMIN_USER_LOGIN_INFO', JSON.stringify(value))
    },
    getLoginInfo: function() {
        return common.castEval(localStorage.getItem('KEY_ADMIN_USER_LOGIN_INFO') || null)
    },
    removeLoginInfo: function() {
        localStorage.removeItem('KEY_ADMIN_USER_LOGIN_INFO')
    },

    // Biz
    setBizLoginInfo: function(value) {
        localStorage.setItem('KEY_BIZ_USER_LOGIN_INFO', JSON.stringify(value))
    },
    getBizLoginInfo: function() {
        return common.castEval(localStorage.getItem('KEY_BIZ_USER_LOGIN_INFO') || null)
    },
    removeBizLoginInfo: function() {
        localStorage.removeItem('KEY_BIZ_USER_LOGIN_INFO')
    },

    setUserType: function(value) {
        localStorage.setItem('KEY_USER_TYPE', value)
    },
    getUserType: function() {
        return localStorage.getItem('KEY_USER_TYPE') || 'TYPE_FRAME'
    },

    hashCode: function(str) {
        let hash = 0
        if (str.length === 0) return hash
        for (let i = 0; i < str.length; i++) {
            const char = str.charCodeAt(i)
            hash = ((hash << 5) - hash) + char
            hash = hash & hash // Convert to 32bit integer
        }
        return hash
    },
    /*
        定时缓存
     */
    setTimingLocalStorage: function(key, value, ttl_ms) {
        var data = { value: value, expirse: new Date(ttl_ms).getTime() }
        localStorage.setItem(key, JSON.stringify(data))
    },
    getTimingLocalStorage: function(key) {
        var data = JSON.parse(localStorage.getItem(key))
        if (data !== null) {
            if (data.expirse == null || (data.expirse != null && data.expirse < new Date().getTime())) {
                localStorage.removeItem(key)
            } else {
                return JSON.parse(data.value)
            }
        }
        return null
    }
}
